Skip to content

Commit

Permalink
attribute typecast
Browse files Browse the repository at this point in the history
  • Loading branch information
Faryshta committed Oct 23, 2017
1 parent a1bc7bc commit b1d5795
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Yii;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\TypecastBehavior;
use yii\db\Expression as DbExpression;
use yii\i18n\PhpMessageSource;
use yii\web\Application as WebApplication;
Expand All @@ -25,6 +26,11 @@ class Module extends \yii\base\Module
*/
public $timestampClass = TimestampBehavior::class;

/**
* @var string
*/
public $typecastClass = AttributeTypecastBehavior::class;

/**
* @var string
*/
Expand Down
11 changes: 11 additions & 0 deletions src/models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ abstract class Entity extends Pivot
*/
protected $updatedAtAttribute = 'updated_at';

/**
* @inheritdoc
*/
protected function attributeTypecast()
{
return parent::attributeTypecast() + [
$this->updatedByAttribute => 'integer',
];
}


/**
* @inheritdoc
*/
Expand Down
10 changes: 10 additions & 0 deletions src/models/PersistentEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ abstract class PersistentEntity extends Entity
*/
protected $deletedAtAttribute = 'deleted_at';

/**
* @inheritdoc
*/
protected function attributeTypecast()
{
return parent::attributeTypecast() + [
$this->deletedByAttribute => 'integer',
];
}

/**
* @inheritdoc
*/
Expand Down
20 changes: 17 additions & 3 deletions src/models/Pivot.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,31 @@ public function behaviors()
'class' => $module->blameableClass,
'createdByAttribute' => $this->createdByAttribute,
'updatedByAttribute' => null,
'value' => $module->getUserId(),
'value' => $module->userId,
],
'timestamp' => [
'class' => $module->timestampClass,
'createdAtAttribute' => $this->createdByAttribute,
'updatedAtAttribute' => null,
'value' => $module->getTimestampValue(),
]
'value' => $module->timestampValue,
],
'typecast' => [
'class' => $module->typecastClass,
'attributeTypes' => $this->attributeTypecast(),
'typecastAfterFind' => true,
],
];
}

/**
* @return ?array pairs of 'attribute' => 'typecast'. Return null if you
* want the typecast to be determined by `rules()` method.
*/
protected function attributeTypecast()
{
return [$this->createdByAttribute => 'integer'];
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit b1d5795

Please sign in to comment.