因此,在 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;
为什么我得到未定义的索引?