Skip to main content

Notes

Useful extensions

https://wbraganca.com/yii2extensions

Sorting/filtering in model met relaties

https://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview

Bootstrap (4) Icons

https://www.w3schools.com/icons/bootstrap_icons_glyphicons.asp

Caching example

https://www.digitalocean.com/community/tutorials/how-to-use-caching-with-the-yii-framework-to-improve-performance

Easy Debug - d() en dd()

https://www.yiiframework.com/wiki/793/debuging-variables-in-yii2

Krajee Exention

https://demos.krajee.com

Buttons

//Cancel

<?= Html::a('Cancel', ['index'], ['class'=>'btn btn-primary']) ?>

//Delete
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>

Active Records

select

$examen = examen::find()->where(['actief' => '1'])->orderBy(['datum_van' => 'SORT_DESC'])->one();

$examen = examen::find()->where(['actief' => '1'])->orderBy(['datum_van' => 'SORT_DESC'])->all();

$gesprekSoort = gesprekSoort::find()->all();

$gesprekSoort = examenGesprekSoort::findAll(['examen_id' => $id]);

$gesprekken = gesprekSoort::find()->where([])->joinWith('examenGesprekSoorts')->all();

Insert update delete

// INSERT (table name, column values)
Yii::$app->db->createCommand()->insert('user', [
    'name' => 'Sam',
    'age' => 30,
])->execute();

// INSERT via raw Query
$sql="insert into examen_gesprek_soort (examen_id, gesprek_soort_id) values(:examenid, :gesprekid)";
$params = array(':examenid'=> $id, ':gesprekid' => $value );
$result = Yii::$app->db->createCommand($sql)->bindValues($params)->execute();

// UPDATE (table name, column values, condition)
Yii::$app->db->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();

// DELETE (table name, condition)
Yii::$app->db->createCommand()->delete('user', 'status = 0')->execute();

$sql="delete from examen_gesprek_soort where examen_id = :examenid";
$params = array(':examenid'=> $id);
$result = Yii::$app->db->createCommand($sql)->bindValues($params)->execute();

Voor- nadelen

  Laravel YII
MVC
Model Eloquent Active Record
View/templating Blade PHP met 'Yii Snippets'
Routing Expliciet Impliciet / Expliciet
Migrations
(meer als optie)
Model creations command line
GUI, reverse engineered form DB
Controller Creation command line
GUI, create full CRUD
View creation ?
GUI, create full CRUD
Documentation ***
**
Active development
Add Ons / Libraries *** **
Install Base (volgens Google) 484,970 58,800
     

Grootste voordeel is de GUI waarmee je reverse engineered een CRUD maakt.

Login

http://code-epicenter.com/how-to-login-user-from-a-database-in-yii-framework-2/

Create CRUD Example

Model Class app\models\Examen
Search Model Class app\models\ExamenSearch
Controller Class app\controllers\ExamenController
View path leeg laten (wordt default waaarde)

Datepicker Install

(in project root)

change in file composer.dev  "minimum-stability": "dev",

Bootstrap 4 install

composer require --prefer-dist yiisoft/yii2-bootstrap4

Save, Delete en Cancel Button

// Save
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>

// Delete
<a class="btn btn-danger" href="/gesprek/delete?id=<?=$model->id?>" data-confirm="Gesprek verwijderen?" data-method="post">Delete</a>

// Cancel
<?= Html::a('Cancel', ['/gesprek/overzicht'], ['class'=>'btn btn-primary']) ?>

Redirect

return $this->redirect(['view', 'id' => $model->id]);

GridView Widget

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        // ['class' => 'yii\grid\SerialColumn'],
        [
          'attribute'=>'naam',
          'contentOptions' => ['style' => 'width:600px; white-space: normal;'],
          'format' => 'raw',
          'value' => function ($data) {
            return Html::a($data->naam, '/examen/update?id='.$data->id);
          },
        ],
        [
          'attribute'=>'datum_van',
          'contentOptions' => ['style' => 'width:40px; white-space: normal;'],
        ],
    ],
]); ?>
ActionColumn show view conditional
[
    'class' => 'yii\grid\ActionColumn',
    'contentOptions' => ['style' => 'width:400px; white-space: normal;'],
    'header'=>'Actions',
    'template' => '{view} {update} {delete}',
    'visibleButtons'=>[
        'view'=> function($model){
              return $model->status!=1;
         },
    ]
],
ActionColumn add own function
[
    'class' => 'yii\grid\ActionColumn',
    'template' => '{download} {view} {update} {delete}',
    'buttons' => [
        'download' => function ($url) {
            return Html::a(
                '<span class="glyphicon glyphicon-arrow-down"></span>',
              	['another-controller/anotner-action', 'id' => $model->id],   // mag ook alleen url als je geen parameter wilt meegeven, dan hoef je geen array te maken.
                [
                    'title' => 'Download',
                    'data-pjax' => '0',
                ]
            );
        },
    ],
],

// Alternatief, copy button, niet vergeten in template te te zetten
    'buttons'=>[
         'copy' => function ($url, $model, $key) {
             return Html::a('<span class="glyphicon glyphicon-copy"></span>', ['copy', 'id'=>$model->id],['title'=>'Copy']);
         },
    ],   
Column Show Boolean as symbol
 [
   'attribute'=>'actief',
   'contentOptions' => ['style' => 'width:10px; white-space: normal;'],
   'format' => 'raw',
       'value' => function ($data) {
            $status = $data->actief ? '<span class="glyphicon glyphicon-ok"></span>' : '<span class="glyphicon glyphicon-minus"></span>';
            return Html::a($status, '/rolspeler/toggle-actief?id='.$data->id);
        }
 ],

Drop down in grid select

https://stackoverflow.com/questions/29231013/how-can-i-use-a-simple-dropdown-list-in-the-search-box-of-gridviewwidget-yii2

[
    'attribute'=>'attribute name',
    'filter'=>array("ID1"=>"Name1","ID2"=>"Name2"),
],

// of

[
    'attribute'=>'attribute name',
    'filter'=>ArrayHelper::map(Model::find()->asArray()->all(), 'ID', 'Name'),
],

// of

[
    'attribute' => 'attribute_name',
    'value' => 'attribute_value',
    'filter' => Html::activeDropDownList(
      				$searchModel,
      				'attribute_name',
                    ArrayHelper::map(ModelName::find()->asArray()->all(), 'ID', 'Name'),
                    ['class'=>'form-control','prompt' => 'Select Category']
                ),
],

Post form on dropdown change

<select name="status" id="status" onchange="this.form.submit()">