PHP 클래스 yii\behaviors\AttributeTypecastBehavior

This behavior is very useful in case of usage of ActiveRecord for the schema-less databases like MongoDB or Redis. It may also come in handy for regular ActiveRecord or even Model, allowing to maintain strict attribute types after model validation. This behavior should be attached to Model or BaseActiveRecord descendant. You should specify exact attribute types via [[attributeTypes]]. For example: php use yii\behaviors\AttributeTypecastBehavior; class Item extends \yii\db\ActiveRecord { public function behaviors() { return [ 'typecast' => [ 'class' => AttributeTypecastBehavior::className(), 'attributeTypes' => [ 'amount' => AttributeTypecastBehavior::TYPE_INTEGER, 'price' => AttributeTypecastBehavior::TYPE_FLOAT, 'is_active' => AttributeTypecastBehavior::TYPE_BOOLEAN, ], 'typecastAfterValidate' => true, 'typecastBeforeSave' => false, 'typecastAfterFind' => false, ], ]; } ... } Tip: you may left [[attributeTypes]] blank - in this case its value will be detected automatically based on owner validation rules. Following example will automatically create same [[attributeTypes]] value as it was configured at the above one: php use yii\behaviors\AttributeTypecastBehavior; class Item extends \yii\db\ActiveRecord { public function rules() { return [ ['amount', 'integer'], ['price', 'number'], ['is_active', 'boolean'], ]; } public function behaviors() { return [ 'typecast' => [ 'class' => AttributeTypecastBehavior::className(), 'attributeTypes' will be composed automatically according to rules() ], ]; } ... } This behavior allows automatic attribute typecasting at following cases: - after successful model validation - before model save (insert or update) - after model find (found by query or refreshed) You may control automatic typecasting for particular case using fields [[typecastAfterValidate]], [[typecastBeforeSave]] and [[typecastAfterFind]]. By default typecasting will be performed only after model validation. Note: you can manually trigger attribute typecasting anytime invoking AttributeTypecastBehavior::typecastAttributes method: php $model = new Item(); $model->price = '38.5'; $model->is_active = 1; $model->typecastAttributes();
부터: 2.0.10
저자: Paul Klimov ([email protected])
상속: extends yii\base\Behavior
파일 보기 프로젝트 열기: yiisoft/yii2 1 사용 예제들

공개 프로퍼티들

프로퍼티 타입 설명
$attributeTypes attribute typecast map in format: attributeName => type. Type can be set via PHP callable, which accept raw value as an argument and should return typecast result. For example: php [ 'amount' => 'integer', 'price' => 'float', 'is_active' => 'boolean', 'date' => function ($value) { return ($value instanceof \DateTime) ? $value->getTimestamp(): (int)$value; }, ] If not set, attribute type map will be composed automatically from the owner validation rules.
$owner the owner of this behavior.
$skipOnNull whether to skip typecasting of null values. If enabled attribute value which equals to null will not be type-casted (e.g. null remains null), otherwise it will be converted according to the type configured at [[attributeTypes]].
$typecastAfterFind whether to perform typecasting after retrieving owner model data from the database (after find or refresh). This option may be disabled in order to achieve better performance. For example, in case of ActiveRecord usage, typecasting after find will grant no benefit in most cases an thus can be disabled. Note that changing this option value will have no effect after this behavior has been attached to the model.
$typecastAfterValidate whether to perform typecasting after owner model validation. Note that typecasting will be performed only if validation was successful, e.g. owner model has no errors. Note that changing this option value will have no effect after this behavior has been attached to the model.
$typecastBeforeSave whether to perform typecasting before saving owner model (insert or update). This option may be disabled in order to achieve better performance. For example, in case of ActiveRecord usage, typecasting before save will grant no benefit an thus can be disabled. Note that changing this option value will have no effect after this behavior has been attached to the model.

공개 메소드들

