From 787f4fa853718ebbfd094f1aa97d235e184109a2 Mon Sep 17 00:00:00 2001 From: Jakub Elias Date: Mon, 3 Nov 2014 15:03:06 +0100 Subject: [PATCH] Reload object from data returned by save() response --- lib/secucard/client/base/BaseModel.php | 62 ++++++++++---------------- lib/secucard/client/base/MainModel.php | 8 +--- 2 files changed, 25 insertions(+), 45 deletions(-) diff --git a/lib/secucard/client/base/BaseModel.php b/lib/secucard/client/base/BaseModel.php index fde7c38..7683862 100644 --- a/lib/secucard/client/base/BaseModel.php +++ b/lib/secucard/client/base/BaseModel.php @@ -70,45 +70,7 @@ public function initValues($values, $initialized = false) throw new \BadMethodCallException('Cannot set attributes on already initialized object'); } - foreach ($values as $attr_name => $value) { - // we need here to call the magic setter for descendant class - $this->setAttribute($attr_name, $value); - } - $this->initialized = $initialized; - - return true; - - /* Create object instance from attributes - foreach ($this->_attribute_defs as $name => $definition) { - if (isset($definition['options']['id'])) { - $this->_id_column = $name; - if (array_key_exists('id', $data)) { - $this->id = $data['id']; - } - } - - } - if ($this->_relations) { - foreach ($this->_relations as $name => $def) { - if (array_key_exists($name, $data)) { - $property = $this->_attribute_defs[$name]; - $class_name = '\\secucard\\models\\' . $def['class']; - - if ($def['type'] == self::RELATION_HAS_ONE) { - $child = new $class_name($this->client); - // TODO do we need relations backwards - //$child->add_relationship($this, $name); - $this->setAttribute($name, $child); - } elseif ($def['type'] == self::RELATION_HAS_MANY) { - - // We need to create collection of the right type - // TODO implement correctly! - //$this->setAttribute($name, $collection); - } - } - } - } - return true;*/ + return $this->_initValues($values, $initialized); } /** @@ -266,6 +228,28 @@ protected function setAttributes($attributes) return true; } + /** + * Function to initialize attributes and relations from array (obtained from API call) + * @param array $values + * @param bool $initialized + * @return bool true/false + */ + protected function _initValues($values, $initialized) + { + if (!is_array($values) || empty($values)) { + return false; + } + + foreach ($values as $attr_name => $value) { + // we need here to call the magic setter for descendant class + $this->setAttribute($attr_name, $value); + } + + $this->initialized = $initialized; + + return true; + } + /** * Function that creates attributes for saving * @return array diff --git a/lib/secucard/client/base/MainModel.php b/lib/secucard/client/base/MainModel.php index 6f74cc2..d3e86b9 100644 --- a/lib/secucard/client/base/MainModel.php +++ b/lib/secucard/client/base/MainModel.php @@ -265,14 +265,10 @@ public function save() // add new object $response = $this->client->post($path, $this->getUpdateAttributes(), array()); - // TODO get the object id from response - // following value is not correct: + $response_json = $response->json(); - $this->_attributes[$this->_id_column] = $response_json[$this->_id_column]; - // or initialize object from the response.. there can be some field values fixed - // TODO - return true; + return $this->_initValues($response_json, true); } /**