0

因此,在 Profile 表中,我创建了名为“rayon_id”的新字段,这是我的新表 Rayon 的“id”的 FK

两个模型都对关系进行了编码。

Rayon.php

/**
 * @return \yii\db\ActiveQuery
 */
public function getProfiles()
{
    return $this->hasMany(Profile::className(), ['rayon_id' => 'id']);
}

和 In Profile.php(覆盖模型)

namespace app\models;

use dektrium\user\models\Profile as BaseProfile;

class Profile extends BaseProfile
{
    public function getRayon()
    {
        return $this->hasOne($this->module->modelMap['Rayon'], ['id' => 'rayon_id']);
    }

这里是我重写的控制器AdminController.php

namespace app\controllers\user;

use dektrium\user\controllers\AdminController as BaseAdminController;
use Yii;
use yii\helpers\Url;
use backend\models\Rayon;

class AdminController extends BaseAdminController
{
    public function actionUpdateProfile($id)
    {
        Url::remember('', 'actions-redirect');
        $user    = $this->findModel($id);
        $profile = $user->profile;
        $rayon = $profile->rayon;

添加时出现错误$rayon = $profile->rayon;

在此处输入图像描述

为什么我得到未定义的索引?

4

1 回答 1

0

找到修复错误的答案。

我使用 Rayon 模型,并编辑 getRayon 函数

namespace app\models;

use backend\models\Rayon;
use dektrium\user\models\Profile as BaseProfile;

class Profile extends BaseProfile
{
    public function getRayon()
    {
        return $this->hasOne(Rayon::className(), ['id' => 'rayon_id']);
        //return $this->hasOne($this->module->modelMap['Rayon'], ['id' => 'rayon_id']);
}

谢谢你。

于 2016-08-18T10:19:51.280 回答