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?
Page 1 of 1
Relations And Arrays
#2
Posted 12 March 2014 - 06:09 AM
$userModel = cmp_user::model()->findAll(); foreach($userModel as $userItems) { echo $userItems->name." :".$userItems->cmp->company_name."<br>"; }
#3
Posted 12 March 2014 - 01:10 PM
Assume you comapny model relations is defined as
Then below code can get all employees for one company
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;
#4
Posted 13 March 2014 - 03:10 AM
Johnny Gan, on 12 March 2014 - 01:10 PM, said:
Assume you comapny model relations is defined as
Then below code can get all employees for one company
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?
#5
Posted 19 March 2014 - 09:39 AM
try this
$company = Company::model()->with('workers')->findByPk($currentUser->company_id); $workers = $company->workers(); foreach($workers as $worker) { echo $worker->name; echo $worker->phone; }
Share this topic:
Page 1 of 1