/**
* Saves model data to the database.
*
* @return array
*/
public function save()
{
$this->saveRelationships();
$this->appendRelationshipData();
$type = $this->exists() ? 'update' : 'create';
$doc = new Document();
$doc->set($this->to_a());
$query = new Query(array('entity' => $doc, 'model' => get_class($this), 'conditions' => array($this->primaryKey => $this->{$this->primaryKey})));
$db = static::connection();
$result = $db->{$type}($query);
$exported = $doc->export();
$this->exists(true);
$this->data[$this->primaryKey] = $exported['update'][$this->primaryKey];
return $result;
}