primaryKey() public static method

The default implementation will return the primary key(s) as declared in the DB table that is associated with this AR class. If the DB table does not declare any primary key, you should override this method to return the attributes that you want to use as primary keys for this AR class. Note that an array should be returned even for a table with single primary key.
public static primaryKey ( ) : string[]
return string[] the primary keys of the associated database table.
Beispiel #1
0
 protected function loadExisting()
 {
     $pk = [];
     foreach ($this->_attributes as $attribute) {
         if (in_array($attribute->standardAttribute->name, $this->_instance->primaryKey())) {
             $pk[$attribute->standardAttribute->name] = $attribute->value;
         }
     }
     if (!$pk) {
         return;
     }
     if (count($pk) == 1 && !reset($pk)) {
         return;
     }
     foreach ($pk as $value) {
         if (!$value) {
             throw new RowException($this->row, 'For updated model all primary key attributes must be specified.');
         }
     }
     /* @var $modelClass \yii\db\ActiveRecord */
     $modelClass = $this->_standardModel->className;
     $model = $modelClass::findOne($pk);
     if (!$model) {
         throw new RowException($this->row, 'Model for update not found.');
     }
     $this->_instance = $model;
 }
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     if (!$owner instanceof ActiveRecord) {
         throw new InvalidConfigException('Owner of this behavior must be extending ActiveRecord model.');
     }
     parent::attach($owner);
     $this->ownerPrimaryKey = $this->owner->primaryKey()[0];
     if (!isset($this->translationModelName, $this->translationOwnerField, $this->languageField)) {
         throw new InvalidConfigException('Please specify contentModelName and contentOwnerKey in the ' . get_class($this->owner) . ' configuration');
     }
     $this->owner->getValidators();
 }
 /**
  * @param integer $type
  * @param \yii\db\ActiveRecord $object
  */
 public function run($type, $object)
 {
     $pkey = $object->primaryKey();
     $pkey = $pkey[0];
     $data = ['table' => $object->tableName(true), 'model_id' => $object->getPrimaryKey(), 'type' => $type, 'date' => date('Y-m-d H:i:s', time())];
     switch ($type) {
         case self::EVT_INSERT:
             $data['field_name'] = $pkey;
             $this->saveField($data);
             break;
         case self::EVT_UPDATE:
             foreach ($this->updatedFields as $updatedFieldKey => $updatedFieldValue) {
                 $data['field_name'] = $updatedFieldKey;
                 $data['old_value'] = $updatedFieldValue;
                 $data['new_value'] = $object->{$updatedFieldKey};
                 $this->saveField($data);
             }
             break;
         case self::EVT_DELETE:
             $data['field_name'] = $pkey;
             $this->saveField($data);
             break;
         case self::EVT_UPDATE_PK:
             $data['field_name'] = $pkey;
             $data['old_value'] = $object->getOldPrimaryKey();
             $data['new_value'] = $object->{$pkey};
             $this->saveField($data);
             break;
     }
 }
 public static function primaryKey()
 {
     $class = get_called_class();
     if (!isset(self::$primaryKeyCache[$class])) {
         self::$primaryKeyCache[$class] = parent::primaryKey();
     }
     return self::$primaryKeyCache[$class];
 }
Beispiel #5
0
 /**
  * Returns the cache key.
  * @return mixed the cache key
  */
 protected function getCacheKey()
 {
     $owner = 0;
     if ($this->owner !== null) {
         $pks = $this->owner->primaryKey();
         $owner = $this->owner->{$pks}[0];
     }
     return [__CLASS__, $this->code, $this->layout, $owner];
 }
Beispiel #6
0
 /**
  * @inheritdoc
  * @param ActiveRecord $owner
  */
 public function attach($owner)
 {
     $this->attribute = (array) $this->attribute;
     $primaryKey = $owner->primaryKey();
     $primaryKey = is_array($primaryKey) ? array_shift($primaryKey) : $primaryKey;
     if (in_array($primaryKey, $this->attribute, true) && $owner->getIsNewRecord()) {
         $this->attributes[ActiveRecord::EVENT_AFTER_INSERT] = $this->slugAttribute;
     }
     parent::attach($owner);
 }
 /**
  * 
  * @param ActiveRecord $model
  * @param type $key
  * @param type $index
  * @return type
  */
 protected function renderDataCellContent($model, $key, $index)
 {
     $result = Html::tag('span', $index + 1, ['class' => 'serial-column']);
     if ($model instanceof ActiveRecord && ($primaryKeys = $model->primaryKey()) != []) {
         foreach ($primaryKeys as $primary) {
             $result .= ' ' . Html::activeHiddenInput($model, "[{$index}]{$primary}");
         }
     }
     return $result;
 }
 /**
  * @param ActiveRecord $owner
  * @throws Exception
  */
 public function attach($owner)
 {
     if ($this->itemAttribute === null) {
         $primaryKey = $owner->primaryKey();
         if (!isset($primaryKey[0])) {
             throw new Exception('"' . $owner->className() . '" must have a primary key.');
         }
         $this->itemAttribute = $primaryKey[0];
         $this->primaryKeyMode = true;
     }
     return parent::attach($owner);
 }
 /**
  * Returns a models primary-key in json format. This works also with
  * composite primary-keys
  *
  * @param \yii\db\ActiveRecord $model the model instance
  * @return string the models pk in json-format
  * @throws \yii\base\InvalidParamException if the model is not of type ActiveRecord
  * @throws \yii\base\InvalidConfigException if the models pk is empty or invalid
  */
 public static function asJson($model)
 {
     //check if the model is valid
     if (!$model instanceof \yii\db\ActiveRecord) {
         throw new InvalidParamException(Yii::t('app', 'The model must be of type ActiveRecord'));
     }
     //fetch the models pk
     $pk = $model->primaryKey();
     //assert that a valid pk was received
     if ($pk === null || !is_array($pk) || count($pk) == 0) {
         $msg = Yii::t('app', 'Invalid primary key definition: please provide a pk-definition for table {table}', ['table' => $model->tableName()]);
         throw new InvalidConfigException($msg);
     }
     //create final array and return it
     $arrPk = [];
     foreach ($pk as $pkCol) {
         $arrPk[$pkCol] = $model->{$pkCol};
     }
     return Json::encode($arrPk);
 }
Beispiel #10
0
 public function getSeo()
 {
     return $this->owner->hasOne(SeoText::className(), ['item_id' => $this->owner->primaryKey()[0]])->where(['class' => $this->owner->className()]);
 }
 /**
  * Check if model was deleted (not found in new models).
  *
  * @param ActiveRecord $model
  * @param $attribute
  *
  * @return bool
  */
 protected function isDeletedModel($model, $attribute)
 {
     $modelAttributes = $model->attributes;
     unset($modelAttributes[$model->primaryKey()[0]]);
     foreach ($this->relationalData[$attribute]['newModels'] as $newModel) {
         /** @var ActiveRecord $newModel */
         $newModelAttributes = $newModel->attributes;
         unset($newModelAttributes[$newModel->primaryKey()[0]]);
         if ($newModelAttributes == $modelAttributes) {
             return false;
         }
     }
     return true;
 }
Beispiel #12
0
 /**
  * Retrieves primary key name
  */
 private function _getPrimaryKeyName(\yii\db\ActiveRecord $model, $composite = false)
 {
     $primaryKey = $model->primaryKey();
     if (!$composite && count($primaryKey) > 1) {
         throw new Exception('Model has composit key which is not allowed. Relations cannot be established');
     }
     return array_shift($primaryKey);
 }