/** * add object with Model (if not already added) * @param Object $pObject * @param boolean $pThrowException throw exception if object can't be added (no complete id or object already added) * @throws \Exception */ public function addObject(Object $pObject, $pThrowException = true) { if ($pObject->hasCompleteId()) { if ($pObject->getModel()->hasIdProperty()) { $lModelName = $pObject->getModel()->getModelName(); $lId = $pObject->getId(); if (!array_key_exists($lModelName, $this->mMap)) { $this->mMap[$lModelName] = array(); } // if object NOT already added, we can add it and create attached LocalObjectCollection if (!array_key_exists($lId, $this->mMap[$lModelName])) { $this->mMap[$lModelName][$lId] = $pObject; } else { if ($pThrowException) { throw new \Exception('object already added'); } } } } else { if ($pThrowException) { trigger_error(json_encode($pObject->toObject())); trigger_error($pObject->getModel()->getModelName()); trigger_error(json_encode($pObject->getModel()->getIdproperties())); throw new \Exception('object can\'t be added, object has no id or id is incomplete'); } } }
/** * add object with localModel (only if not already added) * @param Object $pObject * @param boolean $pThrowException throw exception if an object with same id already exists * @return boolean true if object is added */ public function addObject(Object $pObject, $pThrowException = true) { if (!$pObject->getModel() instanceof LocalModel) { throw new \Exception('mdodel must be instance of LocalModel'); } $lReturn = false; $lId = $pObject->getId(); if (!$pObject->getModel()->hasIdProperty() || !$pObject->hasCompleteId()) { return $lReturn; } $pModelName = $pObject->getModel()->getModelName(); if (!array_key_exists($pModelName, $this->mMap)) { $this->mMap[$pModelName] = array(); } if (!array_key_exists($lId, $this->mMap[$pModelName])) { $this->mMap[$pModelName][$lId] = $pObject; $lReturn = true; } else { if ($pThrowException) { throw new \Exception('object already added'); } } return $lReturn; }
public function _toId(Object $pObject, $pUseSerializationName = false) { if ($pObject->hasCompleteId()) { return $pObject->getId(); } else { trigger_error("Warning cannot export id of foreign property with model '{$this->mModelName}' because object doesn't have complete id"); return null; } }
/** * @param Object $pObject * @throws \Exception */ private function _updateObject(Object $pObject) { if (!$pObject->getModel()->hasIdProperty() || !$pObject->hasCompleteId()) { throw new \Exception('update operation require complete id'); } $lModel = $pObject->getModel(); $lConditions = array(); $lUpdates = array(); $lUpdateValues = array(); $lConditionsValues = array(); $lMapOfString = $pObject->toSqlDataBase(false, self::getDatabaseConnectionTimeZone()); $lEscapedDbColumns = $pObject->getModel()->getEscapedDbColumns(); foreach ($lMapOfString as $lPropertyName => $lValue) { $lProperty = $lModel->getProperty($lPropertyName); $lColumn = array_key_exists($lProperty->getSerializationName(), $lEscapedDbColumns) ? $lEscapedDbColumns[$lProperty->getSerializationName()] : $lProperty->getSerializationName(); if ($lProperty->isId()) { $lConditions[] = "{$lColumn} = ?"; $lConditionsValues[] = $lValue; } else { $lUpdates[] = "{$lColumn} = ?"; $lUpdateValues[] = $lValue; } } $lQuery = "UPDATE " . $this->getValue("name") . " SET " . implode(", ", $lUpdates) . " WHERE " . implode(" and ", $lConditions) . ";"; return self::$sDbObjectById[$this->getValue("database")->getValue("id")]->executeSimpleQuery($lQuery, array_merge($lUpdateValues, $lConditionsValues)); }