Yii Framework Forum: Relations And Arrays - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Relations And Arrays Rate Topic: -----

#1 User is offline   bot 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 22-December 13

Posted 12 March 2014 - 01:51 AM

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?
0

#2 User is offline   RKA 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 19-February 13

Posted 12 March 2014 - 06:09 AM

$userModel = cmp_user::model()->findAll();
foreach($userModel as $userItems)
{
	echo $userItems->name." :".$userItems->cmp->company_name."<br>";
}

0

#3 User is offline   Johnny Gan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 198
  • Joined: 30-May 13

Posted 12 March 2014 - 01:10 PM

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;

0

#4 User is offline   bot 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 22-December 13

Posted 13 March 2014 - 03:10 AM

View PostJohnny Gan, on 12 March 2014 - 01:10 PM, said:

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?
0

#5 User is offline   Johnny Gan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 198
  • Joined: 30-May 13

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;
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users