PHPackages                             andrewdanilov/yii2-behaviors - 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. [Image &amp; Media](/categories/media)
4. /
5. andrewdanilov/yii2-behaviors

ActiveYii2-extension[Image &amp; Media](/categories/media)

andrewdanilov/yii2-behaviors
============================

Various behaviors for AR models

1.1.0(1mo ago)21913MITPHPPHP &gt;=8.0

Since Apr 8Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/AndrewDanilov/yii2-behaviors)[ Packagist](https://packagist.org/packages/andrewdanilov/yii2-behaviors)[ RSS](/packages/andrewdanilov-yii2-behaviors/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (10)Versions (34)Used By (3)

Yii2 Behaviors
==============

[](#yii2-behaviors)

Various behaviors for AR models

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
composer require andrewdanilov/yii2-behaviors "~1.1.0"

```

or add

```
"andrewdanilov/yii2-behaviors": "~1.1.0"

```

to the `require` section of your `composer.json` file.

Usage
=====

[](#usage)

DateBehavior
------------

[](#datebehavior)

At your controller class add to `behaviors()` method:

```
use yii\db\ActiveRecord;
use andrewdanilov\behaviors\DateBehavior;

class MyController extends ActiveRecord
{
	public function behaviors()
	{
		return [
			// ...
			[
				'class' => DateBehavior::class,
				// AR model attributes to process by behavior
				'dateAttributes' => [
					// DateTime format
					'date_1' => DateBehavior::DATETIME_FORMAT,
					// DateTime format with current datetime as default value if param is empty
					'date_2' => DateBehavior::DATETIME_FORMAT_AUTO,
					// Date format without time
					'date_3' => DateBehavior::DATE_FORMAT,
					// Date format without time with current datetime as default value if param is empty
					'date_4' => DateBehavior::DATE_FORMAT_AUTO,
					// Short notation equal to: 'date_5' => DateBehavior::DATE_FORMAT
					'date_5',
				],
			],
			// ...
		];
	}
}
```

DateBehavior converts date into mysql format before it will be saved to database (`onBeforeSave` event) and into display format after it is fetched from database (`onAfterFind` event).

You can define display format by modifiyng `Yii::$app->formatter` component in your config:

```
$config = [
	// ...
	'components' => [
		// ...
		'formatter' => [
			'defaultTimeZone' => 'Europe/Moscow',
			'dateFormat'     => 'php:d.m.Y',
			'datetimeFormat' => 'php:d.m.Y H:i:s',
			'timeFormat'     => 'php:H:i:s',
		],
	],
];
```

If you have problems with time shifting, set `defaultTimeZone` property of formatter.

TagBehavior
-----------

[](#tagbehavior)

Use this behavior to link two models with many-to-many relation via staging table. This behavior will take care of saving new links to staging table and removing obsolete ones.

Model `Product.php`

```
