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

4.1.0: Can't use findFirst with column specifier

Good evening,

after upgrading to 4.1.0 on Windows 10 all my applications stopped working. When approaching the site(s), I receive this kind of error:

Fatal error: Declaration of BLABLAMODEL::findFirst($parameters = NULL) must be compatible with Phalcon\Mvc\Model::findFirst($parameters = NULL): ?Phalcon\Mvc\ModelInterface in D:\xampp744\htdocs\app\models\BLABLAMODEL.php on line 151

I solved this by changing

public static function findFirst($parameters = null)
{
    return parent::findFirst($parameters);
}

to

public static function findFirst($parameters = null): \Phalcon\Mvc\ModelInterface
{
    return parent::findFirst($parameters);
}

This seems to work so far, but if my findFirst() contains an array of options which limits the columns returned, like

$article = SupplierUpload::findFirst([
    'conditions' => 'article = '.$tmp[1],
    'columns'    => 'article, dispo, season, price'
]);

I get this error

Fatal error: Uncaught TypeError: Return value of SupplierUpload::findFirst() must implement interface Phalcon\Mvc\ModelInterface, instance of Phalcon\Mvc\Model\Row returned in D:\xampp744\htdocs\app\models\SupplierUpload.php on line 90

If I get this right, the internal Model::findFirst should always return a \Phalcon\Mvc\ModelInterface, but with limited columns, it returns a \Phalcon\Mvc\Model\Row? Or did I fix the findFirst declaration the wrong way?

Thank you!

edited Nov '20

Hello, declaration of your extended findFirst() must be will NULL option

use \Phalcon\Mvc\ModelInterface;

public static function findFirst($parameters = null): ?ModelInterface

Reference https://github.com/phalcon/cphalcon/blob/master/phalcon/Mvc/ModelInterface.zep#L103



4.0k

Hello, declaration of your extended findFirst() must be will NULL option

use \Phalcon\Mvc\ModelInterface;

public static function findFirst($parameters = null): ?ModelInterface

Reference https://github.com/phalcon/cphalcon/blob/master/phalcon/Mvc/ModelInterface.zep#L103

Same error, except that findFirst can return NULL now. If using findFirst with column specifier, it still returns Phalcon\Mvc\Model\Row.



8.4k
Accepted
answer

if you are not doing anything with findFirst in your models just remove it from the model it will be inherited anyway from parent class Phalcon\Mvc\Model



8.4k

and i noticed there is two different files D:\xampp744\htdocs\app\models\BLABLAMODEL.php and D:\xampp744\htdocs\app\models\SupplierUpload.php

change these two files as Anton Vasiliev said

the error is clear if you override methods they have to be compatible with the parent class and it seems that the last error you mentioned that the method returns an instance Phalcon\Mvc\Model\Row that couldn't be judged until you show us the code of the two files where errors occur



4.0k

if you are not doing anything with findFirst in your models just remove it from the model it will be inherited anyway from parent class Phalcon\Mvc\Model

This did the trick. Somehow the inheritance messed up there, so thank you for pointing this out.

and i noticed there is two different files D:\xampp744\htdocs\app\models\BLABLAMODEL.php and D:\xampp744\htdocs\app\models\SupplierUpload.php

change these two files as Anton Vasiliev said

the error is clear if you override methods they have to be compatible with the parent class and it seems that the last error you mentioned that the method returns an instance Phalcon\Mvc\Model\Row that couldn't be judged until you show us the code of the two files where errors occur

The different naming was just a mistake by me; in reality, those models and files have other names, but I prefer to anonymize the names I use in different projects.