Yii2: Tutorial Membuat Blog Sederhana Part III

Setelah pada dua artikel sebelumnya kita ngoprek frontend.. maka saya kira cukup untuk tampilan blog sederhana, silahkan dilanjutkan sendiri fitur2 lainnya.. Sekarang kita beralih ke bagian administrator atau backend

Yang akan kita lakukan adalah bikin Crud untuk kelola Post, Category dan Comment

# Generate CRUD Post
Gunakan gii aja ya.. http://back.end/gii

Model Class= common\models\Post
Search Model Class = common\models\PostSearch
Controller Class =backend\controllers\PostController

gii

# Generate CRUD Category
Gunakan gii aja ya.. http://back.end/gii

Model Class= common\models\Category
Search Model Class = common\models\CategorySearch
Controller Class =backend\controllers\CategoryController

# Generate CRUD Comment
Gunakan gii aja ya.. http://back.end/gii

Model Class= common\models\Comment
Search Model Class = common\models\CommentSearch
Controller Class =backend\controllers\CommentController

# Edit Menu pada \backend\views\layouts\main.php
Menu menuju CRUD Category, Post dan Comment

	$menuItems[] = ['label' => 'Post', 'url' => ['/post']];
	$menuItems[] = ['label' => 'Category', 'url' => ['/category']];
	$menuItems[] = ['label' => 'Comment', 'url' => ['/comment']];
	

menu

Atau kalo ingin model dropdown kita bisa lakukan ini

   $menuItems[] = ['label' => 'Blog', 'items' => [
	 ['label' => 'Post', 'url' => ['/post']],
	 ['label' => 'Category', 'url' => ['/category']],
	 ['label' => 'Comment', 'url' => ['/comment']]
   ]];

dropdown
# Edit backend\views\post\_form.php

		use yii\helpers\ArrayHelper; // load classes

		<?php // $form->field($model, 'category_id')->textInput() ?>
		<?php
		$dataCategory=ArrayHelper::map(\common\models\Category::find()->asArray()->all(), 'id', 'name');
		echo $form->field($model, 'category_id')->dropDownList($dataCategory,
				 ['prompt'=>'-Choose a Category-'])
		?>	

		<?php
		echo $form->field($model, 'status')
			->dropDownList(
				['0'=>'Draft','1'=>'Publish'],
				['prompt'=>'']
			);
		?>
	

form
# Untuk Create & Update Time Harusnya Udah Pake Behaviour
- Edit model \common\models\Post

	public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'attributes' => [
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
                    \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
                ],
            ],
			'autouserid' => [
                'class' => \yii\behaviors\BlameableBehavior::className(),
                'attributes' => [
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['user_id'],
                ],
            ],
        ];
    }
	

Ada dua class yang kita pake yaitu
1. TimestampBehavior untuk mendapatkan current timestamp yang mana kemudian diassign ke field create_time and update_time pada event BEFORE_INSERT (akan dijalankan sebelum proses insert)
2. BlameableBehavior untuk mendapatkan current user id alalu diassign ke field user_id event BEFORE INSERT
- Edit model \common\models\Category

	public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'attributes' => [
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
                    \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
                ],
            ],
        ];
    }
	

- Edit model \common\models\Comment

	public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'attributes' => [
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['create_time'],
                ],
            ],
        ];
    }
	

- Edit Gridview pada backend\views\post\index.php

	<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'title',
            'content:ntext',
            [
				'class' => 'yii\grid\DataColumn', // can be omitted, default
				'attribute' => 'category_id',
				'value' => function ($data) {
					return $data->category->name; // Get field relation
				},
			],
            [
				'attribute' => 'status',
				'value' => function ($data) {
					return (($data->status)==1)?"Publish":"Draft";
				},
			],
            // 'create_time:datetime',
            // 'update_time:datetime',
            // 'user_id',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
	

grid
Bersambung..

Source code lengkap.. https://drive.google.com/file/d/0BwSv7MUYD04kcXMtYy1rZVVFMHc/edit?usp=sharing

Tags:
26 Comments
  1. puspita
  2. Anggara Jauhari
  3. aswandi
  4. Hafid Mukhlasin
  5. karisa
    • Hafid Mukhlasin
    • Hafid Mukhlasin
  6. google
  7. google
  8. Agus Minanur Rohman
  9. google
    • google
  10. Hafid Mukhlasin
    • google
      • tantowi
        • mawool
      • Yii2
  11. triady
  12. sidik
  13. Yii2
  14. fauzi
  15. ayunda
  16. website
  17. irfan
  18. Google

Leave a Reply

Your email address will not be published. Required fields are marked *