dd174/yii2-related-behavior

安装次数: 7,316

依赖关系: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 1

公开问题: 2

类型:yii2-extension

v1.0.3 2016-10-19 10:34 UTC

This package is auto-updated.

Last update: 2024-09-17 17:11:39 UTC


README

更新(创建/修改/删除)模型关系

安装

安装此扩展的首选方法是使用 composer

运行以下命令

php composer require dd174/yii2-related-behavior

使用方法

在模型行为中

    public function behaviors()
    {
        return array_merge(
            parent::behaviors(),
            [
                ...
                [
                    'class' => RelatedBehavior::class,
                    'relations' => ['relatedName'],
                    // optional:
                    'scenarios' => ['relatedName' => ['create' => 'create', 'update' => 'update']],
                ],

在控制器中

$model->loadRelation('relatedName', Yii::$app->request->post(), 'keyPost');

提示

在相关模型唯一表单名称中

private $formName;

/**
 * @param null $unique необходим для получения доступа к файлам ($_FILES) в новой моделе
 */
public function setFormName($unique = null)
{
	$unique = $unique ?: ($this->primaryKey ?: uniqid('new', true));
	$this->formName = parent::formName() . '[' . $unique . ']';
}

/**
 * Составляем свое name, что бы легко использовать на одной странице форму с несколькими экземплярами этой модели
 * @return string
 */
public function formName()
{
	if (!$this->formName) {
		$this->setFormName();
	}

	return $this->formName;
}

致谢

作者:Danil DD

邮箱:dd174work@gmail.com