matthew-p / yii2-delete-relations
Yii2 delete relations behavior
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 342
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=7.1
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2023-08-19 23:43:24 UTC
README
Yii2 delete relations behavior
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist matthew-p/yii2-delete-relations "@dev"
or add
"matthew-p/yii2-delete-relations": "@dev"
to the require section of your composer.json
file.
Usage
Once the extension is installed, simply use it in your code by:
Add behavior to Active Record class:
use MP\Yii2DeleteRelations\DeleteRelationsBehavior; use yii\db\ActiveRecord; class Sample extends ActiveRecord { /** * @inheritdoc */ public function behaviors(): array { return [ [ 'class' => DeleteRelationsBehavior::class, 'relations' => ['categories', 'phone', 'orders'], // Delete all relations bulk 'method' => ['orders' => 0, 'phone' => DeleteRelationsBehavior::DELETE_BY_ONE], // orders - update relation column to 0, phone - use delete method ], ]; } /** * Get categories * * @return ActiveQuery|CategoriesQuery */ public function getCategories(): CategoriesQuery { return $this->hasMany(Category::class, ['id' => 'category_id']); } /** * Get orders * * @return ActiveQuery|OrdersQuery */ public function getOrders(): OrdersQuery { return $this->hasMany(Order::class, ['id' => 'order_id']); } /** * Get phone * * @return ActiveQuery|PhoneQuery */ public function getPhone(): PhoneQuery { return $this->hasOne(Phone::class, ['id' => 'phone_id']); } }
That's all. Check it.