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

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

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();

In Controller, get data from arbitrary model;

use app\models\Examen;

$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]);

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

$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();

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

Debug

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

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

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

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

Post form on dropdown change

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