2017 © Pedro Peláez
 

yii2-extension yii2-util

Various utilities

image

sateler/yii2-util

Various utilities

  • Wednesday, August 1, 2018
  • by rsateler
  • Repository
  • 2 Watchers
  • 0 Stars
  • 56 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 12 Versions
  • 17 % Grown

The README.md

Utilities for developing yii2 apps

Various utilities for yii2 development, (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require --prefer-dist sateler/yii2-util "*"

or add, (*4)

"sateler/yii2-util": "*"

to the require section of your composer.json file., (*5)

Grid LinkColumn

Usage, (*6)

<?= 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...
    ]
]);

Acronym Formatter

Formats a string into it's Acronym and show's the complete string as a tooltip title. Load the behavior in the config/web.php:, (*7)

    'formatter' => [
        'class' => \yii\i18n\Formatter::className(),
        'as acronymFormatter' => \sateler\util\formatters\AcronymFormatBehavior::className(),
    ],

Or if you use another formatter class, add the behavior:, (*8)

    public function behaviors() {
        return [ \sateler\util\formatters\AcronymFormatBehavior::className() ];
    }

Then you can use Yii::$app->formatter->asAcronym(), or specify the acronym format in GridView or DetailView., (*9)

Select2SearchBehavior

Makes it easy to generate ajax Select2 based queries and widgets., (*10)

Add the behavior to the model and configure it:, (*11)

    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,
            ],
        ];
    }

Enable the controller that provides the data, (*12)

    public function actionSelect2()
    {
        $model = new Model();
        Yii::$app->response->format = 'json';
        return $model->select2Search(Yii::$app->request->queryParams);
    }

In the view use the select2 widget with the url and merge the default config:, (*13)

    $form->field($formModel, 'id')->widget(Select2::className(), 
        Select2SearchBehavior::getSelect2DefaultOptions(Url::to(['model-controller/select2']),
        [
            'language' => 'es',
            'options' => [
                'placeholder' => 'Seleccionar ítem...',
            ],
        ]
    ))

EnumNameBehavior

Autocreate names properties from an array for model attributes., (*14)

Add the behavior to the model and configure it:, (*15)

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',
                    ],
                ],
            ],
        ];
    }

Use the new property:, (*16)

    echo "The user has a property {$user->type} and a name for it: {$user->typeName}";

UuidColumnBehavior

Autocreate names properties from an array for model attributes., (*17)

Add the behavior to the model and configure it:, (*18)

class User extends ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => UuidColumnBehavior::className(),
                'attribute' => 'uuid_column',
                // 'value' => a Uuid v4 by default
            ],
        ];
    }

UnixTimestampStringBehavior

Creates a virtual attribute with a string representation of a timestamp, (*19)

Add the behavior to the model and configure it:, (*20)

class User extends ActiveRecord
{
    public function behaviors() {
        return [
            [
                'class' => UnixTimestampStringBehavior::className(),
                'underlying' => 'timestamp_column',
                'virtual' => 'string_attribute',
                // 'format' => 'Y-m-d', by default
            ],
        ];
    }

The Versions

01/08 2018

dev-master

9999999-dev

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2

01/08 2018

1.2.1

1.2.1.0

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2

14/07 2018

1.2.0

1.2.0.0

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2

22/02 2018

1.1.0

1.1.0.0

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2

29/01 2018

1.0.6

1.0.6.0

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2

18/01 2018

1.0.5

1.0.5.0

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2

18/01 2018

1.0.4

1.0.4.0

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2

12/01 2018

1.0.3

1.0.3.0

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2

12/01 2018

1.0.2

1.0.2.0

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2

05/01 2018

1.0.1

1.0.1.0

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2

17/12 2017

0.1

0.1.0.0

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2

17/12 2017

1.0.0

1.0.0.0

Various utilities

  Sources   Download

Apache-2.0

The Requires

 

by Sateler

extension yii2