Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
[solved] data_types is not being casted on the values of to_array()
  • Update:  this issue is solved by Observer_typing




    I'll be working on a custom patch extending the Orm\Model but i just wanted to mention it , so you might consider a solution on a future release.


    The issue is generated on this block
    https://github.com/fuel/orm/blob/1.8/develop/classes/model.php#L2052-L2066

    I wanna use the array returned from static::properties() to see if a defined datatype as shown
    http://fuelphp.com/docs/packages/orm/creating_models.html#/propperties
    can be used for casting its value when invoking the to_array() method.

    The issue i was having was this, lets say for the model below, the method $instance->to_array()  would return created_at and updated_at as strings even if it was defined as int.



    class Model_Article extends Orm\Model
    {
    protected static $_properties = array(
    'id', // both validation & typing observers will ignore the PK
    'name' => array(
    'data_type' => 'varchar',
    'label' => 'Article Name',
    'validation' => array('required', 'min_length' => array(3), 'max_length' => array(20)),
    'form' => array('type' => 'text'),
    'default' => 'New article',
    ),
    'gender' => array(
    'data_type' => 'varchar',
    'label' => 'Gender',
    'form' => array('type' => 'select', 'options' => array('m' => 'Male', 'f' => 'Female')),
    'validation' => array('required'),
    ),
    'created_at' => array(
    'data_type' => 'int',
    'label' => 'Created At',
    'form' => array(
    'type' => false, // this prevents this field from being rendered on a form
    ),
    ),
    'updated_at' => array('data_type' => 'int', 'label' => 'Updated At')
    );
    }


    I noticed this because i've an orm observer  on a user model triggered on after_insert and after_update events that inserts/updates a mongodb , and the method where_lte was fetching weird results because it was indexed as a string

    $mongodb = \Mongo_Db::instance();

    $insert_id = $mongodb->insert('users', $model->to_array()));


    $mongodb = \Mongo_Db::instance();
    $mongodb->where_lt('created_at', time());
    $users = $mongodb->get('users');


Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!