PHP code example of andrewdanilov / yii2-behaviors
1. Go to this page and download the library: Download andrewdanilov/yii2-behaviors library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
andrewdanilov / yii2-behaviors example snippets
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',
],
],
// ...
];
}
}
namespace common\models;
/**
* Class Product
*
* @property int $id
* ...
* @property int[] $category_ids
*/
class Product extends \yii\db\ActiveRecord
{
public $category_ids;
public function behaviors()
{
return [
'category' => [ // name of behavior
'class' => 'andrewdanilov\behaviors\TagBehavior',
'referenceModelClass' => 'common\models\ProductCategory',
'referenceModelAttribute' => 'product_id',
'referenceModelTagAttribute' => 'category_id',
'tagModelClass' => 'common\models\Category',
'ownerModelIdsAttribute' => 'category_ids',
]
];
}
/**
* Getter for retrieving child models (tags) list.
* It can be used therefore as property $product->categories
* or link named 'categories', i.e. $product->with('categories')
*
* @return \yii\db\ActiveQuery
*/
public function getCategories()
{
$behavior = $this->getBehavior('category'); // use name of behavior here ('category')
if ($behavior instanceof \andrewdanilov\behaviors\TagBehavior) {
return $behavior->getTags();
}
return null;
}
// ...
}
namespace common\models;
use yii\db\ActiveRecord;
/**
* Class Category
*
* @property int $id
* @property string $name
* ...
*/
class Category extends ActiveRecord
{
// ...
}
namespace common\models;
use yii\db\ActiveRecord;
/**
* Class ProductCategory
*
* @property int $id
* @property int $product_id
* @property int $category_id
*/
class ProductCategory extends ActiveRecord
{
// ...
}
use andrewdanilov\behaviors\TagBehavior;
use common\models\Category;
use common\models\Product;
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
/* @var $this yii\web\View */
/* @var $form yii\widgets\ActiveForm */
/* @var $model Product|TagBehavior */
use andrewdanilov\behaviors\JsonTypecastBehavior;
class Item extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
'jsontypecast' => [
'class' => JsonTypecastBehavior::class,
'attributes' => [
'json_field_1', // field names
'json_field_2',
'json_field_3',
// ...
],
],
];
}
// ...
}
// Use example:
$item = new Item();
$item->json_field_1 = [1, 2, 3];
// before saving json_field_1 will be converted to a json string
$item->save();
// then after saving it will be converted back to an array
print_r($item->json_field_1);
// Outputs:
// Array
// (
// [0] => 1
// [1] => 2
// [2] => 3
// )
class MyController extends \yii\web\Controller
{
public function behaviors(): array
{
return array_merge(parent::behaviors(), [
'rateLimit' => [
'class' => \andrewdanilov\behaviors\RateLimitBehavior::class,
// identityKey - key to identify current visitor, optional
'identityKey' => Yii::$app->user->id ?? Yii::$app->session->getId(),
'limits' => [
// '<action>' => [<interval>, <rate>]
// <action> - идентификатор действия
// <interval> - интервал, на котором проводим измерения
// <rate> - доступное число запросов на указанном интервале
'index' => [1, 100], // 100 раз в секунду
'view' => 10, // 10 раз в секунду (сокращенная запись)
'create' => [60, 1], // 1 раз в минуту
'update' => [1, 1], // 1 раз в секунду
],
],
]);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.