PHPackages                             sakhlab/yii2-relations - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. sakhlab/yii2-relations

ActiveYii2-extension

sakhlab/yii2-relations
======================

Модуль позволяет привязывать любые модели к любым моделям (пример - похожие товары и т.д.)

021PHP

Since Apr 3Pushed 9y ago1 watchersCompare

[ Source](https://github.com/sakhlab/yii2-relations)[ Packagist](https://packagist.org/packages/sakhlab/yii2-relations)[ RSS](/packages/sakhlab-yii2-relations/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Yii2-relations
==============

[](#yii2-relations)

Модуль дает возможность быстро добавить возможность связывать модели между собой. Пример использования: привязка похожих продуктов.

Установка
---------

[](#установка)

Выполнить команду

```
php composer require pistol88/yii2-relations "*"

```

Или добавить в composer.json

```
"pistol88/yii2-relations": "*",

```

И выполнить

```
php composer update

```

Подключение и настройка
-----------------------

[](#подключение-и-настройка)

В конфигурационный файл приложения добавить модуль relations

```
    'modules' => [
        //..
        'relations' => [
            'class' => 'pistol88\relations\Module',
            'fields' => ['code'],
        ],
        //..
    ]
```

\*fields - массив доп. полей (по умолчанию в окне выбора показываются только id и название)

Использование
-------------

[](#использование)

Связи хранятся в отдельном поле (TEXT) в виде сериализованного массива, поле нужно создать и добавить в модели. К модели, которая имплементирует \\pistol88\\relations\\interfaces\\Torelate и наследует AR, подключить поведение:

```
    function behaviors()
    {
        return [
            'relations' => [
                'class' => 'pistol88\relations\behaviors\AttachRelations',
                'relatedModel' => 'common\models\Product',
                'inAttribute' => 'relations',
            ],
        ];
    }

    public function getName()
    {
        return $this->name;
    }

    public function getId()
    {
        return $this->id;
    }
```

- inAttribute - название поля модели, где будут храниться связи
- relatedModel - модель, элементы которой нужно привязывать

Теперь привязанные модели будет возвращаеть метод $model-&gt;getRelations()-&gt;all().

Виджеты
-------

[](#виджеты)

Выбор подключаемых моделей осуществляется через виджет:

 $model\]);?&gt; Его необходимо вызвать внутри формы редактирования вашей модели.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/38f4b6b7a75fc2c51891ec63b86cd686caef8a5291595a3a87ce9d4592c57c5e?d=identicon)[merdoc](/maintainers/merdoc)

---

Top Contributors

[![pistol88](https://avatars.githubusercontent.com/u/8104605?v=4)](https://github.com/pistol88 "pistol88 (17 commits)")[![yakovlev-vladimir](https://avatars.githubusercontent.com/u/689802?v=4)](https://github.com/yakovlev-vladimir "yakovlev-vladimir (1 commits)")

### Embed Badge

![Health badge](/badges/sakhlab-yii2-relations/health.svg)

```
[![Health](https://phpackages.com/badges/sakhlab-yii2-relations/health.svg)](https://phpackages.com/packages/sakhlab-yii2-relations)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
