-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
217f2d0
commit 0187008
Showing
5 changed files
with
214 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,15 @@ | |
use CActiveRecord; | ||
use CBehavior; | ||
use CBooleanValidator; | ||
use CEvent; | ||
use CModelEvent; | ||
use CNumberValidator; | ||
use CStringValidator; | ||
use InvalidArgumentException; | ||
|
||
/** | ||
* @property \CModel|CActiveRecord $owner The owner component that this behavior is attached to. | ||
* @property \CModel|\CActiveRecord $owner The owner component that this behavior is attached to. | ||
* @property array $attributeTypes | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
|
@@ -78,10 +80,10 @@ class AttributeTypecastBehavior extends CBehavior | |
* the database (after find or refresh). | ||
* This option may be disabled in order to achieve better performance. | ||
* For example, in case of [[\yii\db\ActiveRecord]] usage, typecasting after find | ||
* will grant no benefit in most cases an thus can be disabled. | ||
* will grant no benefit in most cases and thus can be disabled. | ||
* Note that changing this option value will have no effect after this behavior has been attached to the model. | ||
*/ | ||
public $typecastAfterFind = false; | ||
public $typecastAfterFind = true; | ||
|
||
/** | ||
* @var array internal static cache for auto detected [[attributeTypes]] values | ||
|
@@ -250,9 +252,9 @@ public function events(): array | |
|
||
/** | ||
* Handles owner 'afterValidate' event, ensuring attribute typecasting. | ||
* @param \CModelEvent $event event instance. | ||
* @param \CEvent $event event instance. | ||
*/ | ||
public function afterValidate(CModelEvent $event): void | ||
public function afterValidate(CEvent $event): void | ||
{ | ||
if (!$this->owner->hasErrors()) { | ||
$this->typecastAttributes(); | ||
|
@@ -270,18 +272,18 @@ public function beforeSave(CModelEvent $event): void | |
|
||
/** | ||
* Handles owner 'afterSave' event, ensuring attribute typecasting. | ||
* @param \CModelEvent $event event instance. | ||
* @param \CEvent $event event instance. | ||
*/ | ||
public function afterSave(CModelEvent $event): void | ||
public function afterSave(CEvent $event): void | ||
{ | ||
$this->typecastAttributes(); | ||
} | ||
|
||
/** | ||
* Handles owner 'afterFind' event, ensuring attribute typecasting. | ||
* @param \CModelEvent $event event instance. | ||
* @param \CEvent $event event instance. | ||
*/ | ||
public function afterFind(CModelEvent $event): void | ||
public function afterFind(CEvent $event): void | ||
{ | ||
$this->typecastAttributes(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace yii1tech\model\typecast\test\data; | ||
|
||
use yii1tech\model\typecast\AttributeTypecastBehavior; | ||
|
||
/** | ||
* @mixin \yii1tech\model\typecast\AttributeTypecastBehavior | ||
* | ||
* @property-read \yii1tech\model\typecast\AttributeTypecastBehavior $typecastBehavior | ||
*/ | ||
class ItemWithTypecast extends Item | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function model($className = __CLASS__) | ||
{ | ||
return parent::model($className); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function behaviors(): array | ||
{ | ||
return [ | ||
'typecastBehavior' => [ | ||
'class' => AttributeTypecastBehavior::class, | ||
'typecastBeforeSave' => true, | ||
'typecastAfterFind' => true, | ||
'attributeTypes' => [ | ||
'name' => AttributeTypecastBehavior::TYPE_STRING, | ||
'category_id' => AttributeTypecastBehavior::TYPE_INTEGER, | ||
'price' => AttributeTypecastBehavior::TYPE_FLOAT, | ||
'is_active' => AttributeTypecastBehavior::TYPE_BOOLEAN, | ||
'callback' => function ($value) { | ||
return 'callback: ' . $value; | ||
}, | ||
], | ||
], | ||
]; | ||
} | ||
} |