Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function delete(Selection $selection)
 {
     $data = $this->getData();
     $data = self::sortAll($data, $selection->getOrdering());
     $limit = $selection->getLimit();
     $predicate = $selection->getPredicate();
     $count = 0;
     foreach ($data as $key => $record) {
         if ($predicate($record)) {
             $this->deleteKey($key);
             $count++;
             if (isset($limit) and $count >= $limit) {
                 break;
             }
         }
     }
     return $count;
 }
Exemple #2
0
 /**
  * {@inheritdoc}
  */
 public function deleteSelection(Selection $selection)
 {
     $sqlString = 'DELETE FROM ' . $this->owner->quoteModel($this->name);
     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);
 }
Exemple #3
0
 /**
  * Set association.
  *
  * @param ActiveRecord $record
  *            A record.
  * @param array $association
  *            Association options.
  * @param ActiveRecord|Selection|ActiveRecord[] $value
  *            New value.
  * @throws InvalidAssociationException If association type unknown.
  */
 public function setAssociation(ActiveRecord $record, $association, $value)
 {
     switch ($association['type']) {
         case 'belongsTo':
             if (!isset($value)) {
                 $this->unsetAssociation($record, $association);
                 return;
             }
             Assume::that($value instanceof ActiveRecord);
             Assume::that($value->getModel() == $association['model']);
             $key = $association['otherKey'];
             $otherId = $association['model']->primaryKey;
             $record->{$key} = $value->{$otherId};
             return;
         case 'hasOne':
             Assume::that($value instanceof ActiveRecord);
             Assume::that($value->getModel() == $association['model']);
             $this->unsetAssociation($record, $association);
             $key = $association['thisKey'];
             $id = $this->primaryKey;
             $value->{$key} = $record->{$id};
             $value->save();
             return;
         case 'hasMany':
             $key = $association['thisKey'];
             $id = $this->primaryKey;
             $idValue = $record->{$id};
             if ($value instanceof Selection) {
                 $value->set($key, $idValue)->update();
                 return;
             }
             if (!is_array($value)) {
                 $value = array($value);
             }
             $this->unsetAssociation($record, $association);
             foreach ($value as $item) {
                 Assume::that($item instanceof ActiveRecord);
                 Assume::that($item->getModel() == $association['model']);
                 $item->{$key} = $idValue;
                 if (!$item->isNew()) {
                     $item->save();
                 }
             }
             return;
         case 'hasAndBelongsToMany':
             return;
     }
     throw new InvalidAssociationException('Unknown association type: ' . $association['type']);
 }