Skip to content

Commit

Permalink
Reload object from data returned by save() response
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Elias committed Nov 3, 2014
1 parent 425fa44 commit 787f4fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 45 deletions.
62 changes: 23 additions & 39 deletions lib/secucard/client/base/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions lib/secucard/client/base/MainModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 787f4fa

Please sign in to comment.