本文整理汇总了PHP中yii\db\ActiveRecord::link方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord::link方法的具体用法?PHP ActiveRecord::link怎么用?PHP ActiveRecord::link使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\ActiveRecord
的用法示例。
在下文中一共展示了ActiveRecord::link方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: afterInsert
/**
* Handles afterInsert event of the owner.
*/
public function afterInsert()
{
if ($this->_model) {
if ($this->ownerType) {
$this->_model->entity_type = $this->ownerType;
}
$this->owner->link('metaRelation', $this->_model);
}
}
示例2: saveFilesToRelation
/**
* @param array $files
*/
protected function saveFilesToRelation($files)
{
$modelClass = $this->getUploadModelClass();
foreach ($files as $file) {
$model = new $modelClass();
$model->setScenario($this->uploadModelScenario);
$model = $this->loadModel($model, $file);
if ($this->getUploadRelation()->via !== null) {
$model->save(false);
}
$this->owner->link($this->uploadRelation, $model);
}
}
示例3: 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);
}
}
}
}
示例4: linkAll
public static function linkAll(ActiveRecord $model, array $models, $relationName)
{
try {
foreach ($models as $item) {
if ($item->isNewRecord) {
$model->link($relationName, $item);
} else {
$item->save();
}
}
return true;
} catch (Exception $e) {
return false;
}
}
示例5: saveRelationModel
/**
* Saves a single model within a relation.
* via: item is saved, then linked
* direct link: item is saved by linking
* existing models:
* @param ActiveRecord $model base model
* @param string $relationName name of the relation
* @param boolean $isVia relation via table
* @param ActiveRecord $item the model in the relation
* @param array $config configuration array
* @return boolean save success
* @internal
*/
private function saveRelationModel(&$model, $relationName, $isVia, &$item, &$config)
{
$result = true;
$skipUpdate = ArrayHelper::getValue($config, self::SKIP_UPDATE, $this->defaultSkipUpdate);
if ($isVia) {
if ($item->isNewRecord) {
$result = $result && $item->save();
$model->link($relationName, $item);
} else {
if (!$skipUpdate) {
$result = $result && $item->save();
}
$findInRelation = $model->getRelation($relationName)->andWhere($item->getPrimaryKey(true))->exists();
if (!$findInRelation) {
$model->link($relationName, $item);
}
}
} else {
if ($item->isNewRecord) {
$model->link($relationName, $item);
} else {
if (!$skipUpdate) {
// save linked item only, if something changed
if (count($item->getDirtyAttributes()) > 0) {
$item->save();
}
}
}
}
$result = $result && $this->saveRelations($item, $config);
return $result;
}