Yii Framework Forum: Nested Relational criteria in search() - Yii Framework Forum

Jump to content

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

Nested Relational criteria in search() Rate Topic: -----

#1 User is offline   jcsmesquita 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 27-July 11

Posted 26 September 2011 - 05:13 AM

I've just spent a few hours trying to figure this out, turns out you can nest relational criteria in the search() function in a model.

Like this:
$criteria->with = array('event' => array('with' => 'student_user'));
$criteria->compare("student_user.username", $this->student_username, true);


Both 'event' and 'student_user' are BELONGS_TO relations. 'student_username' is a public variable defined at the beginning of my model (and set as safe in my rules). I've been trying use the following in my CGridView widget columns:

array(
    'name' => 'student_username',
    'header' => 'Student',
    'type' => 'raw',
    'value' => '$data->event->student_user->username'
),


The nested criteria helped me get the search function working, hope this helps someone :)

jc
4

#2 User is offline   cnanders 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 22-January 11

Posted 23 March 2012 - 02:25 PM

Thank you SO much for this post! You just saved me hours of frustration. It is amazing how simple that nested "with" statement works. One thing that had me confused is that I was trying to do the equivalent of


$criteria->compare(event.student_user.username,"some_name",true) 


and it kept giving me the error "Unknown column 'event.student_user.username' in where clause". I still think it is a bit strange that you just use student_user.username. I imagine this is standard convention for JOIN queries. I'm just not that familiar with them.

Once again, I amazed at how powerful and awesome Yii is.
0

#3 User is offline   jcsmesquita 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 27-July 11

Posted 24 March 2012 - 02:29 AM

I'm glad it helped :)
0

#4 User is offline   cappadochian 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 147
  • Joined: 02-January 11

Posted 30 November 2012 - 01:38 PM

View Postjcsmesquita, on 24 March 2012 - 02:29 AM, said:

I'm glad it helped :)


Hi jcsmesquita,

in which model have you done this?
0

#5 User is offline   jcsmesquita 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 27-July 11

Posted 04 June 2013 - 12:53 PM

View Postcappadochian, on 30 November 2012 - 01:38 PM, said:

Hi jcsmesquita,

in which model have you done this?

Hi cappadochian,

I did it in the parent model "Events".
0

#6 User is offline   le_top 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 280
  • Joined: 08-June 10
  • Location:France

Posted 04 June 2013 - 01:53 PM

Yes You Can, and it is even easier if you use my RelatedSearchBehavior: http://www.yiiframew...searchbehavior/ .
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