Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function isNew()
 {
     return $this->record->isNew();
 }
Exemple #2
0
 /**
  * Remove a record from collection.
  *
  * @param ActiveRecord $record
  *            A record.
  */
 public function remove(ActiveRecord $record)
 {
     if (isset($this->join)) {
         $pk = $this->otherPrimary;
         $this->join->where($this->thisKey . ' = ?', $this->recordId)->and($this->otherKey . ' = ?', $record->{$pk})->deleteSelection();
     } else {
         $key = $this->thisKey;
         $record->{$key} = null;
         $record->save();
     }
 }
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']);
 }