本文整理汇总了PHP中yii\db\ActiveRecord::primaryKey方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord::primaryKey方法的具体用法?PHP ActiveRecord::primaryKey怎么用?PHP ActiveRecord::primaryKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\ActiveRecord
的用法示例。
在下文中一共展示了ActiveRecord::primaryKey方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: loadExisting
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;
}
示例2: attach
/**
* @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();
}
示例3: run
/**
* @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;
}
}
示例4: primaryKey
public static function primaryKey()
{
$class = get_called_class();
if (!isset(self::$primaryKeyCache[$class])) {
self::$primaryKeyCache[$class] = parent::primaryKey();
}
return self::$primaryKeyCache[$class];
}
示例5: getCacheKey
/**
* 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];
}
示例6: attach
/**
* @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);
}
示例7: renderDataCellContent
/**
*
* @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;
}
示例8: attach
/**
* @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);
}
示例9: asJson
/**
* 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);
}
示例10: getSeo
public function getSeo()
{
return $this->owner->hasOne(SeoText::className(), ['item_id' => $this->owner->primaryKey()[0]])->where(['class' => $this->owner->className()]);
}
示例11: isDeletedModel
/**
* 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;
}
示例12: _getPrimaryKeyName
/**
* 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);
}