本文整理汇总了PHP中yii\db\ActiveRecord::getRelation方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord::getRelation方法的具体用法?PHP ActiveRecord::getRelation怎么用?PHP ActiveRecord::getRelation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\ActiveRecord
的用法示例。
在下文中一共展示了ActiveRecord::getRelation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* @param ActiveRecord $model
* @param $relationName
*/
public function __construct(ActiveRecord $model, $relationName)
{
$this->owner = $model;
$this->relationName = $relationName;
$this->relation = $this->owner->getRelation($relationName);
/** @var ActiveQuery $via */
$via = is_array($this->relation->via) ? $this->relation->via[1] : $this->relation->via;
/** @var \yii\db\ActiveRecord $viaClass */
$this->viaTable = ($viaClass = $via->modelClass) ? $viaClass::tableName() : reset($via->from);
$this->relationAttribute = $this->relation->link;
foreach ($via->link as $viaAttribute => $ownerAttribute) {
$this->condition[$viaAttribute] = $this->owner->{$ownerAttribute};
}
}
示例2: relation
public static function relation(ActiveRecord $model, $relation_name, $options = [])
{
/* @var ActiveRecord|YcmModelUtilTrait $model */
$relation = $model->getRelation($relation_name);
$config = [$relation_name, 'widget', 'widgetClass' => Select2::className(), 'data' => RelationHelper::getSelectChoices($model, $relation_name), 'hideSearch' => false, 'options' => ['multiple' => $relation->multiple, 'placeholder' => 'Select...'], 'pluginOptions' => ['allowClear' => true]];
return ArrayHelper::merge($config, $options);
}
示例3: getRelation
/**
* @param ActiveRecord $model
* @param $relation_name
* @return null|\yii\db\ActiveQuery|\yii\db\ActiveQueryInterface
*/
public static function getRelation(ActiveRecord $model, $relation_name)
{
$relation = null;
foreach (explode('.', $relation_name) as $relation_subname) {
$relation = $model->getRelation($relation_subname);
$model = new $relation->modelClass();
}
return $relation;
}
示例4: getSelectChoices
/**
* Get available relation choices
* @param $relation_name
* @return mixed
*/
public static function getSelectChoices(ActiveRecord $model, $relation_name)
{
$class = $model->className();
if (!isset(self::$relationsChoices[$class][$relation_name])) {
self::$relationsChoices[$class][$relation_name] = [];
$relation = $model->getRelation($relation_name, false);
if ($relation) {
self::$relationsChoices[$class][$relation_name] = ModelHelper::getSelectChoices(new $relation->modelClass());
}
}
return self::$relationsChoices[$class][$relation_name];
}
示例5: afterSave
public function afterSave()
{
$data = [];
if (isset($_POST[$this->owner->formName()])) {
$data = $_POST[$this->owner->formName()];
}
if ($data) {
foreach ($data as $attribute => $value) {
if (!in_array($attribute, $this->relations)) {
continue;
}
if (is_array($value)) {
$relation = $this->owner->getRelation($attribute);
if ($relation->via !== null) {
/** @var ActiveRecord $foreignModel */
$foreignModel = $relation->modelClass;
$this->owner->unlinkAll($attribute, true);
if (is_array(current($value))) {
foreach ($value as $data) {
if (isset($data[$foreignModel::primaryKey()[0]]) && $data[$foreignModel::primaryKey()[0]] > 0) {
$fm = $foreignModel::findOne($data[$foreignModel::primaryKey()[0]]);
$fm->load($data, '');
$this->owner->link($attribute, $fm);
}
}
} else {
foreach ($value as $fk) {
if (preg_match('~^\\d+$~', $fk)) {
$this->owner->link($attribute, $foreignModel::findOne($fk));
}
}
}
}
} else {
$this->owner->unlinkAll($attribute, true);
}
}
}
}
示例6: getNewTranslation
/**
* Loads a specific translation model
*
* @param string $language the language to return
*
* @return null|\yii\db\ActiveQuery|static
*/
private function getNewTranslation($language)
{
$translation = null;
/** @var \yii\db\ActiveQuery $relation */
$relation = $this->owner->getRelation($this->relation);
/** @var ActiveRecord $class */
$class = $relation->modelClass;
//if ($translation === null) {
$translation = new $class();
$translation->{key($relation->link)} = $this->owner->getPrimaryKey();
$translation->{$this->languageAttribute} = $language;
//}
return $translation;
}
示例7: editableRelationConfig
protected static function editableRelationConfig(ActiveRecord $model, $relation_name)
{
/* @var ActiveRecord|YcmModelUtilTrait $model */
$relation = $model->getRelation($relation_name);
/* @var Module $module */
$module = \Yii::$app->getModule('ycm-utils');
/* @var ActiveRecord|YcmModelUtilTrait $relationModel */
$relationModel = \Yii::createObject($relation->modelClass);
$modelChoices = ModelHelper::getSelectChoices($relationModel);
/**
* @todo #1 implement fill ajax loading with ajax mapping
* @fixme #2 Relation is multiple, after live edit fix JS error `Cannot read property '[object Array]' of null`
*/
return ['attribute' => $relation->multiple ? $relation_name . $model->method_postfix_relation_ids : reset($relation->link), 'label' => ucfirst($relation_name), 'filter' => $modelChoices, 'value' => $relation->multiple ? function ($m) use($relation_name, $modelChoices, $model) {
return implode(', ', array_map(function ($relation_id) use($modelChoices) {
return $modelChoices[$relation_id];
}, array_values((array) $m->{$relation_name . $model->method_postfix_relation_ids})));
} : null, 'editableOptions' => ['inputType' => Editable::INPUT_SELECT2, 'size' => 'lg', 'options' => ['options' => ['multiple' => $relation->multiple], 'data' => $modelChoices, 'pluginOptions' => count($modelChoices) > $model->ajax_enable_threshold ? ['minimumInputLength' => 3, 'ajax' => ['url' => Url::to(['/ycm/model/choices', 'name' => $module->ycm->getModelName($relationModel)]), 'dataType' => 'json', 'processResults' => new JsExpression('function (results) { return results; }')]] : null], 'displayValueConfig' => !$relation->multiple ? $modelChoices : null]];
}
示例8: viaListFormat
/**
* Renders grid column for list value of via table data
* @param ActiveRecord|bool $model
* @param $attribute
* @param array $options
* @return array
* @throws \Exception
*/
public static function viaListFormat($model, $attribute, $options = [])
{
$relation = $model->getRelation($attribute);
$relationClass = $relation->modelClass;
$columns = Yii::$app->db->getTableSchema($relationClass::tableName())->columnNames;
$titles = array_intersect(['title', 'name', 'username'], $columns);
if (!($title = reset($titles))) {
throw new \Exception(Yii::t('app', 'Relation does not have any title column'));
}
return array_merge(['attribute' => $attribute, 'format' => 'raw', 'value' => !$model->isNewRecord ? implode(', ', $relation->select($title)->column()) : function ($data) use($attribute, $title) {
return implode(', ', $data->getRelation($attribute)->select($title)->column());
}, 'filter' => false], $options);
}
示例9: addRelationField
/**
* @param array $formFields
* @param \yii\db\ActiveRecord $model
* @param string $relation
* @param array $hiddenAttributes
* @param array $safeAttributes
* @param bool $multiple true for multiple values inputs, usually used for search forms
* @return array
* @throws InvalidConfigException
*/
protected static function addRelationField($formFields, $model, $relation, $hiddenAttributes, $safeAttributes, $multiple = false)
{
$activeRelation = $model->getRelation(Html::getAttributeName($relation));
if (!$activeRelation->multiple) {
// validate foreign keys only for hasOne relations
$isHidden = false;
foreach ($activeRelation->link as $left => $right) {
if (!in_array($right, $safeAttributes)) {
return $formFields;
}
if (isset($hiddenAttributes[$right])) {
$formFields[$relation] = Html::activeHiddenInput($model, $right);
unset($hiddenAttributes[$right]);
$isHidden = true;
}
}
if ($isHidden) {
return $formFields;
}
}
if (!Yii::$app->user->can($activeRelation->modelClass . '.read')) {
return $formFields;
}
if (count($activeRelation->link) > 1) {
throw new InvalidConfigException('Composite key relations are not supported by ' . get_called_class());
}
if ($activeRelation->multiple) {
if (($field = static::getHasManyRelationField($model, $relation, $activeRelation)) !== null) {
$formFields[$relation] = $field;
}
} else {
if (($field = static::getHasOneRelationField($model, $relation, $activeRelation, $multiple)) !== null) {
$formFields[$relation] = $field;
}
}
return $formFields;
}
示例10: getNameAttribute
/**
* @param ActiveRecord $model
* @param string $relationName
* @return string
* @throws \Exception
*/
protected static function getNameAttribute($model, $relationName)
{
/** @var ActiveRecord $class */
$class = $model->getRelation($relationName)->modelClass;
$attributes = Yii::$app->db->getTableSchema($class::tableName())->columnNames;
if (!($existingNames = array_intersect(['name', 'title', 'username'], $attributes))) {
throw new \Exception("Relation does not have name attribute: " . $class);
}
return reset($existingNames);
}
示例11: getUploadRelation
/**
* @return \yii\db\ActiveQuery|\yii\db\ActiveQueryInterface
*/
protected function getUploadRelation()
{
return $this->owner->getRelation($this->uploadRelation);
}
示例12: setRelation
/**
* Loads a specific relation with $data.
* incremental: existing subitems are neither removed nor unlinked.
* non-incremental: existing (loaded) subitems are unlinked and/or deleted.
*
* @param ActiveRecord $model model
* @param string $relationName the relation's name
* @param array $data data to load, including relational data
* @param array $config configuration array
* @internal
*/
private function setRelation(&$model, $relationName, &$data, &$config)
{
if (!$model->hasProperty($relationName)) {
throw new \yii\base\UnknownPropertyException(sprintf('model {%s} has no relation {%s}', $model->className(), $relationName));
}
$relation =& $config[self::RELATIONS][$relationName];
$formName = ArrayHelper::getValue($relation, self::FORMNAME, false);
$scenario = ArrayHelper::getValue($relation, self::SCENARIO, $this->defaultScenario);
$incremental = ArrayHelper::getValue($relation, self::INCREMENTAL, $this->defaultIncremental);
$delete = ArrayHelper::getValue($relation, self::DELETE, $this->defaultDelete);
$relationData = $formName == false ? $data : $data[$formName];
$rel = $model->getRelation($relationName);
$pattern = new $rel->modelClass();
$recursive = !$pattern->hasMethod('isActiveDocument');
$models = null;
$relation[self::REMOVE] = [];
$relation[self::LINK] = [];
// relation is a collection or a single component
if ($rel->multiple) {
$models = [];
if ($incremental) {
// loop through array data and load sub models
foreach ($relationData as $key => $value) {
$m = $this->loadModel($rel->modelClass, $scenario, $value, $relation, $recursive);
$models[] = $m;
}
} else {
$sort = ArrayHelper::getValue($relation, self::SORTABLE, null);
if ($sort !== null) {
$index = 0;
foreach ($relationData as $key => &$value) {
$relationData[$key][$sort] = $index++;
}
}
// loop through relation data, load data and detect removable sub models
foreach ($model->{$relationName} as $item) {
$keys = $item->getPrimaryKey(true);
// try to find subitem in data
reset($relationData);
$found = false;
foreach ($relationData as $key => &$value) {
// normalize
if (!empty($formName)) {
$value = $value[$formName];
}
$modelKeys = array_intersect_key($value, $keys);
if (count(array_diff_assoc($modelKeys, $keys)) == 0) {
$m = $this->loadExistingModel($item, $scenario, $value, $relation, $recursive);
$models[] = $m;
$found = true;
// processed, so remove from data array
unset($relationData[$key]);
break;
}
}
// we have an existing item, but it was not loaded by $data, so mark for remove.
if (!$found) {
$relation[self::REMOVE][] = $item;
}
}
// everything left in $relationData is new model data
// model might be existing, but not linked
foreach ($relationData as $key => $value) {
// normalize
if (!empty($formName)) {
$value = $value[$formName];
}
$m = $this->loadModel($rel->modelClass, $scenario, $value, $relation, $recursive);
$models[] = $m;
$relation[self::LINK][$this->serializeKey($model)][] = $m;
}
}
} else {
// relation is a single component
$oldItem = $model->{$relationName};
$models = $this->loadModel($rel->modelClass, $scenario, $value, $relation, $recursive);
if (!$incremental) {
if ($oldItem !== null) {
$keys = $oldItem->getPrimaryKey(true);
if ($models !== null) {
$modelKeys = $models->getPrimaryKey(true);
if (count(array_diff_assoc($keys, $modelKeys)) !== 0) {
$relation[self::REMOVE][] = $oldItem;
}
} else {
$relation[self::REMOVE][] = $models;
}
}
}
//.........这里部分代码省略.........
示例13: getRelation
/**
* @inheritdoc
*/
public function getRelation($name, $throwException = true)
{
if (isset($this->relations[$name])) {
return $this->relations[$name]();
}
return parent::getRelation($name, $throwException);
}
示例14: normalizeRelations
/**
* @param ActiveRecord $model
* @param array $with
* @return ActiveRelationInterface[]
*/
private function normalizeRelations($model, $with)
{
$relations = [];
foreach ($with as $name => $callback) {
if (is_integer($name)) {
$name = $callback;
$callback = null;
}
if (($pos = strpos($name, '.')) !== false) {
// with sub-relations
$childName = substr($name, $pos + 1);
$name = substr($name, 0, $pos);
} else {
$childName = null;
}
if (!isset($relations[$name])) {
$relation = $model->getRelation($name);
$relation->primaryModel = null;
$relations[$name] = $relation;
} else {
$relation = $relations[$name];
}
if (isset($childName)) {
$relation->with[$childName] = $callback;
} elseif ($callback !== null) {
call_user_func($callback, $relation);
}
}
return $relations;
}
示例15: loadRelation
/**
* Loads posted data into model relation models
* @param ActiveRecord $model
* @param $relationName
* @param $data
* @param array $requiredData
* @return array
*/
public static function loadRelation(ActiveRecord $model, $relationName, $data, $requiredData = [])
{
$relationClass = $model->getRelation($relationName)->modelClass;
return static::loadMultiple(@$data[(new $relationClass())->formName()], $relationClass, $model->{$relationName}, $requiredData);
}