yiisoft / yiisoft/active-record

Laravel's sync() analog in Yii

Open
#21 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type:enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.