{"id":16314890,"url":"https://github.com/frostealth/yii2-relation-behavior","last_synced_at":"2025-10-25T17:30:19.487Z","repository":{"id":62507630,"uuid":"45710566","full_name":"frostealth/yii2-relation-behavior","owner":"frostealth","description":"Easy linking and sync relationships many-to-many and one-to-many","archived":false,"fork":false,"pushed_at":"2016-04-12T11:07:43.000Z","size":9,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-31T09:22:16.953Z","etag":null,"topics":["sync-relationships","yii2","yii2-behaviors"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frostealth.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-06T22:08:41.000Z","updated_at":"2018-03-26T03:08:45.000Z","dependencies_parsed_at":"2022-11-02T13:16:19.761Z","dependency_job_id":null,"html_url":"https://github.com/frostealth/yii2-relation-behavior","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostealth%2Fyii2-relation-behavior","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostealth%2Fyii2-relation-behavior/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostealth%2Fyii2-relation-behavior/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frostealth%2Fyii2-relation-behavior/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frostealth","download_url":"https://codeload.github.com/frostealth/yii2-relation-behavior/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238183490,"owners_count":19430126,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["sync-relationships","yii2","yii2-behaviors"],"created_at":"2024-10-10T21:55:32.587Z","updated_at":"2025-10-25T17:30:19.188Z","avatar_url":"https://github.com/frostealth.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Yii2 Relation Behavior\n\nEasy linking and sync relationships many-to-many.\n\n## Installation\n\nRun the [Composer](http://getcomposer.org/download/) command to install the latest stable version:\n\n```\ncomposer require frostealth/yii2-relation-behavior @stable\n```\n\n## Behaviors\n\n* `SyncRelationBehavior`\n* `EasyRelationBehavior`\n\n## Usage\n\n### SyncRelationBehavior\n\nAttach the behavior to your model:\n\n```php\npublic function behaviors()\n{\n    return [\n        SyncRelationBehavior::className(),\n    ];\n}\n```\n\nUse the `sync` method to construct many-to-many associations. \nThe `sync` method accepts an array of IDs.\n\n```php\n$model-\u003esync('categories', [2, 5, 9]);\n```\n\n### EasyRelationBehavior\n\nThe `EasyRelationBehavior` extends the `SyncRelationBehavior`.\n\nAttach the behavior to your model:\n\n```php\npublic function behaviors()\n{\n    return [\n        [\n            'class' =\u003e EasyRelationBehavior::className(),\n            'relations' =\u003e ['categories'],\n            'suffix' =\u003e 'ids', // by default\n        ],\n    ];\n}\n\npublic function rules()\n{\n    return [\n        ['categoriesIds', 'each', 'rule' =\u003e ['integer', 'integerOnly' =\u003e true]],\n    ];\n}\n```\n\nJust add your `$suffix` to the relation name and you will get associated ids:\n\n```php\n$categoriesIds = $model-\u003ecategoriesIds; // [1, 3, 4]\n\n// linking\n$categoriesIds = [2, 3, 5];\n$model-\u003ecategoriesIds = $categoriesIds;\n$model-\u003esave();\n```\n\nAdd control to view for managing related list.\n\nWithout extensions it can be done with multiple select:\n\n```php\n\u003c?php $categories = ArrayHelper::map(Category::find()-\u003eall(), 'id', 'name') ?\u003e\n\u003c?= $form-\u003efield($model, 'categoriesIds')-\u003edropDownList($categories, ['multiple' =\u003e true]) ?\u003e\n```\n\n## License\n\nThe MIT License (MIT).\nSee [LICENSE.md](https://github.com/frostealth/yii2-relation-behavior/blob/master/LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrostealth%2Fyii2-relation-behavior","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrostealth%2Fyii2-relation-behavior","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrostealth%2Fyii2-relation-behavior/lists"}