Yii Framework Forum: Active Relation question.. - Yii Framework Forum

Jump to content

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

Active Relation question.. Rate Topic: -----

#1 User is offline   papasj 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 01-June 09

Posted 01 June 2009 - 09:07 AM

Hello,

I've got 3 tables
table user with primary key userId
table books with primary key bookId
table userHasBook with primary key the 2 foreign keys userId and bookId. This table has also a boolean field named 'active' which is my main problem

I've created a relation in the books model


With a call like



I can get the books that a user has however my problem is that I want to get all books that a user has as long as the userHasBook.active field is 1 (true) ..

Unfortunately I don't have a way to access the 'active' value. Is there a way
to filter the results and get only the ones with userHasBook.active=1 ?

I've tried it by adding in the relation a 'condition'=>'??.active=1' but it results to an error since the query replaces the ?? with the table users and I have a condition where 'users.active=1' and of course mysql says that there is no such column.

Any ideas please ??
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,879
  • Joined: 04-October 08
  • Location:DC, USA

Posted 01 June 2009 - 09:59 AM

The alias for the join table is: <relation name>_<table alias>
So in your case, if you are using 'books' as the alias for your books model, then the join table alias is "has_books".
0

#3 User is offline   papasj 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 01-June 09

Posted 01 June 2009 - 01:02 PM

thanks! I addedd an alias for my users table and then as you said the the 'condition' of <relation_name>_<table_alias>.active=1 worked fine..

(sorry for posting in the wrong section.. after the submit I couldn't move the post)
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