antonyz89/yii2-templates

minor changes in model/crud templates

Installs: 1 439

Dependents: 1

Suggesters: 0

Security: 0

Stars: 2

Watchers: 3

Forks: 0

Type:yii2-extension

1.5.1 2021-08-23 17:43 UTC

This package is auto-updated.

Last update: 2024-04-13 03:43:11 UTC


README

Donate with PayPal

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist antonyz89/yii2-templates dev-master

or add

"antonyz89/yii2-templates": "dev-master"

to the require section of your composer.json file.

Usage

environments/dev/common/config/main

and run php init again

if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['bootstrap'] = ['gii'];
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        'generators' => [
            'model' => [
                'class' => 'antonyz89\templates\model\Generator',
                'templates' => [
                    'default' => '@antonyz89/templates/model/default', // add default template
                ]
            ],
            'crud' => [
                'class' => 'antonyz89\templates\crud\Generator',
                'templates' => [
                    'admin-lte' => '@antonyz89/templates/crud/admin-lte', // add admin-lte template
                    'material-lite' => '@antonyz89/templates/crud/material-lite', // add material-lite template
                    'material-bootstrap' => '@antonyz89/templates/crud/material-bootstrap', // add material-bootstrap template
                    'default' => '@antonyz89/templates/crud/default', // add default template
                ]
            ]
        ],
    ];
}

Easy Pjax with _search.php

Kartik GridView ONLY

CRUD generated by Gii with any template already do it, just add YiiTemplateAsset to complete

Usage:

1 - Just add YiiTemplateAsset on YOUR_MODULE/assets/AppAsset

use antonyz89\templates\assets\YiiTemplatesAsset;

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [];
    public $js = [];
    public $depends = [
        YiiTemplatesAsset::class // ADD
    ];
}

2 - Now, just enable pjax on GridView

GridView::widget([
    'dataProvider' => $dataProvider,
    'pjax' => true, // here
    'columns' => [
        'id',
        'name',
        [
            'class' => 'kartik\grid\ActionColumn',
            'width' => '150px',
            'buttonOptions' => [
                'class' => 'btn btn-sm btn-default'
            ],
            'updateOptions' => [
                'class' => 'btn btn-sm btn-primary'
            ],
            'deleteOptions' => [
                'class' => 'btn btn-sm btn-danger'
            ]
        ]
    ]
]);

3 - Add data-ajax => true on _search's form

<div class="example-search">

    <?php $form = ActiveForm::begin([
        'action' => ['index'],
        'method' => 'get',
        'options' => [
            'data-pjax' => true // HERE
        ],
    ]); ?>

    <div class="row">
        <div class="col-md-1">
            <?= $form->field($model, 'id') ?>
        </div>
        <div class="col-md-5">
            <?= $form->field($model, 'name') ?>
        </div>
    </div>

    <?php ActiveForm::end(); ?>

</div>

DONE!

Just type on _search's fields and GridView will reload

Pjax only from form's submit

To use Pjax only from form's submit, just add 'pjax-only-on-submit' => true on ActiveForm::begin from _search.php

<?php $form = ActiveForm::begin([
        'action' => ['index'],
        'method' => 'get',
        'options' => [
            'data-pjax' => true,
            'pjax-only-on-submit' => true // HERE
        ],
    ]); ?>

ActiveQuery

Alias

Use @alias. on query to replace it with ::tableName or alias.

// Query
User::find()->alias('example')->where(['@alias.name' => 'Antony'])->groupBy('@alias.age');
# result
SELECT `user`.* FROM `user` as `example` WHERE `example`.`name` = 'Antony' GROUP BY `example`.`age`

Disable GROUP BY

set $query->groupBy(false) to disable any GROUP BY on query