getPrimaryKey() public method

Returns the primary key value(s).
public getPrimaryKey ( boolean $asArray = false ) : mixed
$asArray boolean whether to return the primary key value as an array. If true, the return value will be an array with attribute names as keys and attribute values as values. Note that for composite primary keys, an array will always be returned regardless of this parameter value.
return mixed the primary key value. An array (attribute name => attribute value) is returned if the primary key is composite or `$asArray` is true. A string is returned otherwise (`null` will be returned if the key value is `null`).
示例#1
0
 /**
  * Get variation models for the main one in batches.
  * @param Model|ActiveRecordInterface $model main model instance.
  * @return array list of variation models in format: behaviorName => Model[]
  */
 protected function getVariationModelBatches($model)
 {
     $key = serialize($model->getPrimaryKey());
     if (!isset($this->_variationModelBatches[$key])) {
         $this->_variationModelBatches[$key] = $this->findVariationModelBatches($model);
     }
     return $this->_variationModelBatches[$key];
 }
示例#2
0
 /**
  * Get role models for the main one.
  * @param Model|ActiveRecordInterface $model main model instance.
  * @return Model[] list of role models
  */
 public function getRoleModels($model)
 {
     $key = serialize($model->getPrimaryKey());
     if (!isset($this->_roleModels[$key])) {
         $this->_roleModels[$key] = $this->findRoleModels($model);
     }
     return $this->_roleModels[$key];
 }
示例#3
0
 /**
  * Destroys the relationship between two models.
  *
  * The model with the foreign key of the relationship will be deleted if `$delete` is true.
  * Otherwise, the foreign key will be set null and the model will be saved without validation.
  *
  * @param string $name the case sensitive name of the relationship.
  * @param ActiveRecordInterface $model the model to be unlinked from the current one.
  * You have to make sure that the model is really related with the current model as this method
  * does not check this.
  * @param boolean $delete whether to delete the model that contains the foreign key.
  * If false, the model's foreign key will be set null and saved.
  * If true, the model containing the foreign key will be deleted.
  * @throws InvalidCallException if the models cannot be unlinked
  */
 public function unlink($name, $model, $delete = false)
 {
     $relation = $this->getRelation($name);
     if ($relation->via !== null) {
         if (is_array($relation->via)) {
             /* @var $viaRelation ActiveQuery */
             list($viaName, $viaRelation) = $relation->via;
             $viaClass = $viaRelation->modelClass;
             unset($this->_related[$viaName]);
         } else {
             $viaRelation = $relation->via;
             $viaTable = reset($relation->via->from);
         }
         $columns = [];
         foreach ($viaRelation->link as $a => $b) {
             $columns[$a] = $this->{$b};
         }
         foreach ($relation->link as $a => $b) {
             $columns[$b] = $model->{$a};
         }
         $nulls = [];
         foreach (array_keys($columns) as $a) {
             $nulls[$a] = null;
         }
         if (is_array($relation->via)) {
             /* @var $viaClass ActiveRecordInterface */
             if ($delete) {
                 $viaClass::deleteAll($columns);
             } else {
                 $viaClass::updateAll($nulls, $columns);
             }
         } else {
             /* @var $viaTable string */
             /* @var $command Command */
             $command = static::getDb()->createCommand();
             if ($delete) {
                 $command->delete($viaTable, $columns)->execute();
             } else {
                 $command->update($viaTable, $nulls, $columns)->execute();
             }
         }
     } else {
         $p1 = $model->isPrimaryKey(array_keys($relation->link));
         $p2 = $this->isPrimaryKey(array_values($relation->link));
         if ($p2) {
             foreach ($relation->link as $a => $b) {
                 $model->{$a} = null;
             }
             $delete ? $model->delete() : $model->save(false);
         } elseif ($p1) {
             foreach ($relation->link as $a => $b) {
                 if (is_array($this->{$b})) {
                     // relation via array valued attribute
                     if (($key = array_search($model->{$a}, $this->{$b}, false)) !== false) {
                         $values = $this->{$b};
                         unset($values[$key]);
                         $this->{$b} = array_values($values);
                     }
                 } else {
                     $this->{$b} = null;
                 }
             }
             $delete ? $this->delete() : $this->save(false);
         } else {
             throw new InvalidCallException('Unable to unlink models: the link does not involve any primary key.');
         }
     }
     if (!$relation->multiple) {
         unset($this->_related[$name]);
     } elseif (isset($this->_related[$name])) {
         /* @var $b ActiveRecordInterface */
         foreach ($this->_related[$name] as $a => $b) {
             if ($model->getPrimaryKey() == $b->getPrimaryKey()) {
                 unset($this->_related[$name][$a]);
             }
         }
     }
 }
 /**
  * Get condition to exclude current model by it's primay key.
  *
  * If model is new, empty array will be returned.
  *
  * @param  ActiveRecordInterface $model
  * @return array
  */
 protected function getExcludeByPkCondition(ActiveRecordInterface $model)
 {
     if (array_filter($pk = $model->getPrimaryKey(true))) {
         $condition = ['not', $pk];
     } else {
         $condition = [];
     }
     return $condition;
 }
示例#5
0
 /**
  * @param ActiveRecordInterface|Model $master
  */
 protected function syncRecord(ActiveRecordInterface $master)
 {
     /**@var ActiveRecordInterface|Model $slave * */
     $slave = null;
     if (!$master->getIsNewRecord()) {
         $slave = $this->findSlaveByPk($master->getPrimaryKey());
     }
     if ($master->getIsNewRecord() or !$slave) {
         //if related slave not exists - create new slave too
         $slave = $this->createNewSlave();
     }
     $hasArsync = $slave->getBehavior('ArSyncBehavior');
     if ($hasArsync) {
         $slave->detachBehavior('ArSyncBehavior');
         //prevent conflict if sync with two-way binding
     }
     if ($this->slaveScenario) {
         $slave->setScenario($this->slaveScenario);
     }
     $slave = $this->populateSlave($master, $slave, $this->fieldMap);
     $this->saveSlave($slave);
     if ($hasArsync) {
         $slave->attachBehavior('ArSyncBehavior', $hasArsync);
     }
 }