Skip to content

Commit 2627110

Browse files
authored
Merge pull request #21 from dmstr/feature/add-timestamp-behavior
TimestampBehavior for twig, less and html models
2 parents 8a3d87d + 9c1fef0 commit 2627110

File tree

8 files changed

+75
-0
lines changed

8 files changed

+75
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
class m211116_101347_add_timestamp_cols extends Migration
6+
{
7+
public function up()
8+
{
9+
$this->addColumn('{{%twig}}', 'created_at', $this->dateTime()->null());
10+
$this->addColumn('{{%twig}}', 'updated_at', $this->dateTime()->null());
11+
$this->addColumn('{{%less}}', 'created_at', $this->dateTime()->null());
12+
$this->addColumn('{{%less}}', 'updated_at', $this->dateTime()->null());
13+
$this->addColumn('{{%html}}', 'created_at', $this->dateTime()->null());
14+
$this->addColumn('{{%html}}', 'updated_at', $this->dateTime()->null());
15+
}
16+
17+
public function down()
18+
{
19+
echo "m211116_101347_add_timestamp_cols cannot be reverted.\n";
20+
return false;
21+
}
22+
}

src/models/BaseModel.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,51 @@
1010
namespace dmstr\modules\prototype\models;
1111

1212

13+
use bedezign\yii2\audit\AuditTrailBehavior;
1314
use Yii;
15+
use yii\behaviors\TimestampBehavior;
1416
use yii\db\ActiveQuery;
1517
use yii\db\ActiveRecord;
18+
use yii\db\Expression;
1619

1720
/**
1821
* @package dmstr\modules\prototype\models
1922
* @author Elias Luhr <[email protected]>
2023
*/
2124
class BaseModel extends ActiveRecord
2225
{
26+
27+
/**
28+
* Column attribute 'created_at'
29+
*/
30+
const ATTR_CREATED_AT = 'created_at';
31+
32+
/**
33+
* Column attribute 'updated_at'
34+
*/
35+
const ATTR_UPDATED_AT = 'updated_at';
36+
37+
/**
38+
* @inheritdoc
39+
*
40+
* Use yii\behaviors\TimestampBehavior for created_at and updated_at attribute
41+
*
42+
* @return array
43+
*/
44+
public function behaviors()
45+
{
46+
47+
$behaviors = parent::behaviors();
48+
49+
$behaviors['timestamp'] = [
50+
'class' => TimestampBehavior::class,
51+
'createdAtAttribute' => static::ATTR_CREATED_AT,
52+
'updatedAtAttribute' => static::ATTR_UPDATED_AT,
53+
'value' => new Expression('NOW()'),
54+
];
55+
return $behaviors;
56+
}
57+
2358
/**
2459
* @inheritdoc
2560
*/

src/views/html/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use dmstr\modules\prototype\models\BaseModel;
34
use rmrevin\yii\fontawesome\FA;
45
use yii\bootstrap\ButtonDropdown;
56
use yii\grid\GridView;
@@ -103,6 +104,8 @@
103104
'contentOptions' => ['nowrap' => 'nowrap'],
104105
],
105106
'key',
107+
BaseModel::ATTR_UPDATED_AT,
108+
BaseModel::ATTR_CREATED_AT,
106109
],
107110
]
108111
); ?>

src/views/html/view.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use dmstr\bootstrap\Tabs;
4+
use dmstr\modules\prototype\models\BaseModel;
45
use rmrevin\yii\fontawesome\FA;
56
use yii\helpers\Html;
67
use yii\widgets\DetailView;
@@ -79,6 +80,8 @@
7980
'id',
8081
'key',
8182
'value:html',
83+
BaseModel::ATTR_UPDATED_AT,
84+
BaseModel::ATTR_CREATED_AT,
8285
],
8386
]
8487
); ?>

src/views/less/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use dmstr\modules\prototype\models\BaseModel;
34
use rmrevin\yii\fontawesome\FA;
45
use yii\bootstrap\ButtonDropdown;
56
use yii\grid\GridView;
@@ -103,6 +104,8 @@
103104
'contentOptions' => ['nowrap' => 'nowrap'],
104105
],
105106
'key',
107+
BaseModel::ATTR_UPDATED_AT,
108+
BaseModel::ATTR_CREATED_AT,
106109
],
107110
]
108111
); ?>

src/views/less/view.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use dmstr\bootstrap\Tabs;
4+
use dmstr\modules\prototype\models\BaseModel;
45
use rmrevin\yii\fontawesome\FA;
56
use yii\helpers\Html;
67
use yii\widgets\DetailView;
@@ -83,6 +84,8 @@
8384
'format' => 'html',
8485
'value' => '<pre>' . Html::encode($model->value) . '</pre>'
8586
],
87+
BaseModel::ATTR_UPDATED_AT,
88+
BaseModel::ATTR_CREATED_AT,
8689
],
8790
]
8891
); ?>

src/views/twig/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use dmstr\modules\prototype\models\BaseModel;
34
use rmrevin\yii\fontawesome\FA;
45
use yii\grid\GridView;
56
use yii\helpers\Html;
@@ -72,6 +73,8 @@
7273
'contentOptions' => ['nowrap' => 'nowrap'],
7374
],
7475
'key',
76+
BaseModel::ATTR_UPDATED_AT,
77+
BaseModel::ATTR_CREATED_AT,
7578
],
7679
]); ?>
7780
</div>

src/views/twig/view.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use dmstr\bootstrap\Tabs;
4+
use dmstr\modules\prototype\models\BaseModel;
45
use rmrevin\yii\fontawesome\FA;
56
use yii\helpers\Html;
67
use yii\widgets\DetailView;
@@ -75,6 +76,8 @@
7576
'format' => 'raw',
7677
'value' => "<pre>" . htmlspecialchars($model->value) . "</pre>",
7778
],
79+
BaseModel::ATTR_UPDATED_AT,
80+
BaseModel::ATTR_CREATED_AT,
7881
],
7982
]); ?>
8083

0 commit comments

Comments
 (0)