Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function update(UpdateSelection $selection)
 {
     $data = $this->getData();
     $data = self::sortAll($data, $selection->getOrdering());
     $updates = $selection->getData();
     $limit = $selection->getLimit();
     $predicate = $selection->getPredicate();
     $count = 0;
     foreach ($data as $key => $record) {
         if ($predicate($record)) {
             $this->updateKey($key, array_merge($record, $updates));
             $count++;
             if (isset($limit) and $count >= $limit) {
                 break;
             }
         }
     }
     return $count;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function updateSelection(UpdateSelection $selection)
 {
     $typeAdapter = $this->owner->getTypeAdapter();
     $sqlString = 'UPDATE ' . $this->owner->quoteModel($this->name);
     $data = $selection->getData();
     if (count($data)) {
         $sqlString .= ' SET';
         $first = true;
         foreach ($data as $key => $value) {
             if ($first) {
                 $first = false;
             } else {
                 $sqlString .= ',';
             }
             $sqlString .= ' ' . $key . ' = ';
             if ($value instanceof \Jivoo\Data\Query\Expression) {
                 $sqlString .= $value->toString($this->owner);
             } else {
                 $sqlString .= $typeAdapter->encode($this->getType($key), $value);
             }
         }
     }
     if ($selection->getPredicate() !== null) {
         $sqlString .= ' WHERE ' . $selection->getPredicate()->toString($this->owner);
     }
     $ordering = $selection->getOrdering();
     if (count($ordering)) {
         $columns = array();
         foreach ($ordering as $orderBy) {
             $columns[] = $this->escapeQuery($orderBy[0]) . ($orderBy[1] ? ' DESC' : ' ASC');
         }
         $sqlString .= ' ORDER BY ' . implode(', ', $columns);
     }
     $limit = $selection->getLimit();
     if (isset($limit)) {
         $sqlString .= ' ' . $this->owner->sqlLimitOffset($limit);
     }
     return $this->owner->execute($sqlString);
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function updateSelection(UpdateSelection $selection)
 {
     $data = $this->getData();
     $data = self::sortAll($data, $selection->getOrdering());
     $updates = $selection->getData();
     $limit = $selection->getLimit();
     $predicate = $selection->getPredicate();
     $count = 0;
     foreach ($data as $key => $record) {
         if ($predicate === null or $predicate($record)) {
             $data = array_map(function ($value) use($record) {
                 if ($value instanceof Expression) {
                     return $value($record);
                 }
                 return $value;
             }, $updates);
             $this->updateKey($key, array_merge($record, $data));
             $count++;
             if (isset($limit) and $count >= $limit) {
                 break;
             }
         }
     }
     return $count;
 }