It's possible extend AR relationships?

Hi,

I’d like know if there are a way to create my own rules to AR relationships or extends the existent HAS_MANY, BELONGS_TO etc?

Thanks.

What for you need it?

Maybe you are trying to implement something that is already supported by actual implementation.

Explain us what exactly you want to achive.

I’d like to implement something like polymorphic relationships like rails.

In my app I’ll have some common models that will share information to the others models.

Eg.:

If I have a model called AttachedFiles, that will have a relation with all other applications models , I need to identify to each register the model owner and the object id.

So I’ll can have a model called StaticPage that have a polymorphic relationship with AttachedFiles, and another model called Documents that have the same relation without the need to create a many-to-many relationship.

Thanks!

You can do this without relationships extending. Look example in rails guide. And look here:




class Employee extends CActiveRecord

{

    public function relations()

    {

        return array(

            'pictures'=>array(self::HAS_MANY,'Picture','imageable_id','condition'=>'imageable_type=:imageable_type','params'=>array(':imageable_type'=>__CLASS__)),

        );

    }

}


class Product extends CActiveRecord

{

    public function relations()

    {

        return array(

            'pictures'=>array(self::HAS_MANY,'Picture','imageable_id','condition'=>'imageable_type=:imageable_type','params'=>array(':imageable_type'=>__CLASS__)),

        );

    }

}



Sure full implementation require override beforeDelete() and beforeSave() methods, but i think you understand idea. Another solution is to create PolymorphicBehavior.

creocoder, thanks, the behavior idea is what i was looking for.

Can you show me a example of how to implement this behavior? nothing really complex just a base idea to have a start.

Thanks

Nice! In the current app we are looking to move to Yii we do the equivalent of:

picture.product_id

picture.employee_id

and essentially have a BELONGS_TO relationship for each where the two fields are mutually exclusive. Is it better (more efficient) to do

picture.imageable_id

picture.imageable_type

assuming there are only the two relations?

Thanks again for all the support.

I’ll try to make it for yiiext soon. Base idea is to modify model relations, automatically add condition that i’m show above. And implement beforeSave() and beforeDelete() behavior methods to handle events.

mikeax

Not first, not second. I think that this is wrong data model in both situations (even polymorphic relationships). Right data model is to have node table and picture table with node_id FK. Other content belongs to node. I really belive that good data modeling is to keep >=3NF.

But, ok try to compare first and second variants. Second variant extensible without altering DB.

Thanks Creocoder,

I have been trying to keep our schema simpler as one of the challenges we had in our current app was performance with the complexity of Joins. Some of it has just been poor SQL by previous employees. It appears that Yii eliminates alot of this issue with the AR models.

I agree with you on ‘the way it should be’. Thanks.

And how would you resolve the relation “owner” in the Picture class?. I tried to do the next, but dind’t work. The problem is that when relations() is called, the model properties are not defined, so $this->imageable_type thows an exception.


class Picture extends CActiveRecord

{

    public function relations()

    {

        return array(

            'owner'=>array(self::BELONGS_TO, $this->imageable_type, 'imageable_id')

        );

    }

}