MANY_MANY relation with conditions

I am having some serious headspins over this issue, my relation is setup in the Model, as well as being loaded in the controller. My code in he Model:


'proposed' => array(

                self::MANY_MANY,

                'Person',

                'tbl_models_events(model_id, event_id)',

                'condition' => 'proposed_proposed.status_id = :status',

                'params' => array(':status' => Lookup::item('ModelsEventStatus', ModelsEvents::STATUS_PROPOSED)),

                'together' => false

            ),

My code in the controller:




 public function actionView($id) {

        $event = Event::model()->with('confirmed', 'proposed')->findByPk((int) $id);

        $event->address = Address::model()->findByPk($event->address_id);

        $this->render('view', array(

            'event' => $event,

        ));

    }

i var_dump the result, and even though I have records in the db that match the criteria but i get empty result sets…???

Is my relation not correct or what am I doing wrong????

welcome to the forum…

one question: what is confirmed? is it a scope? can you try to just call the ‘proposed’, just that one… and then lets move on…

For me, I dont really understand that if the table that links both tables is named tbl_models_events, how come the condition is proposed_proposed.status_id instead of tbl_models_events.status_id… can you drop the resulted SQL here?

second question: why dont you have a an Address relation to the event? You could easily build your relation to the address like this:




// on relations Event model

'myaddress'=>array(self::HAS_ONE,'Address','address_id'),


// now i can access

$event->myaddress->propertyofaddress... 



Hi, thanks for your suggestions, I actually do have an address relation, which works perfectly, I was stomped on the proposed one, but now it works, and this is the code that fixed it…




'proposed' => array(

                self::MANY_MANY, 'Person', 'tbl_models_events(model_id, event_id)',

                'alias' => 'Models',

                'condition' => 'proposed_Models.status_id = :sid',

                'params' => array(':sid' => ModelsEvents::STATUS_PROPOSED),

                'together' => false

            ),

            'confirmed' => array(

                self::MANY_MANY, 'Person', 'tbl_models_events(model_id, event_id)',

                'alias' => 'Models',

                'condition' => 'confirmed_Models.status_id = :sid',

                'params' => array(':sid' => ModelsEvents::STATUS_CONFIRMED),

                'together' => false

            ),



now i’m not sure if this is double work and I only need one relation to get all models assigned to event, but in my event i am separating models in confirmd and proposed, in order to keep organized and easier to work with for the booker…

I still have no clue how to use the scopes for more advanced usage than filtering simple relations, like status active, which i have in my Person model


'active' => array(

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

            ),

but that is probably a topic for another post…

FYI my address relation that worked looks like this




'address' => array(self::BELONGS_TO, 'Address', 'address_id'),



for some reason i cant get it to work with has_one relation…

Hello,

i’ve got similar problem:

the relation looks like this:




'related' => array(

	self::MANY_MANY,

        'Table1',

        'RelationTable(userId, objectId)',

        'condition' => '"type" = :type',

        'params' => array(':type' => $this->type),

),

and if i try to get it work like:


$model = MyModel::model()->findByPk($pk);

$model->type = 1;

$related = $model->related;

i’ve got result as like “type” wasn’t set. Any ideas how to get it work?

I’m far from an expert, but try and copy my above code, and you don’t have to declare related as it automatically fetches the relation through lazy loading. I render my file with only one model and holding those relations so in my view i can call $model->relation->column but only render (


$this->render('viewfile'), array('model'=>$model));

hope that helps…

You can even call a relation of a relation in the view if you set the relations up right like so


$model->relation->child_relation->column

and so on…

Obs! what did the trick for me was adding the ‘alias’ and also in your model you have to declare a composite primary key as a function




 public function primaryKey() {

        return array('model_id', 'event_id');

    }