การใช้งาน BlameableBehavior เพือบันทึก User ID ที่กำลัง Login ใน Yii Framework 2

wave
มานพ กองอุ่น 3 พ.ค. 2016 15:24:40 8,823

BlameableBehavior เป็น behavior ใน Model มีไว้สำหรับการบันทึกผู้ Create และ Update โดยอัตโนมัติ เวลาที่เพิ่มข้อมูลจะนำ id ของ user ที่ login บันทึกลง created_by และ updated_by โดยอัตโนมัติ หากไม่มี User Login จะ return ค่าเป็น null 

https://github.com/yiisoft/yii2/blob/master/framework/behaviors/BlameableBehavior.php

รูปแบบการใช้งาน

use yii\behaviors\BlameableBehavior;
//...
public function behaviors()
  {
      return [
          BlameableBehavior::className(),
      ];
  }
//...

หรือระบุ attribute เฉพาะ หาก attribute ไม่ใช่ created_by และ updated_by

//...
public function behaviors()
  {
      return [
          [
              'class' => BlameableBehavior::className(),
              'createdByAttribute' => 'author_id',
              'updatedByAttribute' => 'updater_id',
          ],
      ];
  }
//...

ตัวอย่าง

ตัวอย่างการใช้งาน BlameableBehavior สำหรับ attribute ที่ไม่ใช้ created_by และ updated_by

<?php
namespace common\models;

use common\models\User;
use yii\behaviors\BlameableBehavior;


class Post extends \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return 'blog_post';
    }

  public function behaviors()
  {
      return [
          [
              'class' => BlameableBehavior::className(),
              'createdByAttribute' => 'author_id',
              'updatedByAttribute' => 'updater_id',
          ],
      ];
  }

    public function rules()
    {
        return [
            [['title', 'content', 'status', 'author_id'], 'required'],
            [['status', 'create_time', 'update_time', 'author_id', 'updater_id'], 'integer'],
            [['content', 'tags'], 'string'],
            [['title'], 'string', 'max' => 128]
        ];
    }


    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'content' => 'Content',
            'tags' => 'Tags',
            'status' => 'Status',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'author_id' => 'เขียนโดย',
            'updater_id' => 'แก้ไขโดย',
        ];
    }
/*
* สร้าง Relation 
*
*/
    public function getAuthor(){
        return $this->hasOne(User::className(), ['id' => 'author_id']);
    }
    public function getUpdater(){
        return $this->hasOne(User::className(), ['id' => 'updater_id']);
    }


}

ทดลอง Login แล้วบันทึกข้อมูลผ่าน Form จะเห็นว่ามีข้อมูลเกิดขึ้นที่ author_id และ updater_id ซึ่งเป็น id ของ User ที่กำลัง Login

 

การเรียกข้อมูลผ่าน Relation

การใช้งาน โดยสามารถเรียกข้อมูลจากการทำ Relation ใน Model โดยมีตัวอย่างการเรียกใช้งานดังนี้

DetailView

<?= DetailView::widget([ 
'model' => $model, 
'attributes' => [ 
    //'id', 
    'title', 
    'content', 
    'tags' 
    'create_time:dateTime', 
    'update_time:dateTime', 
    [ 
        'attribute' => 'author_id', 
        'value' => $model->author->username, 
    ], 
    [ 
        'attribute' => 'updater_id', 
        'value' => $model->updater->username, 
    ]
    ], 
]) ?> 

 


ความคิดเห็น

หากบทเรียนรู้มีความผิดพลาดประการใด หรือมีข้อเสนอแนะกรุณาแจ้ง contact@programmerthailand.com

เขียนบทเรียนรู้ของคุณ

รายละเอียด
  • ดู 8,823
  • รักเลย 0
  • หมวดหมู่ Yii Framework 2 (Yii2)
  • เขียนเมื่อ
  • แก้ไขเมื่อ
  • Tags yii blameable behavior
ข้อมูลผู้เขียน
มานพ กองอุ่น

มานพ กองอุ่น

เป็นสมาชิกเมื่อ: 18 ธ.ค. 2009

เนื้อหาที่เกี่ยวข้อง