yii\db\ActiveRecord::primaryKey PHP Method

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.
    public static function primaryKey()
    {
        return static::getTableSchema()->primaryKey;
    }

Usage Example

Exemplo n.º 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;
 }
All Usage Examples Of yii\db\ActiveRecord::primaryKey