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 validation 'Wrong number of parameters'

I am having a bit of an issue with the new validator on CLI Task, probably I am doing something wrong.

php 5.5.9, phalcon 3, Xubuntu 14.04, package manager instalation

Line 124 is return $this->validate($validator);

   public function validation()
    {
        $validator = new \Phalcon\Validation();
        $validator->add(
            ['RELATED', 'RELATED_ID', 'bin_key64'],
            new \Phalcon\Validation\Validator\Uniqueness()
        );
        return $this->validate($validator);
    }

CLI error is has follows

PHP Fatal error:  Uncaught exception 'BadMethodCallException' with message 'Wrong number of parameters' in /var/www/v2/app/models/Files.php:124
Stack trace:
#0 [internal function]: Phalcon\Mvc\Model->getRelated()
#1 [internal function]: Phalcon\Validation->getValue('RELATED')
#2 [internal function]: Phalcon\Validation\Validator\Uniqueness->isUniqueness(Object(Phalcon\Validation), Array)
#3 [internal function]: Phalcon\Validation\Validator\Uniqueness->validate(Object(Phalcon\Validation), Array)
#4 [internal function]: Phalcon\Validation->validate(NULL, Object(Files))
#5 /var/www/v2/app/models/Files.php(124): Phalcon\Mvc\Model->validate(Object(Phalcon\Validation))
#6 [internal function]: Files->validation()
#7 [internal function]: Phalcon\Mvc\Model->fireEventCancel('validation')
#8 [internal function]: Phalcon\Mvc\Model->_preSave(Object(Phalcon\Mvc\Model\MetaData\Memory), false, 'id')
#9 /var/www/v2/app/tasks/FileTask.php(346): Phalcon\Mvc\Model->save()
#10 /var/www/v2/app/tasks/File in /var/www/apqe/v2/app/models/Files.php on line 124

Hapens also with php 7

Is RELATED a relation like belongsTo or hasMany or any other alias ? Then it won't work.

edited Aug '16

It's not a relation, ['RELATED', 'RELATED_ID', 'bin_key64'] are properties of the model and fields of the table.



145.0k
Accepted
answer
edited Aug '16

Oh i know why it's executed https://github.com/phalcon/cphalcon/blob/master/phalcon/validation.zep#L489 Beacause it's looking for getRelated() method and obviously such method exists from Phalcon\Mvc\Model, you need to change name of this for now, create issue on github maybe so we will add underscore before getRelated but this will be change in phalcon 4.

So Related is a reserved 'propertie' in models? because when phalcon tries to call getRealated it uses the magic method __get()?

edited Aug '16

No no no. Validation is trying to use getter. And obviously there is such a method as getRelated in model(Phalcon\Mvc\Model::getRelated().

But this means yes - related is reserved property name.

I just tested it works if I do not name a propertie 'related'. yes I know there is such a method, been reading the code all nigh ^^

but the model magic method is running 1st thus generating the error.

So if 'related' is not considered a "reserved word" maybe a reference in the manual that such problems can occour?

No it's nor running first. First validation checks for getters, then other things. Check link from source i posted.

Oh ok got it, Will create issue on Github, you are right maybe the underscore will solve the issue on phalcon4, but it's a public ...

Have you guys ever considered to implement a DataMapper ORM?

Well it's huge work to do.

Yes I know, I did try to implement one of my own in pure php, nonsense when you have zephir.

Please dont get me wrong, my idea is not to change the hole ORM to DataMapper and ditch ActiveRecord, my idea is to have both options and offer a alternative for those who use for example Doctrine2.