Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find by multiple criteria with Phalcon findFirst?

Tags:

php

mysql

phalcon

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?

like image 539
KBoek Avatar asked Jun 19 '14 16:06

KBoek


2 Answers

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,
        ]
    ]
);
like image 148
Nikolaos Dimopoulos Avatar answered Sep 21 '22 00:09

Nikolaos Dimopoulos


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

like image 37
Lukas Liesis Avatar answered Sep 23 '22 00:09

Lukas Liesis