I’m completely stuck with a query I’m trying to use to provide data to a CGridView widget. I’ve read through everything I can find on AR and have made some progress, but can’t quite get there. I’ll describe my aim at the bottom. My table structure is attached as an image (I’m not allowed to use links in posts yet).
User.php model:
public function relations()
{
return array(
'customers' => array( self::MANY_MANY, 'Customer', 'UserCustomer(userId, customerId)' ),
'passReset' => array( self::HAS_ONE, 'PassReset', 'id' ),
'userGroup' => array( self::BELONGS_TO, 'UserGroup', 'userGroupId' ),
'userInfo' => array( self::HAS_ONE, 'UserInfo', 'id' ),
);
}
public function search()
{
$criteria = new CDbCriteria;
$criteria->with = array('userInfo', 'userGroup');
// Conditions to retrieve the data this user is allowed to view.
if ( Yii::app()->user->checkAccess('manager') ) {
// TODO: Restrict user data displayed such that a manager (logged-in user)
// can only see data on users from the same customerId (organisation) as them
$criteria->addInCondition( 'userGroup.name', array('staff', 'guest') );
}
$sort = new CSort();
$sort->attributes = array(
'lastName' => array(
'asc' => 'userInfo.lastName',
'desc' => 'userInfo.lastName DESC',
),
'firstName' => array(
'asc' => 'userInfo.firstName',
'desc' => 'userInfo.firstName DESC',
),
'userGroup' => array(
'asc' => 'userGroup.name',
'desc' => 'userGroup.name DESC',
),
);
return new CActiveDataProvider( $this, array(
'criteria' => $criteria,
'sort' => $sort,
));
}
So in the search function, there’s a ‘TODO’ comment. That’s pretty much what I can’t get. The query works great so far - returns correct info for a user in the admin group (all info). But for a user in the manager group I want it to get all userInfo data for users who are in the guest or staff group who are also from the same customer (i.e. have the same customerId as the manager). At the moment a manager can only see staff and guests, but they can see them from every customer organisation (not just their own).
Any help would be greatly appreciated - this is my first project learning Yii and doubles as a school project!