Pet*_*uza 4 crud relational-database relationship yii
我使用Yii的Gii的CRUD生成器作为一个模型,该模型具有与另一个模型定义的关系,期望它会创建某种下拉列表或其他方式来选择相关实体的实例,而只是它显示了一个常规文本框.
这是Gii的正常行为还是我做错了什么?
这是模型及其关系:
Model Event: relation 'Venue' => array( self::BELONGS_TO, 'Venue', 'venue' )
Model Venue: relation 'Events' => array( self::HAS_MANY, 'Event', 'venue' )
我期待事件CRUD能够展示一些选择场地实例的方法.
这只是Gii的正常行为,在生成表单时(对于CRUD和仅表单),它会使所有输入字段成为文本字段.因此,默认的gii CRUD和表单生成器在生成代码时不会考虑关系.对于您来说,
我们必须手动更改视图文件,即_form.php.
因此,根据您的要求,您可以对该文件进行以下更改:
/* As you have 'venue' field as the foreign key in the Event model */
<div class="row">
<?php echo $form->labelEx($model, 'venue'); ?>
<?php echo $form->dropDownList($model,'venue', CHtml::listData(Venue::model()->findAll(),
'id', //this is the attribute name(of Venue model- could be the id of the venue) for list option values
'name' // this is the attribute name(of Venue model- could be the name of the venue) for list option texts
)
); ?>
<?php echo $form->error($model,'venue'); ?>
</div>
Run Code Online (Sandbox Code Playgroud)
要进一步更改/自定义,请阅读有关CActiveForm的更多信息.
希望这可以帮助.