PHP code example of pozitronik / yii2-dynamic-attributes
1. Go to this page and download the library: Download pozitronik/yii2-dynamic-attributes 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/ */
pozitronik / yii2-dynamic-attributes example snippets
declare(strict_types = 1);
namespace app\models;
use pozitronik\dynamic_attributes\traits\DynamicAttributesTrait;
use yii\db\ActiveRecord;
class MyTableModel extends ActiveRecord {
use DynamicAttributesTrait;
/*Прочий обычный код*/
}
$tableModel = new MyTableModel();
$tableModel->my_cool_attribute = 'произвольный атрибут'
/*или даже*/
$tableModel->{'числовой атрибут'} = 100500;
/*или даже так*/
$tableModel->{'❤️Z̮̞̠͙͔ͅḀ̗̞͈̻̗Ḷ͙͎̯̹̞͓G̻O̭̗̮❤️'} = 'yes';
$tableModel->save();
/*значения динамических атрибутов сохраняются вместе с моделью, и будут доступны и далее*/
$otherTableModel = MyTableModel::find()->where(['id' => $tableModel->id])->one();
$otherTableModel->{'❤️Z̮̞̠͙͔ͅḀ̗̞͈̻̗Ḷ͙͎̯̹̞͓G̻O̭̗̮❤️'} === 'yes';// => true
declare(strict_types = 1);
namespace app\models;
use pozitronik\dynamic_attributes\traits\DynamicAttributesSearchTrait;
use yii\data\ActiveDataProvider;
/**
* Class UsersSearch
*/
class MyTableModelSearch extends MyTableModel {
/*Добавляем трейт*/
use DynamicAttributesSearchTrait;
/**
* @inheritdoc
*/
public function rules():array {
/*Отдельно описывать правила для динамических атрибутов не нужно, они сгенерируются автоматически*/
return [
[['id'], 'integer']
];
}
/**
* @param array $params
* @return ActiveDataProvider
*/
public function search(array $params):ActiveDataProvider {
$query = MyTableModel::find();
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
/*Добавляем сортировку*/
$dataProvider->setSort($this->adaptSort([
'defaultOrder' => ['id' => SORT_ASC],
'attributes' => [
'id' => [
'asc' => ['id' => SORT_ASC],
'desc' => ['id' => SORT_DESC]
],
]
]));
$this->load($params);
$query->andFilterWhere(['id' => $this->id]);
/*Добавляем поддержку фильтров*/
$this->adaptQuery($query);
return $dataProvider;
}
}
return [
// ...
'modules' => [
'dynamic_attributes' => [
'class' => DynamicAttributesModule::class,
'params' => [
'models' => [/* Список алиасов классов в формате "Имя класса" => "алиас класса" */
MyTableModel:class => 'myTableModel'
],
'limitFloatPrecision' => true, /* Включает ограничение размера сохраняемых значений с плавающей точкой до 14 десятичных знаков, см. DynamicAttributesValues::$limitFloatPrecision */
'cacheEnabled' => true, /* Включает кеширование всех данных модуля, если настроено глобально. Рекомендуется оставить включённым во избежание проблем производительности. */
]
]
]
//...
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.