0
对于有许多客户端和拥有多个用户的客户端的用户,使用许多关系查询。尝试查看特定用户的特定客户记录。如果该客户端不与该用户关联,则重定向到其他页面。Yii多对多关系查询
// the relation in the client model
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(
'owners'=>array(self::MANY_MANY, 'User','owner_client(owner_id, client_id)'),
);
}
//the relation in the user model
public function relations()
{
return array(
'clients'=>array(self::MANY_MANY, 'Clients','owner_client(owner_id, client_id)'),
);
}
//determine if user can view this client
//client record
$client_record = Clients::model()->findByPk($id);
//many query to find users
$users = $client_record->owners;
//if user id is not found in array, redirect
if (!in_array(Yii::app()->user->id, $users))
{
$this->redirect(array('/site/dashboard'));
}
上面的代码重定向,尽管我知道客户是关系到登录
看起来像代码进出口寻找,但得到....活动记录“用户”试图选择一个无效的列“owner_id” – 2014-10-10 17:11:12
其实,我只注意到别的东西。我的MANY_MANY声明通常是这样的:''A'=>数组(self :: MANY_MANY,'A','B_A(A_id,B_id)'''我将'owner_id,client_id'交换成'client_id, owner_id' – Jerome 2014-10-10 17:14:59
从那里我了解了MANY_MANY,我注意到这是唯一的区别,但它仍然是一样的错误 – 2014-10-10 17:23:03