We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Creating new column types

Is there a way to create new column type? So like there is e.g. string, integer, decimal, boolean, I would like to also have type json, so column in table would be string, but when I am saving or getting data, it would straight away encoded or decoded by json, so I would be already working on array / object.



98.9k

You can override and replace this method to support the custom types you are needing. https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/db/adapter/pdo/mysql.zep#L87



6.1k

Ok, I can create here custom types, so e.g. if I will use type 'json' then in db this field will be type of text. But I dont see that I could put in here code which would actually do json_decode when getting fields with type 'json' and json_encode when saving fields with type 'json'. I would say that I would like something similar to this https://github.com/phalcon/incubator/blob/1.3.0/Library/Phalcon/Mvc/Model/Behavior/DateTime.php but with that difference it would automatically applied to all fields with that type...

not by initialize on every model, like

public function initialize()
{
    $options = array(
       'timezone' => 'Europe/Belgrade'
    );
    $this->addBehavior(
        new \Phalcon\Mvc\Model\Behavior\DateTime(array(
            'createdAt' => $options,
            'updatedAt' => $options
        ));
    );
}

and also this is just on save and update, but I need also on retreive data from db.

edited Oct '15

I'm searching for things like that too. I got a spartial Point in my table (which is mysql default) but i can not represent it without building my own sql. Custom data types would be a good thing to transform models data into database structure.

In my case I got a class called Point which got a toString function which renders then 'ST_GeomFromText(\'POINT(' . $this->longitude . ' ' . $this->latitude . ')\', ' . SRID . ')' So the saving with this datatype works. But to read it the sql query has to contain x and y separately like SELECT X(location) as lat, Y(location) as lon FROM table;