dongluni0568 2015-06-25 05:23
浏览 52
已采纳

yii2 gii crud db关系(一对多)

gii generated models successfully (with relations) :

/**
 * @return \yii\db\ActiveQuery
 */
public function getClient()
{
    return $this->hasOne(Client::className(), ['id' => 'client_id']);
}

but when i generated crud, in client filed just input text field. Help me please, where is problem?

  • 写回答

1条回答 默认 最新

  • dtj2ww9500 2015-06-25 05:39
    关注

    That's correct. In your _form.php file you have to define a dropdown box if the user should choose a client:

    <?= $form->field($model, 'client')->dropDownList($clients) ?>
    

    and in controller actions create/update you have to provide the $clients:

    return $this->render('create', [  // or: return $this->render('update', [
        'model'    => $model,
        'clients' => ArrayHelper::map(Client::find()->all(), 'id', 'name'),
    ]);
    

    Don't forget to pass them in the view files for create.php and update.php to the _form.php file:

    <?= $this->render('_form', [
        'model' => $model,
        'clients' => $clients, // <-- added
    ]) ?>
    

    In other views where you just want to show client you may use this:

    echo $model->client->name; //or something different
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部