public function sync($name, array $ids, $delete = true)
    {
        /** @var \yii\db\ActiveRecordInterface $relationClass */
        $relationClass = $this->owner->getRelation($name)->modelClass;

        $current = $this->getLinkedIds($name);
        $unlink = array_diff($current, $ids);
        $link = array_diff($ids, $current);

        $models = !empty($unlink) ? $relationClass::findAll($unlink) : [];
        foreach ($models as $model) {
            $this->owner->unlink($name, $model, $delete);
        }

        $models = !empty($link) ? $relationClass::findAll($link) : [];
        foreach ($models as $model) {
            $this->owner->link($name, $model);
        }

        $this->afterSync($name);

        return ['linked' => $link, 'unlinked' => $unlink];
    }
public function sync($name, array $ids, $delete = true)
    {
        /** @var \yii\db\ActiveRecordInterface $relationClass */
        $relationClass = $this->owner->getRelation($name)->modelClass;

        $current = $this->getLinkedIds($name);
        $unlink = array_diff($current, $ids);
        $link = array_diff($ids, $current);

        $models = !empty($unlink) ? $relationClass::findAll($unlink) : [];
        foreach ($models as $model) {
            $this->owner->unlink($name, $model, $delete);
        }

        $models = !empty($link) ? $relationClass::findAll($link) : [];
        foreach ($models as $model) {
            $this->owner->link($name, $model);
        }

        $this->afterSync($name);

        return ['linked' => $link, 'unlinked' => $unlink];
    }
public function sync ( $ name , array $ ids , $ delete = true ) { /** @var \yii\db\ActiveRecordInterface $relationClass */ $ relationClass = $ this -> owner -> getRelation ( $ name ) -> modelClass ; $ current = $ this -> getLinkedIds ( $ name ) ; $ unlink = array_diff ( $ current , $ ids ) ; $ link = array_diff ( $ ids , $ current ) ; $ models = ! empty ( $ unlink ) ? $ relationClass :: findAll ( $ unlink ) : [ ] ; foreach ( $ models as $ model ) { $ this -> owner -> unlink ( $ name , $ model , $ delete ) ; } $ models = ! empty ( $ link ) ? $ relationClass :: findAll ( $ link ) : [ ] ; foreach ( $ models as $ model ) { $ this -> owner -> link ( $ name , $ model ) ; } $ this -> afterSync ( $ name ) ; return [ 'linked' => $ link , 'unlinked' => $ unlink ] ; }
Sync the relationship with a list of IDs

@param string $name
@param array  $ids
@param bool   $delete whether to delete the model that contains the foreign key

@return array changes
Sync the relationship with a list of IDs