yiisoft / yiisoft/active-record
Laravel's sync() analog in Yii
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 119
- Forks
- 38
- Avg merge
- 1h 11m
- Merged PRs (30d)
- 1
Description
I have 2 AR models: User and Role connected via junction table. I need to ensure that only certain Role models attached to User model. IDs of roles are given.
In Laravel I can write:
$ids = [1, 2, 3];
$user->roles()->sync($ids);
Now, in Yii I do the same thing this way:
$ids = [1, 2, 3];
$user->unlinkAll('role', true);
$roles = Role::find()->where(['id' => $ids)->all();
foreach ($roles as $role) {
$user->link('role', $role);
}
Is it possible to sync links between models without unnecessary creation of Role entities and deleting all previous connections?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the Active Record relationship APIs represented by unlinkAll(), link(), and the User–Role junction relation. Define how synchronization should preserve unchanged links, remove obsolete ones, and attach the requested role IDs without creating unnecessary Role entities; the issue is done when those behaviors are covered by the project's tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100