https://github.com/cornernote/yii2-linkall
Behavior to handle saving multiple many to many related records in Yii2
https://github.com/cornernote/yii2-linkall
Last synced: 3 months ago
JSON representation
Behavior to handle saving multiple many to many related records in Yii2
- Host: GitHub
- URL: https://github.com/cornernote/yii2-linkall
- Owner: cornernote
- License: other
- Created: 2015-06-19T14:28:49.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2017-08-07T10:04:58.000Z (almost 8 years ago)
- Last Synced: 2025-05-01T12:47:55.566Z (3 months ago)
- Language: PHP
- Homepage:
- Size: 32.2 KB
- Stars: 25
- Watchers: 5
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Yii2 LinkAll
[](https://github.com/cornernote/yii2-linkall/tags)
[](LICENSE.md)
[](https://travis-ci.org/cornernote/yii2-linkall)
[](https://scrutinizer-ci.com/g/cornernote/yii2-linkall/code-structure)
[](https://scrutinizer-ci.com/g/cornernote/yii2-linkall)
[](https://packagist.org/packages/cornernote/yii2-linkall)Behavior to handle saving multiple many to many related records in Yii2.
## Installation
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
$ composer require cornernote/yii2-linkall "*"
```or add
```
"cornernote/yii2-linkall": "*"
```to the `require` section of your `composer.json` file.
## Usage
Post Model
```php
class Post extends ActiveRecord
{
public function behaviors()
{
return [
\cornernote\linkall\LinkAllBehavior::className(),
];
}public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
->viaTable('post_to_tag', ['post_id' => 'id']);
//->via('postToTag');
}
}
```Tag Model
```php
class Tag extends ActiveRecord
{}
```Post Controller
```php
class PostController extends Controller
{
public function actionExample()
{
$post = Post::findOne(1);
$tags = [Tag::findOne(2), Tag::findOne(3)];
$extraColumns = []; // extra columns to be saved to the many to many table
$unlink = true; // unlink tags not in the list
$delete = true; // delete unlinked tags
$post->linkAll('tags', $tags, $extraColumns, $unlink, $delete);
}
}
```