public function synchronizeWithArray(array $array, $deep = true)
{
$refresh = false;
foreach ($array as $key => $value) {
if ($key == '_identifier') {
$refresh = true;
$this->assignIdentifier($value);
continue;
}
if ($deep && $this->getTable()->hasRelation($key)) {
if (!$this->{$key}) {
$this->refreshRelated($key);
}
if (is_array($value)) {
if (isset($value[0]) && !is_array($value[0])) {
$this->unlink($key, array(), false);
$this->link($key, $value, false);
} else {
$this->{$key}->synchronizeWithArray($value);
}
}
} else {
if ($this->getTable()->hasField($key) || array_key_exists($key, $this->_values)) {
$this->set($key, $value);
}
}
}
// Eliminate relationships missing in the $array
foreach ($this->_references as $name => $relation) {
$rel = $this->getTable()->getRelation($name);
if (!isset($array[$name]) && (!$rel->isOneToOne() || !isset($array[$rel->getLocalFieldName()]))) {
unset($this->{$name});
}
}
if ($refresh) {
$this->refresh();
}
}