Esempio n. 1
0
 /**
  * doUpdate
  *
  * @param $dataset
  * @param $conditions
  *
  * @return  mixed
  */
 protected function doUpdate($dataset, array $condFields)
 {
     $this->db->transactionStart(true);
     try {
         // Do create first
         $dataset = parent::doUpdate($dataset, $condFields);
         // 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;
 }