Skip to content

Commit

Permalink
Merge pull request #260 from liasica/master
Browse files Browse the repository at this point in the history
Support custome TimestampBehavior
  • Loading branch information
schmunk42 authored Aug 1, 2019
2 parents 1cb6245 + 3f7ba0b commit cfde783
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/commands/BatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class BatchController extends Controller
*/
public $useTimestampBehavior = true;

/**
* @var string support user custom TimestampBehavior class
*/
public $timestampBehaviorClass = 'yii\behaviors\TimestampBehavior';

/**
* @var string the name of the column where the user who updated the entry is stored
*/
Expand Down Expand Up @@ -205,7 +210,7 @@ class BatchController extends Controller
* @var bool whether to generate access filter migrations
*/
public $generateAccessFilterMigrations;

public $crudBaseTraits;

public $crudTemplate = 'default';
Expand Down Expand Up @@ -273,6 +278,7 @@ public function options($id)
'template',
'overwrite',
'useTimestampBehavior',
'timestampBehaviorClass',
'createdAtColumn',
'updatedAtColumn',
'useTranslatableBehavior',
Expand Down Expand Up @@ -380,6 +386,7 @@ public function actionModels()
'interactive' => $this->interactive,
'overwrite' => $this->overwrite,
'useTimestampBehavior' => $this->useTimestampBehavior,
'timestampBehaviorClass' => $this->timestampBehaviorClass,
'createdAtColumn' => $this->createdAtColumn,
'updatedAtColumn' => $this->updatedAtColumn,
'useTranslatableBehavior' => $this->useTranslatableBehavior,
Expand Down
14 changes: 11 additions & 3 deletions src/generators/model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class Generator extends \yii\gii\generators\model\Generator
*/
public $useTimestampBehavior = true;

/**
* @var string support user custom TimestampBehavior class
*/
public $timestampBehaviorClass = 'yii\behaviors\TimestampBehavior';

/**
* @var string the name of the column where the user who updated the entry is stored
*/
Expand Down Expand Up @@ -147,7 +152,7 @@ public function rules()
'useTimestampBehavior',
'singularEntities',
], 'boolean'],
[['languageTableName', 'languageCodeColumn', 'createdByColumn', 'updatedByColumn', 'createdAtColumn', 'updatedAtColumn', 'savedForm'], 'string'],
[['languageTableName', 'languageCodeColumn', 'createdByColumn', 'updatedByColumn', 'createdAtColumn', 'updatedAtColumn', 'savedForm', 'timestampBehaviorClass'], 'string'],
[['tablePrefix'], 'safe'],
]
);
Expand Down Expand Up @@ -188,6 +193,7 @@ public function formAttributes()
'useTimestampBehavior',
'createdAtColumn',
'updatedAtColumn',
'timestampBehaviorClass',
];
}

Expand Down Expand Up @@ -221,6 +227,7 @@ public function hints()
'generateHintsFromComments' => 'This indicates whether the generator should generate attribute hints
by using the comments of the corresponding DB columns.',
'useTimestampBehavior' => 'Use <code>TimestampBehavior</code> for tables with column(s) for created at and/or updated at timestamps.',
'timestampBehaviorClass' => 'Use custom TimestampBehavior class.',
'createdAtColumn' => 'The column name where the created at timestamp is stored.',
'updatedAtColumn' => 'The column name where the updated at timestamp is stored.',
'useBlameableBehavior' => 'Use <code>BlameableBehavior</code> for tables with column(s) for created by and/or updated by user IDs.',
Expand Down Expand Up @@ -678,8 +685,9 @@ protected function generateTimestamp($table)

if ($this->useTimestampBehavior && ($createdAt || $updatedAt)) {
return [
'createdAtAttribute' => $createdAt,
'updatedAtAttribute' => $updatedAt,
'createdAtAttribute' => $createdAt,
'updatedAtAttribute' => $updatedAt,
'timestampBehaviorClass' => $this->timestampBehaviorClass,
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/generators/model/default/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use yii\behaviors\BlameableBehavior;
<?php endif; ?>
<?php if (!empty($timestamp)): ?>
use yii\behaviors\TimestampBehavior;
use <?php echo $timestamp['timestampBehaviorClass']; ?>;
<?php endif; ?>

/**
Expand Down

0 comments on commit cfde783

Please sign in to comment.