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

No automatic type validation in ORM ?

Hello!

I have a model called User in Phalcon that has a variable id with the annotation @Column(type="integer", nullable=false) .

If i try to find a User by Id using as param a string, it will try a PDOException instead of autovalidating the expected type.

Example:

$user = User::findFirstById('This is a String');

It throws SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer

So, my question is: What is the use of the "type" annotation ? I tought it would automaticaly add type validation before queries.

Also, is there any way for me to add a validation INSIDE THE MODEL that would validate the columns before a SELECT query ?

Thank you



98.9k
edited Aug '14

It seems it's expecting a bind type to be passed in this case:


use Phalcon\Db\Column;

//...

$user = User::findFirst(['id = ?0', ['bind' => ['This is a String'], 'bindTypes' => Column::BIND_PARAM_INT]]);

You probably want to write this as a shortcut in your model:

class User extends Phalcon\Mvc\Model
{
    public static function findFirstById($value)
    {
        return self::findFirst(['id = ?0', ['bind' => $value, 'bindTypes' => Column::BIND_PARAM_INT]]);
    }
}