Behavior dengan TimestampBehavior dan BlameableBehavior
Model :
<?php
namespace app\models;
use Yii;
use yii\db\Expression;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\BlameableBehavior;
/**
* This is the model class for table "pesan".
*
* @property int $id
* @property string $barang
* @property string $jumlah
* @property string $pemesan
* @property string $alamat
* @property string $nohp
* @property string $status
* @property string $tanggal
* @property string $gambar
*/
class Pesan extends \yii\db\ActiveRecord
{
public $gambarFile;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'pesan';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['tanggal'], 'safe'],
[['barang', 'jumlah', 'pemesan', 'alamat', 'nohp', 'status'], 'string', 'max' => 255],
[['gambarFile'], 'file',
'extensions' => 'jpg,png,jpeg',
'maxSize'=>'256000000',
'skipOnEmpty'=>true,
],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'barang' => 'Barang',
'jumlah' => 'Jumlah',
'pemesan' => 'Pemesan',
'alamat' => 'Alamat',
'nohp' => 'Nohp',
'status' => 'Status',
'tanggal' => 'Tanggal',
'gambarFile' => 'Upload Gambar',
];
}
public function behaviors(){
return [
TimestampBehavior::className(),
BlameableBehavior::className()
];
}
}
View
view.php
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
use app\models\User;
/* @var $this yii\web\View */
/* @var $model app\models\Barang */
$this->title = $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Pesan', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<?php
$username_created_by = $model->created_by;
if($user=User::findIdentity($model->created_by)){
$username_created_by=$user->username;
}
$username_updated_by = $model->updated_by;
if($user=User::findIdentity($model->updated_by)){
$username_updated_by=$user->username;
}
?>
<div class="barang-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<div class="row">
<div class="col-md-7">
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
// 'gambar',
[
'attribute' => 'created_by',
'value' => $username_created_by,
],
[
'attribute' => 'updated_by',
'value' => $username_updated_by,
],
[
'attribute' => 'created_at',
'format' => ['date', 'php:d-m-Y H:i:s']
],
[
'attribute' => 'updated_at',
'format' => ['date', 'php:d-m-Y H:i:s']
],
],
]) ?>
</div>
<div class="col-md-5">
<center>
<?php
if(!empty($model->gambar)){
?>
<img src="<?= yii::$app->request->baseUrl; ?>
/imgpesan/<?= $model->gambar; ?>" width="50%"/>
<?php
}
else{
?>
<img src="<?= yii::$app->request->baseUrl; ?>
/imgpesan/noimg.png" width="50%"/>
<?php
}
?>
</center>
</div>
</div>
</div>
index. php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use app\models\User;
/* @var $this yii\web\View */
/* @var $searchModel app\models\PesanSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Beli Barang';
//$this->params['breadcrumbs'][] = $this->title;
?>
<div class="pesan-index">
<h1><?php // Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Beli', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'barang',
'jumlah',
'pemesan',
'alamat',
//'nohp',
//'status',
//'tanggal',
//'gambar',
[
'attribute' => 'created_at',
'format' => ['date', 'php:Y-m-d H:i:s']
],
[
'attribute' => 'updated_at',
'format' => ['date', 'php:Y-m-d H:i:s']
],
// 'updated_at',
[
'attribute' => 'updated_by',
'value' => function($data){
$username_updated_by = $data->updated_by;
if($user=User::findIdentity($data->updated_by)){
$username_updated_by=$user->username;
}
return $username_updated_by;
}
],
[
'attribute' => 'created_by',
'value' => function($data){
$username_created_by = $data->created_by;
if($user=User::findIdentity($data->created_by)){
$username_created_by=$user->username;
}
return $username_created_by;
}
],
// 'updated_by',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
Field Tabel
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
`created_by` int(11) NOT NULL,
`updated_by` int(11) NOT NULL
0 Response to "Behavior dengan TimestampBehavior dan BlameableBehavior"
Posting Komentar