Esempio n. 1
0
File: Meta.php Progetto: jivoo/jivoo
 /**
  * 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;
 }
Esempio n. 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($value instanceof ActiveRecord);
             assume($value->getModel() == $association['model']);
             $key = $association['otherKey'];
             $otherId = $association['model']->primaryKey;
             $record->{$key} = $value->{$otherId};
             return;
         case 'hasOne':
             assume($value instanceof ActiveRecord);
             assume($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($item instanceof ActiveRecord);
                 assume($item->getModel() == $association['model']);
                 $item->{$key} = $idValue;
                 if (!$item->isNew()) {
                     $item->save();
                 }
             }
             return;
         case 'hasAndBelongsToMany':
             return;
     }
     throw new InvalidAssociationException(tr('Unknown association type: %1', $association['type']));
 }