Creating relations from a behavior

I was wondering if an active record behaviour could add relations on the fly?

pieter

Yes, it can (not documented though.)

In the behavior, you first need to retrieve the meta information of the AR model using:

$this->owner->metaData

You can create a new instance of CActiveRelation (its child class) and add it to the meta data's relations array.

Can you give an example when I want to use it in Controller? Thanks


    public function localized($lang=null)

    {

        $obj = $this->Owner;


        $class = ActiveRecord::HAS_ONE;


        $obj->getMetaData()->relations['localized'] =

            new $class('localized',

                $this->getLangClassName(),

                $this->getLangForeignKey(),

                array(

                    'condition' => sprintf("`localized`.`%s`='%s'", $this->langField, $lang),

                )

            );


        return $obj->with('localized');

    }

localized method is behavior public method, and it’s scope name for model

This is very interesting.

e.g., in symfony there is a commentable behavior, every record can have comments stored in a relation. Can anyone show, how to organize and package such a behavior with models, views and widgets?