PHP code example of yii1tech / model-typecast

1. Go to this page and download the library: Download yii1tech/model-typecast library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yii1tech / model-typecast example snippets




use yii1tech\model\typecast\TypecastBehavior;

class Item extends CActiveRecord
{
    public function behaviors()
    {
        return [
            'typecastBehavior' => [
                'class' => TypecastBehavior::class,
                'attributeTypes' => [
                    'id' => TypecastBehavior::TYPE_INTEGER,
                    'amount' => TypecastBehavior::TYPE_INTEGER,
                    'price' => TypecastBehavior::TYPE_FLOAT,
                    'is_active' => TypecastBehavior::TYPE_BOOLEAN,
                    'created_at' => TypecastBehavior::TYPE_DATETIME,
                    'json_data' => TypecastBehavior::TYPE_ARRAY_OBJECT,
                ],
                'typecastAfterValidate' => true,
                'typecastBeforeSave' => false,
                'typecastAfterSave' => true,
                'typecastAfterFind' => true,
            ],
        ];
    }

    // ...
}



$model = new Item();
$model->setAttributes([
    'name' => 'item name',
    'price' => '10.50',
    'amount' => '14',
    'is_active' => '1',
]);

if ($model->validate()) {
    var_dump($model->id); // outputs: int(123456)
    var_dump($model->price); // outputs: float(10.5)
    var_dump($model->amount); // outputs: int(14)
    var_dump($model->is_active); // outputs: bool(true)
}

$model = Item::model()->findByPk($id);
var_dump($model->id); // outputs: int(12345)
var_dump($model->amount); // outputs: int(18)
var_dump($model->is_active); // outputs: bool(true)



$model = new Item();
$model->price = '38.5';
$model->is_active = 1;
$model->typecastAttributes();

var_dump($model->price); // outputs: float(38.5)
var_dump($model->is_active); // outputs: bool(true)



$model = new Item();
$model->json_data = [ // will be saved in DB as '{foo: "bar"}'
    'foo' => 'bar',
];
$model->save();



// in case mapping in `attributeTypes` is set to `TypecastBehavior::TYPE_ARRAY`
use yii1tech\model\typecast\TypecastBehavior;

class Item extends CActiveRecord
{
    public function behaviors()
    {
        return [
            'typecastBehavior' => [
                'class' => TypecastBehavior::class,
                'attributeTypes' => [
                    'json_data' => TypecastBehavior::TYPE_ARRAY,
                ],
            ],
        ];
    }

    // ...
}

$model = Item::model()->findByPk($id);
var_dump($model->json_data); // outputs: array(1) {...}
var_dump($model->json_data['foo']); // outputs: string(bar)
$model->json_data['foo'] = 'new value'; // PHP E_NOTICE: Indirect modification of overloaded property Item::$json_data has no effect!
$model->json_data = [ // no problem
    'foo' => 'new value',
];
$model->save();

// in case mapping in `attributeTypes` is set to `TypecastBehavior::TYPE_ARRAY_OBJECT`
use yii1tech\model\typecast\TypecastBehavior;

class Item extends CActiveRecord
{
    public function behaviors()
    {
        return [
            'typecastBehavior' => [
                'class' => TypecastBehavior::class,
                'attributeTypes' => [
                    'json_data' => TypecastBehavior::TYPE_ARRAY_OBJECT,
                ],
            ],
        ];
    }

    // ...
}

$model = Item::model()->findByPk($id);
var_dump($model->json_data); // outputs: object(ArrayObject) {...}
var_dump($model->json_data['foo']); // outputs: string(bar)
$model->json_data['foo'] = 'new value'; // no problem
$jsonDataCopy = $model->json_data; // new variable holds the reference to `\ArrayObject` instance!
$jsonDataCopy['foo'] = 'value from copy'; // changes value of `$model->json_data`!
var_dump($model->json_data['foo']); // outputs: string(value from copy)
$model->save();



use yii1tech\model\typecast\TypecastBehavior;

class Item extends CActiveRecord
{
    public function behaviors()
    {
        return [
            'typecastBehavior' => [
                'class' => TypecastBehavior::class,
                'attributeTypes' => [
                    'created_at' => TypecastBehavior::TYPE_DATETIME,
                ],
            ],
        ];
    }

    // ...
}

$model = new Item();
$model->created_at = new DateTime('now'); // will be saved in DB as '2023-12-22 10:14:17'
$model->save();

$model = Item::model()->findByPk($id);
var_dump($model->created_at); // outputs: object(DateTime)



use yii1tech\model\typecast\TypecastBehavior;

class Item extends CActiveRecord
{
    public function behaviors()
    {
        return [
            'typecastBehavior' => [
                'class' => TypecastBehavior::class,
                'attributeTypes' => [
                    'created_at' => TypecastBehavior::TYPE_TIMESTAMP,
                ],
            ],
        ];
    }

    // ...
}

$model = new Item();
$model->created_at = new DateTime('now'); // will be saved in DB as '1703257478'
$model->save();

$model = Item::model()->findByPk($id);
var_dump($model->created_at); // outputs: object(DateTime)



use yii1tech\model\typecast\TypecastBehavior;

class Item extends CActiveRecord
{
    public function behaviors()
    {
        return [
            'typecastBehavior' => [
                'class' => TypecastBehavior::class,
                'attributeTypes' => [
                    'heap_data' => function ($value) {
                        if (is_object($value)) {
                            return $value;
                        }
                        
                        $heap = new \SplMaxHeap();
                        foreach (json_decode($value) as $element) {
                            $heap->insert($element);
                        }
                        
                        return $heap;
                    },
                ],
            ],
        ];
    }

    // ...
}

$model = Item::model()->findByPk($id);
var_dump($model->heap_data); // outputs: object(SplMaxHeap)