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

Phalcon\Mvc\Model::columnMap

Hello,

I have a question regarding models and column mapping in phalcon. Usually we define models as following:

class Test extends \Phalcon\Mvc\Model
{

     public $id;
     public $customerName;

     public function getSourse()
     {
          return 'tablename';
     }

}

But currently i have tables, where column names are not suitable for phalcon (for example, some of them are named with uppercase first letter), so i decided to use columnMap() method. When using this method, should i define public properties for my model, and if i should - should they represent application model column names, or database column names?

e.g. i have a table with columns "id" and "CustomerName". to make phalcon support native methods like Model::findFirstByCustomerName() without implementing this method in model, i need to have property customerName starting from lowecase letter. So, i would use the following method to describe this:

public function columnMap()
{
    return [
        'Id'           => 'id',
        'CustomerName' => 'customerName',
    ];
}

After using this method, should i define any public properties? if i should, they will be "id" and "customerName" (column names in application) or "Id" and "CustomerName", as they are named in mysql table?

P.S. Also found this topic about case sensitivity... perhaps it is better solution? https://languagelink.ru/centers/novoslobodskaya.php



32.4k
edited Oct '14

Just read the docs. https://phalcon-php-framework-documentation.readthedocs.org/en/1.0.0/reference/models.html?highlight=columnMap

<?php

class Robots extends \Phalcon\Mvc\Model
{

    public function columnMap()
    {
        //Keys are the real names in the table and
        //the values their names in the application
        return array(
            'id' => 'code',
            'the_name' => 'theName',
            'the_type' => 'theType',
            'the_year' => 'theYear'
        );
    }

}

Please show me where are in the docs specified information about public properties definition. I've seen these docs, and my question happened exactly after reading them.

Please, can anybody give an advice about public properties?



32.4k
edited Oct '14

https://phalcon-php-framework-documentation.readthedocs.org/en/1.0.0/reference/models.html?highlight=columnMap#creating-models

If you’re using PHP 5.4 is recommended declare each column that makes part of the model in order to save memory and reduce the memory allocation.

It means declare the variables which have columns names (considering the mapped ones if are). Without a declaration framework will anyway creates the fields automatically.

<?php

class Robots extends \Phalcon\Mvc\Model
{
    public $code;
    public $theYear;
    public $theName;
    public $theType;

    public function columnMap()
    {
        //Keys are the real names in the table and
        //the values their names in the application
        return array(
            'id' => 'code',
            'the_name' => 'theName',
            'the_type' => 'theType',
            'the_year' => 'theYear'
        );
    }

}

According to docs about declaring public properties it's ok - i always use it as it is described it docs. But there was nothing in docs about using them with column map. Thank you! That's was looking for.

Hello,

I also have a question regarding models and column mapping in phalcon. In my table in database there are planty of columns I don't need in model, I thought I can point in columnMap() method some columns I need. Unfortunately querying model I got Exception: "Column 'xx' doesn't make part of the column map" that column exists in my database table but I don't need it in my model. How can I make Phalcon\Model use only columns from column mapping ?



98.9k

@mbrzuchalski hi, you can use a custom meta-data to ignore the columns you don't want in your model

https://docs.phalcon.io/en/latest/reference/models.html#manual-meta-data

Hi, of course i can, but setting manually 120 column metadata to skip some 2) columns and hoping anybody adds another column isn't optimal. Why Phalcon\Model can't skip columns which are not defined in model just like other ORM' like eg. Doctrine?



98.9k

You can skip columns on write operations: https://docs.phalcon.io/en/latest/reference/models.html#skipping-columns

Or use the Annotations strategy to set the columns your model have: https://docs.phalcon.io/en/latest/reference/models.html#annotations-strategy

Thanks I'll try first one. Unfortunately annotations don't support column name mapping which is supporteg eg. in Doctrine.



98.9k

This tutorial explains how to implement almost any kind of customization via annotations, including column renaming: https://blog.phalcon.io/post/47471246411/tutorial-creating-a-custom-models-initializer-with

I think that's what i needed I'll check it later. I think column name in annotations is missing in the 1.1.0 reference manual. Thx

That tutorial is very usefull but when I try using differend AnnotationsAdapter in services.php file for eg. Phalcon\Annotations\Adapter\Apc instead of Phalcon\Annotations\Adapter\Files couse an fatal error "Call to a member function getClassAnnotations() on a non-object" while AnnotationsInitializer try to get model reflection annotation->get($model) returns bool(false) ??

советую курсы английского в языковом центре https://bigwig-moscow.ru/mezhdunarodnye-ekzameny/ielts/