Yii Framework Forum: How Activerecord Load Object Set In Complicated Relationship - Yii Framework Forum

Jump to content

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

How Activerecord Load Object Set In Complicated Relationship Rate Topic: -----

#1 User is offline   Kable 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 21-February 13

Posted 22 February 2013 - 08:12 AM

Hi everybody

I've three DB tables for two objects with MANY-MANY relationship, as below

table user (id int, ......)
table subject (id int, usn int, ......)
table user_subject (user_id int, subject_id int, role int)

user_subject table links user and subject tables by foreign key user_id and subject_id.

I'd like to load all subjects associated with user 1 whose role as 1, and have usn between 1 to 20.

I use following code to do this:

$criteria = new CDbCriteria;
$criteria->with = array('users');
$criteria->condition = "user_id=:userID AND role=1 AND usn>=:startUSN AND usn<=:endUSN";
$criteria->params = array(':userID' => 1, ':startUSN' => 1, ':endUSN' => 20);
$subject = Subject::model()->findAll($criteria);


But the result I got for $subject is null.

Could anyone tell me where I did wrong?

Thanks for your time!
0

#2 User is offline   ragua 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 122
  • Joined: 23-January 12
  • Location:Paris - France

Posted 22 February 2013 - 08:23 AM

Hi,
take a look at this:
http://www.yiiframew...iteria-together

If it doesn't work, try to set bad value to see the SQL request sent to database.
0

#3 User is offline   Kable 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 21-February 13

Posted 22 February 2013 - 09:08 AM

View Postragua, on 22 February 2013 - 08:23 AM, said:

Hi,
take a look at this:
http://www.yiiframew...iteria-together

If it doesn't work, try to set bad value to see the SQL request sent to database.


Thanks ragua.

I tried to add together=true to my code. The outcome is the same as without it. Then I tried to pass string value to an int parameter. But I still get same 'null' results and without any error report. So I suppose that the findAll returns before send any request to the database.
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 1,531
  • Joined: 16-February 11
  • Location:Japan

Posted 22 February 2013 - 09:53 AM

Hi Kalbe, welcome to the forum.

You don't have to specify 'together' when there's no 'OFFSET' or 'LIMIT' in the condition. 'with' is enough for the current scenario.

View PostKable, on 22 February 2013 - 08:12 AM, said:

table user (id int, ......)
table subject (id int, usn int, ......)
table user_subject (user_id int, subject_id int, role int)
...
$criteria = new CDbCriteria;
$criteria->with = array('users');
$criteria->condition = "user_id=:userID AND role=1 AND usn>=:startUSN AND usn<=:endUSN";
$criteria->params = array(':userID' => 1, ':startUSN' => 1, ':endUSN' => 20);
$subject = Subject::model()->findAll($criteria);


But the result I got for $subject is null.


I would try the plain sql for debugging. Something like ...
select * from subject
  join user_subject on subject.id = user_subject.subject_id
  join user on user_subject.user_id = user.id
where user_id = 1 and role = 1 and usn >= 1 and usn <= 20

Does it really return some records? Or, is empty result the correct outcome?
0

#5 User is offline   Kable 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 21-February 13

Posted 22 February 2013 - 09:55 AM

question doesn't have right answer yet
0

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 1,531
  • Joined: 16-February 11
  • Location:Japan

Posted 22 February 2013 - 07:42 PM

View PostKable, on 22 February 2013 - 09:55 AM, said:

question doesn't have right answer yet

:D
Keep debugging!
0

#7 User is offline   Kable 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 21-February 13

Posted 22 February 2013 - 09:51 PM

View Postsoftark, on 22 February 2013 - 07:42 PM, said:

:D
Keep debugging!


Hi softark,

Thank you for your advice!

The problem solved. It was my own mistake, passing wrong value to the criteria parameter.

Thanks again for your time! and have a nice day

This discussion is closed, problem solved.
0

#8 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 1,531
  • Joined: 16-February 11
  • Location:Japan

Posted 23 February 2013 - 01:07 AM

Yeah, that's great. :)
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