How to find by multiple criteria with Phalcon findFirst?

KBoek picture KBoek · Jun 19, 2014 · Viewed 12.6k times · Source

I'm trying to get a video from my video database, the selection is based on a unique combination of external_id and language_id (both integers). I tried the following code, but it looks like findFirst() only picks up the first criterium

$video = Video::findFirst("language_id=" . $language->id . " and external_id=" . $external->id);

Can anybody help me how to properly use findFirst with multiple criteria?

Answer

Nikolaos Dimopoulos picture Nikolaos Dimopoulos · Jun 19, 2014

Try binding your parameters vs. concatenating them. Safer and it might identify an error area

$video = Video::findFirst(
    [
        'columns'    => '*',
        'conditions' => 'language_id = ?1 AND external_id = ?2',
        'bind'       => [
            1 => $language->id,
            2 => $external->id,
        ]
    ]
);