Download the PHP package yii1tech/model-typecast without Composer
On this page you can find all versions of the php package yii1tech/model-typecast. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package model-typecast
Model Attributes Typecast Extension for Yii 1
This extension provides support for Yii1 Model and ActiveRecord attributes typecast.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the "require" section of your composer.json.
Usage
This extension provides support for Yii1 Model and ActiveRecord automatic attributes typecast.
It is performed via usage of \yii1tech\model\typecast\TypecastBehavior
behavior.
It should be attached to \CModel
or \CActiveRecord
descendant.
For example:
Tip: you may leave
\yii1tech\model\typecast\TypecastBehavior::$attributeTypes
blank - in this case its value will be detected automatically: for ActiveRecord - based on owner DB table schema, for regular model - based validation rules.
In the above example attribute typecasting will be automatically performed in following cases:
- after model successful validation
- after model successful saving
- after model retrieval from Database
For example:
You can manually trigger attribute typecasting anytime invoking \yii1tech\model\typecast\TypecastBehavior::typecastAttributes()
method:
JSON Typecasting
This behavior allows automatic conversion of array or traversable objects into JSON string on model saving. For example:
Note: such conversion will take place even, if there is no direct attribute type specification.
You can typecast JSON column value either to plain array
or \ArrayObject
instance.
Plain arrays consume less memory, but writing of its particular internal keys will not work.
\ArrayObject
allows free operation over internal keys, but you should note that its value always passed by reference.
DateTime Typecasting
This behavior allows automatic conversion of \DateTime
instances into ISO datetime string on model saving.
For example:
In case you store the dates using integer Unix timestamp, you can use \yii1tech\model\typecast\TypecastBehavior::TYPE_TIMESTAMP
for correct
conversion. For example:
This extension also supports nesbot/carbon package.
In order to convert dates to \Carbon\Carbon
you should use following types:
\yii1tech\model\typecast\TypecastBehavior::TYPE_DATETIME_CARBON
\yii1tech\model\typecast\TypecastBehavior::TYPE_TIMESTAMP_CARBON
Custom Typecasting
You may specify any custom typecasting for the attribute using a callable as a type specification at \yii1tech\model\typecast\TypecastBehavior::$attributeTypes
.
For example: