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

Model not Loading

I have the following model:

    /**
     * @Primary
     * @Identity
     * @Column(name="person_id", type="integer", nullable=false)
     */
    public $id;

    /**
     * @Column(name="str_name", type="string", length=50, nullable=false)
     */
    public $name;

    /**
     * @Column(name="str_lastName", type="string", length=50, nullable=false)
     */
    public $lastName;

    /**
     * @Column(name="dt_birthday", type="date", length=15, nullable=false)
     */
    public $dBirhtday;

    /**
     * @Column(name="country_id", type="string", length=30, nullable=false)
     */
    public $country;

    ... getters and setters ...

And i have the following code:

$pes = Person::findFirst(1);
echo '<pre>';
var_dump($pes);
echo '</pre>';

When I try to print any getter value, it is null.

What am I doing wrong? Why the model is not being loaded????

Thanks,



4.0k
edited Jul '14

I found out my error.

Is there a way to map via annotations a name for column and a different name for the object property that will hold the column data like is done in Hibernate?

/**
 * @Column(name="str_name", type="string", length=50, nullable=false)
 */
public $name;


26.3k
Accepted
answer

Yes, there is columnMap() method.


<?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'
        );
    }

}

Check this docs: https://docs.phalcon.io/en/latest/reference/models.html#independent-column-mapping.



4.0k
edited Jul '14

I found it just after I posted.

But is there any a way to achive the same result via Column annotation? For example:

/**
 * @Column(name="str_name", type="string", length=50, nullable=false)
 */
public $name;