Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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,



2.5k
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;


16.8k
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: http://docs.phalconphp.com/en/latest/reference/models.html#independent-column-mapping.



2.5k
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;