首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重写控制器时未定义索引

重写控制器时未定义索引
EN

Stack Overflow用户
提问于 2016-08-18 09:37:19
回答 1查看 428关注 0票数 0

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

这两种模型都有编码的关系。

Rayon.php

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

Profile.php (重写模型)中

代码语言:javascript
复制
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

代码语言:javascript
复制
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;时出错了

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-18 10:19:51

找到了修复错误的答案。

我使用人造丝模型,并编辑getRayon函数

代码语言:javascript
复制
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']);
}

谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39014772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档