【问题标题】:YII: how to display related table field on attributes?YII:如何在属性上显示相关的表字段?
【发布时间】:2012-09-06 03:25:33
【问题描述】:

我是 YII 的新手,我有一个问题...... 我有一个像

这样的模型
Person {
   $id ; 
   $name ; 
   $address ; 
}


Car{
   $id ;
   $carLicenseNumber ; 
   $person_id ; // BELONGS TO PERSON 
}

我有一个 CJuiAutoComplete,但我不知道当它想要更新时如何在字段上显示人名

$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
    'model'=>$model,
    'id'=>'person',
    'name'=>'person',

    'attribute'=> 'person->name', // I WANT THIS but no idea how to do this ... 

    'source'=>$this->createUrl('person/suggestPerson'),
    'options'=>array(
        'delay'=>150,
        'minLength'=>2,
        'showAnim'=>'fold',
        'select'=>"js:function(event, ui) {
            $('#label').val(ui.item.label);
            $('#temp_person_id').val(ui.item.id);
        }"
    ),
    'htmlOptions'=>array(
        'size'=>'40'
    ),
));

如何在属性上显示person->name?

注意:$model 是 Car 实例

我尝试使用像

这样的临时变量

$temp = $model->person->name ; 然后将 $temp 分配给 'attribute'=>$temp 效果很好……

我只想知道如何在文本字段或自动完成字段上分配相关字段的正确方法。

【问题讨论】:

  • 好的,在您的这种形式中,您是否打算为person(姓名、身份证、地址等个人详细信息)也采用新值?除了car 详细信息。
  • 嗯,看来您已经找到了解决方案,祝您好运。

标签: php yii


【解决方案1】:

在我与一些 YII 开发人员在 Live Chat 上聊天后,我找到了解决方案

$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
    'model'=>$model->person, // change this to model->person instead of model
    'id'=>'person_name',
    'name'=>'person_name',

    'attribute'=> 'name', // just name

    'source'=>$this->createUrl('person/suggestPerson'),
    'options'=>array(
        'delay'=>150,
        'minLength'=>2,
        'showAnim'=>'fold',
        'select'=>"js:function(event, ui) {
            $('#label').val(ui.item.label);
            $('#temp_person_id').val(ui.item.id);
        }"
    ),
    'htmlOptions'=>array(
        'size'=>'40'
    ),
));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 2023-03-12
    相关资源
    最近更新 更多