Пример #1
0
 /**
  * Construct meta data object.
  *
  * @param Model $model
  *            Meta data model.
  * @param string $recordKey
  *            Name of key column in meta model (e.g. 'userId').
  * @param ActiveRecord $record
  *            Record data meta data describes.
  */
 public function __construct(Model $model, $recordKey, ActiveRecord $record)
 {
     $this->model = $model;
     $this->recordKey = $recordKey;
     $id = $record->getModel()->getAiPrimaryKey();
     $this->id = $record->{$id};
     $this->record = $record;
 }
Пример #2
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']);
 }