|
Project Information
Featured
Downloads
Links
|
The CSaveRelationsBehavior enables ActiveRecord model to save HasMany and ManyMany relational active records along with the main model Requirements
Installation
Usage
public function behaviors(){
return array('CSaveRelationsBehavior' => array('class' => 'application.components.CSaveRelationsBehavior'));
}$model->setRelationRecords('relationName',$data);
$model->save();You will typically get this data from some checkboxes values listing the ids of the related model.
You can get this data by using the input techniquehttp://www.yiiframework.com/doc/guide/form.table within the form of the main model In both cases, the foreign keys related to the main model will automatically be populated with its primary key(s) value(s).
Most of the time, you will call the setRelationRecords that way:
By default, the behavior will handle the save operation in a transactional way so that if anything goes wrong during the save of some related data, your relational integrity will not be affected. If you prefer to handle this yourself, you can set the 'transactional' property of the behavior to false. Also, if any error occurs during the save process, the 'hasError' property will be set to true. Delete of related records is automaticaly managed.
If you dont want the behavior to take care of it, just set the deleteRelatedRecords property to false:
Additional features:
You can use this method to force the save of some relation. You can also set the error message of the relation by using the second parameter (see setSaveRelationMessage below)
Simply do the oposite
Set the message to be shown in the error summary of the main model
Set the validation scenario to be applied to each related record Bugs, feedback, enhancements:Any bug report, feedback or enhancements requests will be highly appreciated. |