Example #1
0
 /**
  * Format the fields values structure depending on the type of query.
  *
  * @param \Titon\Db\Query $query
  * @return string
  */
 public function formatValues(Query $query)
 {
     $fields = $query->getData();
     switch ($query->getType()) {
         case Query::INSERT:
             return sprintf($this->getClause(self::GROUP), implode(', ', array_fill(0, count($fields), '?')));
             break;
         case Query::MULTI_INSERT:
             $value = sprintf($this->getClause(self::GROUP), implode(', ', array_fill(0, count($fields[0]), '?')));
             return implode(', ', array_fill(0, count($fields), $value));
             break;
     }
     return '';
 }
Example #2
0
 /**
  * Primary method that handles the processing of update queries.
  *
  * Before a save is executed, a `preSave` and `preUpdate` event will be triggered.
  * This event allows data to be modified before saving via references.
  * If this event returns a falsey value, the save will exit early and
  * return a 0. This allows behaviors and events to cease save operations.
  *
  * Before the driver is queried, the connection context will be set to `write`.
  *
  * After the query has executed, and no rows have been affected, the method
  * will exit early with a 0 response. Otherwise, a `postSave` and `postUpdate` event will be triggered.
  *
  * @param \Titon\Db\Query $query
  * @param mixed $options {
  *      @type bool $before  Will trigger before callbacks
  *      @type bool $after   Will trigger after callbacks
  * }
  * @return int
  *      - The count of records updated
  *      - 0 if save operation failed
  */
 protected function _processUpdate(Query $query, array $options = [])
 {
     $data = $query->getData();
     $options = $options + ['before' => true, 'after' => true];
     // Fetch ID
     $this->id = $id = $this->findID($query);
     if ($options['before']) {
         foreach (['db.preSave', 'db.preUpdate'] as $event) {
             $event = $this->emit($event, [$query, $id, &$data]);
             if (!$event->getState()) {
                 return 0;
             }
         }
     }
     // Reset the modified data
     $query->data($data);
     // Update the connection context and execute the query
     $count = $this->getDriver()->setContext('write')->executeQuery($query)->save();
     // Exit early if save failed
     if ($count === false) {
         return false;
     }
     if ($options['after']) {
         $this->emit('db.postSave db.postUpdate', [$id, $count]);
     }
     return $count;
 }