Hi! I have declared the communication between a company HAS_MANY users. The company may have many users… how to access all users of the company and display their data in the list?
Hi! I have declared the communication between a company HAS_MANY users. The company may have many users… how to access all users of the company and display their data in the list?
$userModel = cmp_user::model()->findAll();
foreach($userModel as $userItems)
{
echo $userItems->name." :".$userItems->cmp->company_name."<br>";
}
Assume you comapny model relations is defined as
public function relations()
{
return array(
'employees'=>array(
self::HAS_MANY,'User',array('company_id'=>'id'),
'joinType'=>'INNER JOIN'
),
);
}
Then below code can get all employees for one company
$company = Company::model()->with('employees')->findByPk(1);
$employees=$company->employees;
Thank you. At the moment, I am having a problem…
Instead users whose company_id, coincides with $currentUser->company_id I see the name of the user whose id matches id…
if the id of a company such as 30, in the foreach loop, I see the name of the user whose id is equal to 30 while it company_id empty or has a value of 25 for example.
Company Relations
'workers' => array(self::HAS_MANY, 'User', 'id'),
'owneer' => array(self::BELONGS_TO, 'User', 'owneer_id'),
User Relations
'companyOwneer' => array(self::HAS_ONE, 'Company', 'owneer_id'),
'company' => array(self::BELONGS_TO, 'Company', 'company_id'),
$currentUser = User::model()->findByPk(Yii::app()->user->id);
$company = Company::model()->findByPk($currentUser->company_id);
$workers = $company->workers();
foreach($workers as $worker) {
echo $worker->name;
echo $worker->phone;
}
will you described a method to solve the problem?
try this
$company = Company::model()->with('workers')->findByPk($currentUser->company_id);
$workers = $company->workers();
foreach($workers as $worker) {
echo $worker->name;
echo $worker->phone;
}