sateler / yii2-util
各种实用工具
1.3.5
2021-09-06 22:39 UTC
Requires
- php: >=7
- ramsey/uuid: ^4.0
- yiisoft/yii2: ~2.0.13
README
为yii2开发提供的各种实用工具
安装
安装此扩展的首选方式是通过 Composer。
可以运行
php composer.phar require --prefer-dist sateler/yii2-util "*"
或者在 composer.json
文件的 require 部分添加
"sateler/yii2-util": "*"
到
Grid LinkColumn
使用
<?= GridView::widget([ // other options... 'columns' => [ [ 'class' => LinkColumn::class, 'attribute' => 'name', 'idAttribute' => 'id', // 'id' by default, 'linkIdAttribute' => 'id', // the id attribute name for the generated link url, 'id' by default (...?id=...) 'action' => 'update', // 'view' by default 'controller' => 'models', // null by default (current controller) // if you need something more complex than a simple link // 'urlCreator' => function ($model, $key, $index) { return ['controller/action', 'id' => $model->id]; } 'linkOptions' => [], // or a callable for each row // if some links may not be accessible //'createLink' => function ($model, $key, $index) { return Yii::$app->user->can('view', ['model' => $model]); }, ], // other columns... ] ]);
缩写格式化器
将字符串格式化为缩写并显示完整字符串作为工具提示标题。在 config/web.php
中加载行为
'formatter' => [ 'class' => \yii\i18n\Formatter::className(), 'as acronymFormatter' => \sateler\util\formatters\AcronymFormatBehavior::className(), ],
或者如果你使用另一个格式化器类,添加行为
public function behaviors() { return [ \sateler\util\formatters\AcronymFormatBehavior::className() ]; }
然后你可以使用 Yii::$app->formatter->asAcronym()
,或者在 GridView
或 DetailView
中指定 acronym
格式。
Select2SearchBehavior
轻松生成基于ajax的Select2查询和组件。
将行为添加到模型并配置它
public function behaviors() { return [ 'select2' => [ 'class' => Select2SearchBehavior::className(), // Common configurations: 'select2SearchAttributes' => ['attr1', 'attr2'], // Required. These are the attributes that will be searched 'select2SortAttributes' => ['attr3' => SORT_ASC], // To sort the results. Defaults to ['id' => SORT_ASC]. 'select2FilterQuery' => function($query, $params) { $query->joinWith(['other_table'])->andWhere(['attr4' => 'constant']); }, // To modify the query. Defaults to null 'select2ShowTextFunction' => function($model) { return "{$model->attr1} / ({$model->attr2})"; }, // To build the text property. Defaults to implode the search attributes. 'select2ExplodeSearchTermChar' => ' ', // Explode the search query using this chars, false to disable. Defaults to ' '. // Param names and other config (with default values): 'select2IdProperty' => 'id', // The property to be used as id 'select2idParameter' => 'id', 'select2SearchParameter' => 'search', 'select2IdParameter' => 'page', 'select2PageParameter' => 20, ], ]; }
启用提供数据的控制器
public function actionSelect2() { $model = new Model(); Yii::$app->response->format = 'json'; return $model->select2Search(Yii::$app->request->queryParams); }
在视图中使用select2组件,指定url并合并默认配置
$form->field($formModel, 'id')->widget(Select2::className(), Select2SearchBehavior::getSelect2DefaultOptions(Url::to(['model-controller/select2']), [ 'language' => 'es', 'options' => [ 'placeholder' => 'Seleccionar ítem...', ], ] ))
EnumNameBehavior
自动为模型属性创建从数组生成的名称属性。
将行为添加到模型并配置它
class User extends ActiveRecord { const TYPE_ADMIN = 'admin'; const TYPE_USER = 'user'; public static $types = [ self::TYPE_ADMIN => 'Administrator', self::TYPE_USER => 'User', ]; public function behaviors() { return [ [ 'class' => EnumNameBehavior::className(), 'properties' => [ [ 'values' => self::$types, 'property' => 'type', 'name' => 'typeName', ], ], ], ]; }
使用新属性
echo "The user has a property {$user->type} and a name for it: {$user->typeName}";
UuidColumnBehavior
自动为模型属性创建从数组生成的名称属性。
将行为添加到模型并配置它
class User extends ActiveRecord { public function behaviors() { return [ [ 'class' => UuidColumnBehavior::className(), 'attribute' => 'uuid_column', // 'value' => a Uuid v4 by default ], ]; }
UnixTimestampStringBehavior
创建一个具有时间戳字符串表示的虚拟属性
将行为添加到模型并配置它
class User extends ActiveRecord { public function behaviors() { return [ [ 'class' => UnixTimestampStringBehavior::className(), 'underlying' => 'timestamp_column', 'virtual' => 'string_attribute', // 'format' => 'Y-m-d', by default ], ]; }