vtvz / yii2-relations
此包已被弃用且不再维护。未建议替代包。
支持简单的ActiveRecord关系
dev-master / 0.2.0.x-dev
2016-05-12 13:30 UTC
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2020-12-11 22:19:04 UTC
README
这是一个扩展,它允许轻松创建不同ActiveRecord模型之间的联系。
安装
通过 composer 安装此扩展是首选方式。
运行以下命令之一
php composer.phar require --prefer-dist vtvz/yii2-relations
或
"vtvz/yii2-relations": "*"
将以下内容添加到您的composer.json文件的require部分。
用法
vtvz\relations\Relations 类是行为。
行为支持所有类型的关联,并允许方便地对其进行配置。
示例
<?php namespace common\models; use vtvz\relations\Relations; class Order extends Model { public function behaviors() { return [ 'relations' => [ 'class' => Relations::className(), 'relations' => [ /* many to many relation type */ 'items' => [ 'model' => Item::className(), 'link' => ['id' => 'itemId'], 'viaTable' => 'order_has_item', 'viaLink' => ['orderId' => 'id'], 'inverseOf' => 'orders', ], /* one (customer) to many (orders) relation type */ 'customer' => [ 'model' => Customer::className(), 'link' => ['id' => 'customerId'], 'type' => 'one', ], /* one to one relation type with delete */ 'orderInfo' => [ 'model' => OrderInfo::className(), 'link' => ['id' => 'id'], 'type' => 'one', 'delete' => true, ], /* many (somethings) to one (order) relation type. All somethings deletes with its order */ 'somethings' => [ 'model' => Something::className(), 'link' => ['orderId' => 'id'], 'type' => 'many', ] ], ], ]; } } ?>
注意
该扩展处于早期开发阶段。需要帮助用测试覆盖扩展并创建正常文档。