How to find by multiple criteria with Phalcon findFirst?

…衆ロ難τιáo~ 提交于 2019-12-06 05:36:04

问题


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?


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



回答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



来源:https://stackoverflow.com/questions/24311249/how-to-find-by-multiple-criteria-with-phalcon-findfirst

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!