save-relations-ar-behavior CSaveRelationsBehavior: Save HasMany and ManyMany relational active records along with the main model

  1. Documentation
  2. Change Log

The CSaveRelationsBehavior enables ActiveRecord model to save HasMany and ManyMany relational active records along with the main model

Version 1.0.3 update:
  • Correct the way in which the related records are found for HAS_MANY relations in the setRelationRecords method
  • Updated demonstration application

Demonstration applications are available in the download section.

Resources

Any bug report, feedback or enhancements requests will be highly appreciated.

Documentation

Requirements
  • Yii Framework 1.0.4 or later
Installation
  • Extract the release file under protected/components
Usage
  • Add the following code to the models you wish to add this behavior to:
public function behaviors(){
 		return array('CSaveRelationsBehavior' => array('class' => 'application.components.CSaveRelationsBehavior'));
}

In your controler, to save the relations data, simply call:

$model->setRelationRecords('relationName',$data);
$model->save();
  • For ManyMany relations, $data is either an array of foreign key values (ie. array(2,5,43)) or an array of associative arrays giving the composite foreign keys values of the related model (ie. array(array('pk1'=>2,'pk2'=>'fr'),array('pk1'=>5,'pk2'=>'en'))). You will typically get this data from some checkboxes values listing the ids of the related model.
  • For HasMany relations, $data should be set as an array of associative arrays giving the attributes values of the related model (ie. array(array('id'=>123, 'name'=>'someValue', 'visible'=>true), array('id'=>456, 'name'=>'someOtherValue', 'visible'=>false))); You can get this data by using the tabular input technique 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:

$model->setRelationRecords('relationName',is_array(@$_POST['ModelName']) ? $_POST['ModelName'] : array());

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:

public function behaviors(){
		return array('CSaveRelationsBehavior' => array('class' => 'application.components.CSaveRelationsBehavior', 'deleteRelatedRecords'=>false));
}
Additional features:
  • $model->addSaveRelation('relationName'[,'customErrorMessage']) 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)
  • $model->removeSaveRelation('relationName') Simply do the oposite
  • $model->setSaveRelationMessage('relationName','customErrorMessage') Set the message to be shown in the error summary of the main model
  • $model->setRelationScenario('relationName','scenario') Set the validation scenario to be applied to each related record
Demonstration

You can try this behavior by using the provided demo application in the download section. Unzip the content to your web server and take a look to the README file to know how to setup this application.

Bugs, feedback, enhancements:

Any bug report, feedback or enhancements requests will be highly appreciated.

Change Log

June 19, 2010
  • v1.0.3 release
    • Correct the way in which the related records are found for HAS_MANY relations in the setRelationRecords method
June 12, 2010
  • v1.0.2 release
    • Enh: Added validation scenario support for HAS_MANY relations
    • Enh: Relation initialisation optimisation code
    • Enh: Added delete related records support
May 16, 2010
  • v1.0.1 release
    • Bug #1: 'save' attribut of the relations property may not be set correctly
    • Enh: Better validation handling; it is now performed in the beforeValidation event
May 15, 2010
  • Demonstration applications for Yii Framework 1.0 and 1.1
May 13, 2010
  • v1.0 Initial release.
7 3
19 followers
5 157 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Database
Developed by: juban_
Created on: May 13, 2010
Last updated: 13 years ago

Downloads

show all

Related Extensions