Yii Framework Forum: Multiple joins in yii and CDbCriteria - Yii Framework Forum

Jump to content

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

Multiple joins in yii and CDbCriteria Rate Topic: -----

#1 User is offline   Addev 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 05-May 12

Posted 17 May 2012 - 06:44 PM

I'm starting with yii.

I have the following DB structure:

    Table:             Rows:

    user               [id,login,password,name,email]
    userToProject      [user_id,project_id,role]
    project            [id,name,status]



And I want to retrieve all the users working in a project with **status=3** as **role=manager**. And this is my code by the way, I need to make the second join to reach the project status.

	$criteria=new CDbCriteria;
	$criteria->join='INNER JOIN {{userToProject}} a ON t.id=a.user_id and a.role='.Role::MANAGER;
	$criteria->distinct=true;
	return User::model()->findAll($criteria);


Can I make it with a criteria or should I implement an SQLcommand and run it?

Thanks
0

#2 User is offline   Pravin Gajera 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 137
  • Joined: 25-October 11
  • Location:India

Posted 17 May 2012 - 11:23 PM

Hello

You can use relation (IN model) here as well.

http://www.yiiframew...en/database.arr

Thanks
0

#3 User is offline   yiwi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 20-January 12

Posted 18 May 2012 - 12:25 AM

Yeah relations are the way to go:

Model User:
'usertoproject' => array(self::HAS_MANY, 'UserToProject', 'user_id')


Model UserToProject:
'project' => array(self::BELONGS_TO, 'Project', 'project_id')


Getting your values:
$m = User::model()->with(array('usertoproject', 'usertoproject.project'=>array('alias'=>'project')))->findAll('project.status=3 AND usertoproject.role=?', array(Role::MANAGER));

0

#4 User is offline   Addev 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 05-May 12

Posted 18 May 2012 - 03:25 PM

Thanks a lot
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