How to access many_many relation through scopes

I’ve been trying to figure this one out forever, I want to check in scopes if a model is currently staffed at an event, and the table is a many_many table with model_id and event_id as foreign keys.

I apologize if this is a noob question, I’m a designer turned developer, and just love Yii’s fairly simple learning curve, and power, but now i’ve seemed to hit several walls in my app.

Im trying something like this at the moment:

In controller:


 $avail_models = Person::model()->with('picture')->active()->notStaffed()->people(2)->findAll();



In model:


 public function scopes(){

     return array(

                'people'=>array(

                    'order' => 'first_name DESC'

                ),

                'active'=>array(

                    'condition'=>'t.status_id ='.Status::STATUS_ACTIVE

                ),

                'has_image'=>array(

                    'condition'=>'profile_image_id <> NULL'

                ),

                'notStaffed'=>array(

                    'order' => 'first_name DESC'


                )

            );

        }




 public function people($cat=2){


            $this->getDbCriteria()->mergeWith(array(

               'condition'=>'category_id ='.$cat,

            ));

            return $this;

        }

        

        public function notStaffed($event_id){

            $this->getDbCriteria()->mergeWith(array(

                'condition'=>'tbl_models_events.model_id != :mid AND tbl_models_events.event_id !=:eid',

                'params'=>array(':mid'=>$this->id, ':eid'=>$event_id),

            ));

            return $this;

        }

I get an error saying my db has unknown column…?

I had a issue similar to yours.

My problem was that, the table that joined the other two in a many-to-many relation had some additional fields that i needed and i tried everything with AR and didn’t worked (i’m no expert with Yii).

In the end, i upgraded to 1.1.6, and i used the query builder.

Maybe there is a way, but i couldn’t find it, and as always, i can’t waste too much time with a minor issue when i have alternative, that’s the reason why i used the query builder.

Take a look: http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder

I believe you can do same thing, because the query builder is just awesome.

Thank you for your response, I did some more searching, and it seems that I went about it all wrong.

Yii apparently needs to use "relation_relation.column" to work in scopes…

But still no luck in incorporating even that… I’m very very new to both oop and Yii, so I guess more studying is required… I hope more tutorials and use cases are posted soon cause it really is a great framework…