CActiveRecord relations "createIfNull" option

Add the option "createIfNull" to HAS_ONE and BELONGS_TO relation definitions:




'varName'=>array(self::BELONGS_TO, 'className', 'foreign_key', /* options...*/ 'createIfNull'=>true) 

So you can do:




$modelA = new ModelA();

$modelA->relatedModelB->name='I am your father'

 

It helps to CGridView to filter related models when you define columns as Strings like "relatedModelB.name".

Edit: Please rename the post to "CActiveRecord relations “createIfNull” option, current name is very general :blink: