cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

\Phalcon\Mvc\Model\Validator\Uniqueness with namespaces

Open nesbert opened this issue 14 years ago • 4 comments

Hey Guys,

I am get the following Phalcon\Db\Exception below when using \Phalcon\Mvc\Model\Validator\Uniqueness validator with a model that is a namespace. Looks like validator class is not using getSource method from the Model when building the supporting query.

Thanks!


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near app\dao\users  WHERE username = 'nhidalgo' AND id <> ''' at line 1 when executing SELECT COUNT(*) AS rowcount FROM app\dao\users  WHERE username = 'nhidalgo' AND id <> ''
<?php
namespace App\Dao;

use Phalcon\Mvc\Model\Validator\Inclusionin;
use Phalcon\Mvc\Model\Validator\Uniqueness;

class Users extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->setSource('users');
    }

    public function validation()
    {
        $this->validate(new Uniqueness(
            array(
                "field"   => "username",
                "message" => "Username must be unique"
            )
        ));
        $this->validate(new Uniqueness(
            array(
                "field"   => "email",
                "message" => "Email must be unique"
            )
        ));

        return !$this->validationHasFailed();
    }
}

nesbert avatar Sep 17 '12 19:09 nesbert

I think the documentation stated that if you use namespaces you MUST implement the method that returns the table name? I dont recall the specific name but do vaguely remember reading this?

nesbert mailto:[email protected] September 17, 2012 12:45 PM

Hey Guys,

I am get the following Phalcon\Db\Exception below when using \Phalcon\Mvc\Model\Validator\Uniqueness validator with a model that is a namespace. Looks like validator class is not using getSource method from the Model when building the supporting query.

Thanks!


|You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near app\dao\users WHERE username = 'nhidalgo' AND id <> ''' at line 1 when executing SELECT COUNT(*) AS rowcount FROM app\dao\users WHERE username = 'nhidalgo' AND id <> '' |

setSource('users'); } public function validation() { $this->validate(new Uniqueness( array( "field" => "username", "message" => "Username must be unique" ) )); $this->validate(new Uniqueness( array( "field" => "email", "message" => "Email must be unique" ) )); return !$this->validationHasFailed(); } } — Reply to this email directly or view it on GitHub https://github.com/phalcon/cphalcon/issues/92.

brandonlamb avatar Sep 17 '12 19:09 brandonlamb

Thanks @brandonlamb I added the code below and all seems to work. I thought setting $this->setSource('users'); in initialize() routine would cause the same effect. But I guess I was wrong. Let me know if this is not a bug and I will close the issue.

....
    public function getSource()
    {
        return 'users';
    }

nesbert avatar Sep 17 '12 19:09 nesbert

HI Nesbert, brandon is right, setSource only changes the mapped table for a single instance, while implementing getSource makes the source available for every instance created.

ghost avatar Sep 17 '12 20:09 ghost

As always thanks @phalcon :)

nesbert avatar Sep 17 '12 20:09 nesbert