Example #1
0
 /**
  * @param $parentEntity object
  * @param $parentField TableField
  * @return bool
  */
 private function handleOneToManyChanges($parentEntity, $parentField)
 {
     $newEntityValues = $this->table->getPropertyValue($parentEntity, $parentField);
     if ($newEntityValues == null || $newEntityValues instanceof CollectionProxy) {
         $newEntityValues = array();
     }
     $buffer = array();
     foreach ($newEntityValues as $key => $child) {
         $id = $this->daoFactory->getDaoFromEntity($child)->getIdValue($child);
         if ($id == null) {
             $id = uniqid(self::TRANSIENT_PREFIX);
         }
         $buffer[$id] = $child;
     }
     $newEntityValues = $buffer;
     $oldEntityValues = $this->table->getPropertyValue($this->getCache($parentEntity), $parentField);
     if ($oldEntityValues == null) {
         $oldEntityValues = array();
     }
     $buffer = array();
     foreach ($oldEntityValues as $key => $child) {
         unset($oldEntityValues[$key]);
         if (!$child instanceof stdClass) {
             $child = $this->getCacheValue($child);
         }
         $buffer[$child->{Dao::ID}] = $child;
     }
     $oldEntityValues = $buffer;
     $valuesAdded = array_diff_key($newEntityValues, $oldEntityValues);
     $valuesDeleted = array_diff_key($oldEntityValues, $newEntityValues);
     foreach ($valuesAdded as $value) {
         $this->persistReferencedEntity($parentEntity, $value, $parentField);
     }
     foreach ($valuesDeleted as $value) {
         $dao = $this->daoFactory->getDaoFromEntity($value->{Dao::TYPE});
         $dao->delete($value->{Dao::ID});
     }
 }