Active Record Newbie

Hi everyone!

I’m trying to do some relational querys with ActiveRecord.

I have a Tutorial model that BELONGS TO a Classroom model (Classroom has many Tutorials). And a Classroom has many Students, but Student has many Classrooms, so I did a relational table for that MANY TO MANY relation.

In Classroom I made this relation:




'class_student'=>array(self::HAS_MANY, 'ClassroomStudents', 'id_classroom'),

'students'=>array(self::HAS_MANY, 'Student', array('id_student'=>'id'), 'through'=>'class_student'),



What I want to do is, for a specific Student, show him the Tutorials from the Classrooms where he is.

In SQL a Right Join between Classroom and Students should do the trick (given the student id). How can I get that with Active Record?

I’m thinkg applying a criteria to Tutorial.




$criteria->with=array('Classroom'=>array('with'=>'students')))



But if I put the condition students.id = $id … it’s wrong because it is a HAS MANY relation.

Please help me! :D

ActiveRecord is doing it for You. If you want display students You should put there relation to classroom and from classroom to tutorials.

And if You do that you can easy show $model->classroom->tutorials->some_value, classroom is array and tutorials is array (couse of has_many relation) so You will need two loops to display it.

And $criteria->with is only if You want to do it in one query, otherwise You dont need it.

Thanks for your answer. But, is it possible the other way round? I mean, using CDBCriteria for Tutorial model, because I’m trying to show Classrooms Tutorials where the student is registered on, but at the same time, Tutorials of classrooms from a specific Teacher (Classroom has one teacher). That’s because I want to use the CListView widget.

Maybe this can help You:

http://www.yiiframework.com/wiki/428/drills-search-by-a-has_many-relation/

Thanks, I’m going to check it!