我正在尝试从我的视频数据库中获取视频,选择基于external_id
和language_id
(两个整数)的唯一组合。我尝试了以下代码,但看起来只有findFirst()
才能获得第一个标准
$video = Video::findFirst("language_id=" . $language->id . " and external_id=" . $external->id);
有人可以帮助我如何正确使用具有多个标准的findFirst吗?
答案 0 :(得分:12)
尝试绑定参数与连接它们。更安全,它可能会识别错误区域
$video = Video::findFirst(
[
'columns' => '*',
'conditions' => 'language_id = ?1 AND external_id = ?2',
'bind' => [
1 => $language->id,
2 => $external->id,
]
]
);
答案 1 :(得分:4)
find()和findFirst()方法都接受指定搜索条件的关联数组:
$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";