Skip to content

Instantly share code, notes, and snippets.

@sawantuday
Created November 13, 2013 14:53
Show Gist options
  • Save sawantuday/7450345 to your computer and use it in GitHub Desktop.
Save sawantuday/7450345 to your computer and use it in GitHub Desktop.
Saving many to many relation
<?php
class CCActiveRecord extends CActiveRecord
{
/**
* @var Boolean when set to true we need to save many to many relation
*/
protected $MMRelation = false;
/**
* A multidimentional array holding relation data
* KEY = related model
* VALUE = array of one or more related ids
* @var array
*/
protected $MMRelationData = array();
/**
* Inserts a record in many to many relation
* @param String $modelName Name of related model
* @param array $data one or more ids of related model
*/
public function addMMRelation($modelName, $data){
$this->MMRelation = true;
$this->MMRelationData[$modelName] = $data;
}
/**
* (non-PHPdoc)
* @see CActiveRecord::afterSave()
*/
public function afterSave(){
//check if model has specified any MMRelation
if($this->MMRelation){
foreach ($this->MMRelationData as $key => $value){
//key is related model. check i fany such relation is specified
$relations = $this->relations();
if( count($relations) < 1
|| !isset($relations[$key])
|| $relations[$key][0]!=self::MANY_MANY){
Logger::error(__CLASS__.': Relation '.$key.' not found');
}
$relation = $relations[$key];
$arr = explode('(', $relation[2]);
$modelName = $arr[0];
$arr2 = explode(')', $arr[1]);
$attributes = explode(',', $arr2[0]);
if(count($value) > 0){
foreach($value as $val){
Yii::app()->db->createCommand()->insert(
$modelName, array(
trim($attributes[0])=>$val,
trim($attributes[1])=>$this->id,
));
/* $model = new $$modelName;
$model->$$attributes[1] = $this->id;
$model->$$attributes[0] = $val;
//TODO : validate both keys exists in respective tables before saving
if(!$model->save()){
Logger::error('Error while storing many to many relation');
} */
}
}
}
}
}
}
<?php
class Department extends CCActiveRecord
{
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'relUser'=> array(self::HAS_ONE, 'User', 'group_id'),
//specify your many to many relation
//array(self::MANY_MANY, 'relatedModelName', 'RelationTable(foreignKey, localKey)')
'user'=>array(self::MANY_MANY, 'User', 'cc_user_group_relation(user_id, group_id)')
);
}
}
<?php
class someController extends Controller
{
public function actionTest(){
$user = User::model()->findByPk('13');
//you just need to add this line
$user->addMMRelation('department', array('10'));
//and then normal save
$user->save();
}
}
<?php
class User extends CCActiveRecord
{
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
//'department'=> array(self::BELONGS_TO, 'Department', 'group_id'),
//specify your many to many relation
//array(self::MANY_MANY, 'relatedModelName', 'RelationTable(foreignKey, localKey)')
'department'=>array(self::MANY_MANY, 'Department', 'cc_user_group_relation(group_id, user_id)'),
'profile'=>array(self::HAS_ONE, 'UserProfile', 'id')
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment