Class CSaveRelationsBehavior
CSaveRelationsBehavior class.
The CSaveRelationBehavior enables ActiveRecord model to save HasMany and ManyMany relational active records along with the main model
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 (http://www.yiiframework.com/doc/guide/form.table)
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.
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
- CComponent
- CBehavior implements IBehavior
- CModelBehavior
- CActiveRecordBehavior
- CSaveRelationsBehavior
License: CSaveRelationsBehavior
Author: Alban Jubert <alban.jubert@trinidev.fr>
Version: 1.0.3
Link: http://www.trinidev.fr/
Located at x2engine/protected/components/CSaveRelationsBehavior.php
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
#
beforeValidate( CModelEvent $event )
Responds to CModel::onBeforeValidate event. Override this method and make it public if you want to handle the corresponding event of the owner. You may set CModelEvent::isValid to be false to quit the validation process. |
public
|
#
beforeSave( CModelEvent $event )
Responds to CActiveRecord::onBeforeSave event. Override this method and make it public if you want to handle the corresponding event of the CBehavior::owner owner. You may set CModelEvent::isValid to be false to quit the saving process. |
public
|
|
public
|
#
beforeDelete( CEvent $event )
Responds to CActiveRecord::onBeforeDelete event. Override this method and make it public if you want to handle the corresponding event of the CBehavior::owner owner. You may set CModelEvent::isValid to be false to quit the deletion process. |
public
|
#
afterDelete( CEvent $event )
Responds to CActiveRecord::onAfterDelete event. Override this method and make it public if you want to handle the corresponding event of the CBehavior::owner owner. |
afterFind(),
beforeCount(),
beforeFind(),
events()
|
afterConstruct(),
afterValidate()
|
attach(),
detach(),
getEnabled(),
getOwner(),
setEnabled()
|
__call(),
__get(),
__isset(),
__set(),
__unset(),
asa(),
attachBehavior(),
attachBehaviors(),
attachEventHandler(),
canGetProperty(),
canSetProperty(),
detachBehavior(),
detachBehaviors(),
detachEventHandler(),
disableBehavior(),
disableBehaviors(),
enableBehavior(),
enableBehaviors(),
evaluateExpression(),
getEventHandlers(),
hasEvent(),
hasEventHandler(),
hasProperty(),
raiseEvent()
|
public
array
|
$relations | array() |
|
public
boolean
|
$transactional | true |
|
public
boolean
|
$hasError | false |
|
public
boolean
|
$deleteRelatedRecords | true |
$owner
|
$enabled
|