mootensai/yii2-relation-trait

Yii 2 模型加载关系,事务保存时使用关系

安装量: 179,249

依赖关系: 10

推荐者: 0

安全性: 0

星标: 47

关注者: 10

分支: 45

开放问题: 19

类型:yii2-extension

1.1.8 2017-10-18 04:48 UTC

README

Yii 2 模型增加加载关系(loadAll($POST))和事务保存关系(saveAll())的功能

PLUS 软删除/恢复功能!

最佳搭配 mootensai/yii2-enhanced-gii

Latest Stable Version License Total Downloads Monthly Downloads Daily Downloads Join the chat at https://gitter.im/mootensai/yii2-relation-trait

支持

Support via Gratipay

https://www.paypal.me/yohanesc

在 LinkedIn 上支持我

https://www.linkedin.com/in/yohanes-candrajaya-b68394102/

安装

安装此扩展的首选方式是通过 composer

运行以下命令:

$ composer require 'mootensai/yii2-relation-trait:dev-master'

或者将以下内容添加到您的 composer.json 文件的 require 部分:

"mootensai/yii2-relation-trait": "*"

在模型中使用

class MyModel extends ActiveRecord{
    use \mootensai\relation\RelationTrait;
}

数组输入及在控制器中使用

它接受一个普通的 POST 数组。以下是一个示例

Array (
    $_POST['ParentClass'] => Array 
        (
            [attr1] => value1
            [attr2] => value2 
            // has many
            [relationName] => Array 
                ( 
                    [0] => Array 
                        (
                            [relAttr] => relValue1
                        )
                    [1] => Array 
                        (
                            [relAttr] => relValue1
                        )
                )
            // has one
            [relationName] => Array
                ( 
                    [relAttr1] => relValue1
                    [relAttr2] => relValue2
                )
        )
)

OR

Array (
    $_POST['ParentClass'] => ['attr1' => 'value1','attr2' => 'value2'],
    // Has One
    $_POST['RelatedClass'] => ['relAttr1' => 'value1','relAttr2' => 'value2'], 
    // Has Many
    $_POST['RelatedClass'] => Array
        (
            [0] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
            [1] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
        )      
)
// sample at controller
if($model->loadAll(Yii:$app->request->post()) && $model->saveAll()){
    return $this->redirect(['view', 'id' => $model->id, 'created' => $model->created]);
}

特性

数组输出

// I use this to send model & related through JSON / Serialize
print_r($model->getAttributesWithRelatedAsPost());
Array
(
    [MainClass] => Array
        (
            [attr1] => value1
            [attr2] => value2
        )

    [RelatedClass] => Array
        (
            [0] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
        )

)
print_r($model->getAttributesWithRelated());
Array
(
    [attr1] => value1
    [attr2] => value2
    [relationName] => Array
        (
            [0] => Array
                (
                    [attr1] => value1
                    [attr2] => value2
                )
        )
)

使用事务

这样,您的数据将是原子的(参见: http://en.wikipedia.org/wiki/ACID

使用常规保存

这样,您的行为仍然有效

在主模型中添加验证

$form->errorSummary($model);

将得到

<<Related Class Name>> #<<index + 1>> : <<error message>>
My Related Model #1 : Attribute is required

它适用于自增主键或非自增主键(我已尝试使用 UUID)

如果您想使用我的行为,请查看这里

https://github.com/mootensai/yii2-uuid-behavior

软删除

将此行添加到您的模型中,以启用软删除

private $_rt_softdelete;

function __construct(){
    $this->_rt_softdelete = [
        '<column>' => <undeleted row marker value>
        // multiple row marker column example
        'isdeleted' => 1,
        'deleted_by' => \Yii::$app->user->id,
        'deleted_at' => date('Y-m-d H:i:s')
    ];
}

将此行添加到您的模型中,以启用软恢复

private $_rt_softrestore;

function __construct(){
    $this->_rt_softrestore = [
        '<column>' => <undeleted row marker value>
        // multiple row marker column example
        'isdeleted' => 0,
        'deleted_by' => 0,
        'deleted_at' => 'NULL'
    ];
}

应在 Yii 支持的数据库上运行

它使用 Yii 的所有 Active Query 或 Active Record 来执行数据库命令

我欢迎任何改进

如果您遇到问题或对增强有想法,请创建问题

~ SDG ~