메소드 설명
afterFind ( Event $event ) Handles owner 'afterFind' event, ensuring attribute typecasting.
afterValidate ( Event $event ) Handles owner 'afterValidate' event, ensuring attribute typecasting.
attach ( $owner )
beforeSave ( Event $event ) Handles owner 'afterInsert' and 'afterUpdate' events, ensuring attribute typecasting.
clearAutoDetectedAttributeTypes ( ) Clears internal static cache of auto detected [[attributeTypes]] values over all affected owner classes.
events ( )
typecastAttributes ( array $attributeNames = null ) Typecast owner attributes according to [[attributeTypes]].

보호된 메소드들

메소드 설명
detectAttributeTypes ( ) : array Composes default value for [[attributeTypes]] from the owner validation rules.
typecastValue ( mixed $value, string | callable $type ) : mixed Casts the given value to the specified type.

메소드 상세

afterFind() 공개 메소드

Handles owner 'afterFind' event, ensuring attribute typecasting.
public afterFind ( Event $event )
$event yii\base\Event event instance.

afterValidate() 공개 메소드

Handles owner 'afterValidate' event, ensuring attribute typecasting.
public afterValidate ( Event $event )
$event yii\base\Event event instance.

attach() 공개 메소드

public attach ( $owner )

beforeSave() 공개 메소드

Handles owner 'afterInsert' and 'afterUpdate' events, ensuring attribute typecasting.
public beforeSave ( Event $event )
$event yii\base\Event event instance.

clearAutoDetectedAttributeTypes() 공개 정적인 메소드

Clears internal static cache of auto detected [[attributeTypes]] values over all affected owner classes.

detectAttributeTypes() 보호된 메소드

Composes default value for [[attributeTypes]] from the owner validation rules.
protected detectAttributeTypes ( ) : array
리턴 array attribute type map.

events() 공개 메소드

public events ( )

typecastAttributes() 공개 메소드

Typecast owner attributes according to [[attributeTypes]].
public typecastAttributes ( array $attributeNames = null )
$attributeNames array list of attribute names that should be type-casted. If this parameter is empty, it means any attribute listed in the [[attributeTypes]] should be type-casted.

typecastValue() 보호된 메소드

Casts the given value to the specified type.
protected typecastValue ( mixed $value, string | callable $type ) : mixed
$value mixed value to be type-casted.
$type string | callable type name or typecast callable.
리턴 mixed typecast result.

프로퍼티 상세

$attributeTypes 공개적으로 프로퍼티

attribute typecast map in format: attributeName => type. Type can be set via PHP callable, which accept raw value as an argument and should return typecast result. For example: php [ 'amount' => 'integer', 'price' => 'float', 'is_active' => 'boolean', 'date' => function ($value) { return ($value instanceof \DateTime) ? $value->getTimestamp(): (int)$value; }, ] If not set, attribute type map will be composed automatically from the owner validation rules.
public $attributeTypes

$owner 공개적으로 프로퍼티

the owner of this behavior.
public $owner

$skipOnNull 공개적으로 프로퍼티

whether to skip typecasting of null values. If enabled attribute value which equals to null will not be type-casted (e.g. null remains null), otherwise it will be converted according to the type configured at [[attributeTypes]].
public $skipOnNull

$typecastAfterFind 공개적으로 프로퍼티

whether to perform typecasting after retrieving owner model data from the database (after find or refresh). This option may be disabled in order to achieve better performance. For example, in case of ActiveRecord usage, typecasting after find will grant no benefit in most cases an 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

$typecastAfterValidate 공개적으로 프로퍼티

whether to perform typecasting after owner model validation. Note that typecasting will be performed only if validation was successful, e.g. owner model has no errors. Note that changing this option value will have no effect after this behavior has been attached to the model.
public $typecastAfterValidate

$typecastBeforeSave 공개적으로 프로퍼티

whether to perform typecasting before saving owner model (insert or update). This option may be disabled in order to achieve better performance. For example, in case of ActiveRecord usage, typecasting before save will grant no benefit an thus can be disabled. Note that changing this option value will have no effect after this behavior has been attached to the model.
public $typecastBeforeSave