覆盖控制器时未定义的索引

时间:2016-08-18 09:37:19

标签: php yii2 yii2-advanced-app yii2-user

所以在Profile表中我创建了一个名为' rayon_id'的新字段。这是我的新表Rayon' id'

的FK

两个模型都有编码关系。

Rayon.php

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

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;

时出错

enter image description here

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

1 个答案:

答案 0 :(得分: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']);
}

谢谢。