Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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,
                ]
            );


133.2k
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.

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.

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