/** * @param Object_Concrete $object * @return void */ public function save(Object_Concrete $object) { // HACK: set the pimcore admin mode to false to get the inherited values from parent if this source one is empty $inheritedValues = Object_Abstract::doGetInheritedValues(); $storetable = $this->model->getDefinition()->getTableName($object->getClass(), false); $querytable = $this->model->getDefinition()->getTableName($object->getClass(), true); $this->inheritanceHelper = new Object_Concrete_Resource_InheritanceHelper($object->getClassId(), "o_id", $storetable, $querytable); $this->createDataRows($object); Object_Abstract::setGetInheritedValues(false); $fd = $this->model->getDefinition()->getFieldDefinitions(); $data = array(); $data["o_id"] = $object->getId(); $data["fieldname"] = $this->model->getFieldname(); // remove all relations try { $this->db->delete("object_relations_" . $object->getClassId(), "src_id = " . $object->getId() . " AND ownertype = 'objectbrick' AND ownername = '" . $this->model->getFieldname() . "'"); } catch (Exception $e) { Logger::warning("Error during removing old relations: " . $e); } foreach ($fd as $key => $value) { $getter = "get" . ucfirst($value->getName()); if (method_exists($fd, "save")) { // for fieldtypes which have their own save algorithm eg. objects, multihref, ... $value->save($this->model); } else { if ($value->getColumnType()) { if (is_array($value->getColumnType())) { $insertDataArray = $value->getDataForResource($this->model->{$getter}(), $object); $data = array_merge($data, $insertDataArray); } else { $insertData = $value->getDataForResource($this->model->{$getter}(), $object); $data[$key] = $insertData; } } else { if (method_exists($value, "save")) { // for fieldtypes which have their own save algorithm eg. fieldcollections $value->save($this->model); } } } } $this->db->update($storetable, $data, $this->db->quoteInto("o_id = ?", $object->getId())); // get data for query table // $tableName = $this->model->getDefinition()->getTableName($object->getClass(), true); // this is special because we have to call each getter to get the inherited values from a possible parent object Object_Abstract::setGetInheritedValues(true); $objectVars = get_object_vars($this->model); $data = array(); $data["o_id"] = $object->getId(); $data["fieldname"] = $this->model->getFieldname(); $this->inheritanceHelper->resetFieldsToCheck(); $oldData = $this->db->fetchRow("SELECT * FROM " . $querytable . " WHERE o_id = ?", $object->getId()); foreach ($objectVars as $key => $value) { $fd = $this->model->getDefinition()->getFieldDefinition($key); if ($fd) { if ($fd->getQueryColumnType()) { //exclude untouchables if value is not an array - this means data has not been loaded if (!is_array($this->model->{$key})) { $method = "get" . $key; $insertData = $fd->getDataForQueryResource($this->model->{$method}(), $object); if (is_array($insertData)) { $data = array_merge($data, $insertData); } else { $data[$key] = $insertData; } //get changed fields for inheritance if ($fd->isRelationType()) { if (is_array($insertData)) { $doInsert = false; foreach ($insertData as $insertDataKey => $insertDataValue) { if ($oldData[$insertDataKey] != $insertDataValue) { $doInsert = true; } } if ($doInsert) { $this->inheritanceHelper->addRelationToCheck($key, array_keys($insertData)); } } else { if ($oldData[$key] != $insertData) { $this->inheritanceHelper->addRelationToCheck($key); } } } else { if (is_array($insertData)) { foreach ($insertData as $insertDataKey => $insertDataValue) { if ($oldData[$insertDataKey] != $insertDataValue) { $this->inheritanceHelper->addFieldToCheck($insertDataKey); } } } else { if ($oldData[$key] != $insertData) { $this->inheritanceHelper->addFieldToCheck($key); } } } } else { Logger::debug("Excluding untouchable query value for object - objectbrick [ " . $object->getId() . " ] key [ {$key} ] because it has not been loaded"); } } } } $this->db->update($querytable, $data, "o_id = " . $object->getId()); $this->inheritanceHelper->doUpdate($object->getId()); $this->inheritanceHelper->resetFieldsToCheck(); // HACK: see a few lines above! Object_Abstract::setGetInheritedValues($inheritedValues); }
/** * Save changes to database, it's an good idea to use save() instead * * @return void */ public function update() { // check if the rows must be inserted or updated in the query and store table if ($this->model->getId()) { try { $this->insertOrUpdate = $this->db->fetchRow("SELECT\r\n object_store_" . $this->model->getO_classId() . ".oo_id as store, object_query_" . $this->model->getO_classId() . ".oo_id as query, object.o_id as object\r\n FROM\r\n (SELECT o_id FROM objects WHERE o_id = " . $this->model->getId() . ") as object LEFT JOIN\r\n object_store_" . $this->model->getO_classId() . " ON object.o_id = object_store_" . $this->model->getO_classId() . ".oo_id LEFT JOIN\r\n object_query_" . $this->model->getO_classId() . " ON object.o_id = object_query_" . $this->model->getO_classId() . ".oo_id;"); } catch (Exception $e) { $this->insertOrUpdate = null; } } if (empty($this->insertOrUpdate)) { $this->insertOrUpdate = array("query" => null, "store" => null, "object" => null); } parent::update(); //$this->createDataRows(); // get fields which shouldn't be updated $fd = $this->model->geto_class()->getFieldDefinitions(); $untouchable = array(); foreach ($fd as $key => $value) { if (method_exists($value, "getLazyLoading") && $value->getLazyLoading()) { if (!in_array($key, $this->model->getLazyLoadedFields())) { //this is a relation subject to lazy loading - it has not been loaded $untouchable[] = $key; } } } // empty relation table except the untouchable fields (eg. lazy loading fields) if (count($untouchable) > 0) { $untouchables = "'" . implode("','", $untouchable) . "'"; $this->db->delete("object_relations_" . $this->model->getO_classId(), $this->db->quoteInto("src_id = ? AND fieldname not in (" . $untouchables . ") AND ownertype = 'object'", $this->model->getO_id())); } else { $this->db->delete("object_relations_" . $this->model->getO_classId(), $this->db->quoteInto("src_id = ? AND ownertype = 'object'", $this->model->getO_id())); } $inheritedValues = Object_Abstract::doGetInheritedValues(); Object_Abstract::setGetInheritedValues(false); $data = array(); $data["oo_id"] = $this->model->getO_id(); foreach ($fd as $key => $value) { $getter = "get" . ucfirst($key); if (method_exists($value, "save")) { // for fieldtypes which have their own save algorithm eg. fieldcollections, objects, multihref, ... $value->save($this->model); } else { if ($value->getColumnType()) { // pimcore saves the values with getDataForResource if (is_array($value->getColumnType())) { $insertDataArray = $value->getDataForResource($this->model->{$getter}(), $this->model); if (is_array($insertDataArray)) { $data = array_merge($data, $insertDataArray); } } else { $insertData = $value->getDataForResource($this->model->{$getter}(), $this->model); $data[$key] = $insertData; } } } } if ($this->insertOrUpdate["store"]) { $this->db->update("object_store_" . $this->model->getO_classId(), $data, $this->db->quoteInto("oo_id = ?", $this->model->getO_id())); } else { $this->db->insert("object_store_" . $this->model->getO_classId(), $data); } // get data for query table // this is special because we have to call each getter to get the inherited values from a possible parent object Object_Abstract::setGetInheritedValues(true); $object = get_object_vars($this->model); $data = array(); $this->inheritanceHelper->resetFieldsToCheck(); $oldData = $this->db->fetchRow("SELECT * FROM object_query_" . $this->model->getO_classId() . " WHERE oo_id = ?", $this->model->getId()); foreach ($object as $key => $value) { $fd = $this->model->geto_class()->getFieldDefinition($key); if ($fd) { if ($fd->getQueryColumnType()) { //exclude untouchables if value is not an array - this means data has not been loaded if (!(in_array($key, $untouchable) and !is_array($this->model->{$key}))) { $method = "get" . $key; $insertData = $fd->getDataForQueryResource($this->model->{$method}(), $this->model); if (is_array($insertData)) { $data = array_merge($data, $insertData); } else { $data[$key] = $insertData; } //get changed fields for inheritance if ($fd->isRelationType()) { if (is_array($insertData)) { $doInsert = false; foreach ($insertData as $insertDataKey => $insertDataValue) { if ($oldData[$insertDataKey] != $insertDataValue) { $doInsert = true; } } if ($doInsert) { $this->inheritanceHelper->addRelationToCheck($key, array_keys($insertData)); } } else { if ($oldData[$key] != $insertData) { $this->inheritanceHelper->addRelationToCheck($key); } } } else { if (is_array($insertData)) { foreach ($insertData as $insertDataKey => $insertDataValue) { if ($oldData[$insertDataKey] != $insertDataValue) { $this->inheritanceHelper->addFieldToCheck($insertDataKey); } } } else { if ($oldData[$key] != $insertData) { $this->inheritanceHelper->addFieldToCheck($key); } } } } else { Logger::debug("Excluding untouchable query value for object [ " . $this->model->getId() . " ] key [ {$key} ] because it has not been loaded"); } } } } $data["oo_id"] = $this->model->getO_id(); if ($this->insertOrUpdate["query"]) { $this->db->update("object_query_" . $this->model->getO_classId(), $data, $this->db->quoteInto("oo_id = ?", $this->model->getO_id())); } else { $this->db->insert("object_query_" . $this->model->getO_classId(), $data); } Object_Abstract::setGetInheritedValues($inheritedValues); unset($this->insertOrUpdate); }