/** * doCreate * * @param $dataset * * @return mixed */ protected function doCreate($dataset) { $this->db->transactionStart(true); try { // Do create first $dataset = parent::doCreate($dataset); // Loop the relation mapper. foreach ($this->relations as $field => $relation) { // Loop each data. foreach ($dataset as &$data) { // If relation field exists, push the foreign key and save. if ($data->{$field}) { if (!is_array($data->{$field}) && !is_object($data->{$field})) { throw new \InvalidArgumentException(sprintf('Saving relations %s::$%s need array or object.', get_class($data), $field)); } foreach ($relation['relations'] as $left => $right) { $data->{$field}->{$right} = $data->{$left}; } $data->{$field} = $relation['table']->saveOne($this->bindData($data->{$field})); } } } } catch (\Exception $e) { $this->db->transactionRollback(true); throw $e; } $this->db->transactionCommit(true); return $dataset; }