From browsing the forum I came across the WithRelatedBehavior extension but I’m struggling to figure out how to update the join tables when you re-save an object with a MANY_MANY relationship.
I’m currently populating a class var with an array of ID’s via a checkboxList, but saving with the related attribute populated with a new array of related objects doesn’t seem to affect the join table, is this not a feature or is it something I am doing wrong?
Thanks in advance for any help, code snippet below:
/**
* populate the relation with the necessary objects for the many/many save
* @param [string] $classArr [the related id's]
* @param [string] $class [the related object class name]
* @param [string] $relation [the name of the relation attribute]
* @return [type] [description]
*/
public function prepareMany($classArr, $class, $relation){
$manyObjs = array();
//must be an array, if not it is probably an empty string
if(!is_array($this->owner->$classArr)){
$this->owner->$classArr = array();
}
if(is_array($this->owner->$classArr)){
foreach($this->owner->$classArr as $id){
$obj = call_user_func(array($class,'model'));
$manyObjs[] = $obj->findByPk($id);
}
$this->owner->$relation = $manyObjs;
}else{
throw new Exception('the class var holding the many ID\'s ('.$classArr.') needs to be an array');
}
}