How to find by multiple criteria with Phalcon findFirst?

13,296

Solution 1

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

Solution 2

Both find() and findFirst() methods accept an associative array specifying the search criteria:

$robot = Robots::findFirst(array(
    "type = 'virtual'",
    "order" => "name DESC",
    "limit" => 30
));

$robots = Robots::find(array(
    "conditions" => "type = ?1",
    "bind"       => array(1 => "virtual")
));


// What's the first robot in robots table?
$robot = Robots::findFirst();
echo "The robot name is ", $robot->name, "\n";

// What's the first mechanical robot in robots table?
$robot = Robots::findFirst("type = 'mechanical'");
echo "The first mechanical robot name is ", $robot->name, "\n";

// Get first virtual robot ordered by name
$robot = Robots::findFirst(array("type = 'virtual'", "order" => "name"));
echo "The first virtual robot name is ", $robot->name, "\n";

continue reading here: Main Doc - Finding Records

Share:
13,296
KBoek
Author by

KBoek

Professional IPTV/webTV Consultant, Solution Architect and Developer

Updated on June 06, 2022

Comments

  • KBoek
    KBoek almost 2 years

    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?

  • KBoek
    KBoek almost 10 years
    Found it. For some reason the external_id was set to UNIQUE in the database, cause errors on any new inserts with the same external_id, but a different language id. Thanks for you help though.