PHPackages                             notamedia/yii2-relation - 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. [Database &amp; ORM](/categories/database)
4. /
5. notamedia/yii2-relation

ActiveYii2-extension[Database &amp; ORM](/categories/database)

notamedia/yii2-relation
=======================

Saving related data in Yii2

1.0.0(8y ago)95.2k4[2 issues](https://github.com/notamedia/yii2-relation/issues)MITPHPPHP &gt;=5.6.0

Since Dec 30Pushed 8y ago4 watchersCompare

[ Source](https://github.com/notamedia/yii2-relation)[ Packagist](https://packagist.org/packages/notamedia/yii2-relation)[ RSS](/packages/notamedia-yii2-relation/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (4)Dependencies (5)Versions (5)Used By (0)

Saving Related Data in Yii2
===========================

[](#saving-related-data-in-yii2)

[![Build Status](https://camo.githubusercontent.com/69d02e43d23e07aec800d7d4b61ea1133965b9815004441c8310afdd304599e0/68747470733a2f2f7472617669732d63692e6f72672f6e6f74616d656469612f796969322d72656c6174696f6e2e737667)](https://travis-ci.org/notamedia/yii2-relation)[![Latest Stable Version](https://camo.githubusercontent.com/95e141794f4d6b02aba9875f053430594bd2cc1afb90b96c501d7856d177382f/68747470733a2f2f706f7365722e707567782e6f72672f6e6f74616d656469612f796969322d72656c6174696f6e2f762f737461626c65)](https://packagist.org/packages/notamedia/yii2-relation)[![Total Downloads](https://camo.githubusercontent.com/6769e7ec36c9cf97b9baa6245a35b5b02f48edcf3a35685dd4f2fb0636c07e85/68747470733a2f2f706f7365722e707567782e6f72672f6e6f74616d656469612f796969322d72656c6174696f6e2f646f776e6c6f616473)](https://packagist.org/packages/notamedia/yii2-relation)[![License](https://camo.githubusercontent.com/79a935754d83f12da8a6dbf65f5d6a19c9a9a9a471babbcdc57452a13dc0ab03/68747470733a2f2f706f7365722e707567782e6f72672f6e6f74616d656469612f796969322d72656c6174696f6e2f6c6963656e7365)](https://packagist.org/packages/notamedia/yii2-relation)

Behavior for support relational data management.

- Insert related models from POST array.
- Pre-processing for new models via callback function.
- Delete related models from database which not exist in POST array.
- Skip related models which already exist in database with same attributes.
- Rollback database changes, if relational model save/delete error occurred.
- Support one-to-one and one-to-many relations.

With pre-processing you can set additional logic before create related models. For example, to add additional columns data to the junction table in a many-to-many relationship.

This behavior uses getters for relational attribute in owner model, such getters must return `ActiveQuery` object. If you use string values in ON condition in `ActiveQuery` object, then this behavior will throw exception.

Installation
------------

[](#installation)

```
composer require notamedia/yii2-relation
```

Usages
------

[](#usages)

For make works this behavior you need:

- Add all relational properties to rules as safe attribute.
- Declare getter for relational attribute.
- Put attribute or attribute with callback to relations property of behavior.
- All used models need to have only one primary key column.

### One to One Relationships

[](#one-to-one-relationships)

```
