/** * Overrides {@link epManagerBase::delete()} to deal with object relationships * @param object * @return bool * @access public */ public function delete(&$o = null) { // get class name if (!($class = $this->_getClass($o))) { return false; } // get class map for class if (!($cm =& $this->_getMap($class))) { return false; } // get oid before it's deleted $oid = $o->epGetObjectId(); // remove inverse relationships from memory $o->epUpdateInverse(epObject::INVERSE_REMOVE); // check if class has any composed_of fields if (!($cofs = $cm->getComposedOf())) { // if not, delete all relations from and to the object $status = parent::delete($o); // remove relations from and to the class $status &= $this->_deleteRelations($class, $oid); return $status; } // otherwise, go through composed_of fields and delete each object composed of $status = true; foreach ($cofs as $cof) { // get the value for the composed_of field if (!($val = $o->epGet($cof->getName()))) { continue; } // is the field value an array? if so, delete recursively if (!is_array($val) && !$val instanceof epArray) { $status &= $this->delete($val); } else { foreach ($val as $val_) { $status &= $this->delete($val_); } } } $status &= parent::delete($o); $status &= $this->_deleteRelations($class, $oid); return $status; }
/** * Updates one-to-many relation between two classes * * Relations from $class_a stored that are not in array $oids_b are deleted and * new relations to $class_b objects in $oids_b are added. * * @param string $base_a name of base class a * @param string $class_a name of class a * @param integer $oid_a object id of the class a object * @param integer $var_a the relational field of object a * @param string $base_b name of base b * @param array $oids_b oids of the class b object related to the class a object * @return bool */ protected function _updateRelations($base_a, $class_a, $oid_a, $var_a, $base_b, $oids_b = array()) { // need to have a non-empty name if (!$base_a || !$class_a || !$oid_a || !$var_a || !$base_b) { throw new epExceptionManager('Incorrect parameters to update relations'); return false; } // get all relation objects for ($class_a, $a_oid, $class_b) if (!($o =& $this->_relationExample($class_a, $oid_a, $var_a, $base_b))) { throw new epExceptionManager('Cannot create relation object'); return false; } // switch relations table $this->_setRelationTable($base_a, $base_b); // return status $status = true; // array to keep all stored oids_b $stored_oids_b = array(); // find by example $o (find from db only, false no cache) if ($rs = parent::find($o, EP_GET_FROM_DB, false)) { // go through each relation found foreach ($rs as $r) { // encode class_b and oid_b $oid_b = $this->_encodeClassOid($r->get_class_b(), $r->get_oid_b()); // collect stored oids $stored_oids_b[] = $oid_b; // delete oid_b not in oids_b if (!$oids_b || !in_array($oid_b, $oids_b)) { $status &= parent::delete($r); } } } // done if oids_b is empty if (!$oids_b) { return $status; } // insert new relations ($oid_b not in $stored_oids_b) foreach ($oids_b as $oid_b_) { // weed out empty oid_b_ if (!$oid_b_) { continue; } // already stored? skip if (in_array($oid_b_, $stored_oids_b)) { continue; } // decode oid_b_ if (!$this->decodeClassOid($oid_b_, $class_b, $oid_b)) { // skip if fail (bad encoded oid) continue; } // make relation if (!($r =& parent::_create('epObjectRelation', false, false, array($class_a, $oid_a, $var_a, $base_b, $class_b, $oid_b)))) { throw new epExceptionManager('Cannot create object relation object'); $status = false; continue; } // store relation $status &= parent::commit($r); } return $status; }