我的yii应用程序出现了一个奇怪的问题。我的数据库Players-id,name,team_id和Teams-id,name中有两个表。我可以创建新的玩家,但当我想查看玩家的个人资料时,出现了一个错误——"试图获取非对象的属性":
'value'=>$model->team->NAME,
最奇怪的问题是,当我为ID为1和2的玩家测试url时,一切都很好,我看到了正确的信息,但对于其他ID,我有这个问题。这是我的部分代码:
view.php
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'ID',
'NAME',
'TEAM_ID',
array(
'label'=>'Отбор',
'type'=>'text',
'value'=>$model->team->NAME,
),
),
)); ?>
播放器.php
public function relations()
return array(
'team' => array(self::BELONGS_TO, 'TEAMS', 'ID'),
);
}
Teams.php
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'player' => array(self::HAS_MANY, 'PLAYERS', 'ID'),
);
}
PlayersController.php
public function actionView($id)
{
$teams = new CActiveDataProvider('Teams');
$players = new CActiveDataProvider('Players');
$this->render('view', array(
'model'=>$this->loadModel($id),
));
}
似乎需要修复Players
模型中relations
的错误:
public function relations()
return array(
'team' => array(self::BELONGS_TO, 'TEAMS', 'TEAM_ID'), // TEAM_ID instead of ID
);
}