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

FindFirst causes Phalcon Error if the value is not in the database

Hello,

Im a beginner with Phalcon. When i use findFirst with a value wich is in the database everything is okay but when i choose a value like test the view will not be loaded.

$Key = (trim($this->request->get("key")));
$event = Event::findFirst(
                [
                    "conditions" => " passkey =' " . $Key . " ' ",
                ]
            );

Do you have any idea.

When I search for id's the Problem does not occur like in this example.

$event = Event::findFirst(
                [
                    "conditions" => " id = " . $Key,
                ]
            );


145.0k
Accepted
answer

First you need to use bind params because right now your code can be sql injected.

Also remove space. It should be like this:

Event::findFirst([
    'conditions' => 'passkey = :passKey:',
    'bind' => [
        'passKey' => $Key
    ]
]);

What exactly error you have? You didn't provide anything.



1.1k

First thanks a lot for the tip!

I do not get an error message in the apache log. The problem is when I use a value for $Key which is not in the database the view I get is the basic index Layout and when I choose a value which is in the database the right view will be shown.

Yea but when you use it then phalcon returns false, and in view you need to check if you have not empty value - then use model or better just return 404 if value not found.



1.1k

Thanks to you! The problem was wrong validating of the returned value. It works now!