我是CakePHP的新手,我嘗試過搜索,但找不到這個問題的答案。如何在find()中指定範圍cakephp
簡單地說,我希望查詢是這樣的:
SELECT id from posts WHERE id IN (15,18,20);
但我不知道要放什麼東西在find()
通話。
我是CakePHP的新手,我嘗試過搜索,但找不到這個問題的答案。如何在find()中指定範圍cakephp
簡單地說,我希望查詢是這樣的:
SELECT id from posts WHERE id IN (15,18,20);
但我不知道要放什麼東西在find()
通話。
從模型將是這樣的範圍內:
$ids = array(15, 18, 20);
$posts = $this->find('all', array(
'conditions' => array(
'Post.id' => $ids
)
);
條件數組中
,你可以通過'IN'子句中要使用的值數組
從posts控制器
$id_array = array(15, 18, 20);
$this->Post->find('all', array('Post.id' => $id_array));
更多關於這個問題 http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
是真的,我忘了「條件」陣列 – kaklon
$conditions = array("Post.title" => array("First post", "Second post", "Third post")) $this->find(all,array($conditions));
Chk它,如果它的工作。
這在CakePHP聯機手冊http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions中有介紹。只需在您的conditions
指定數組:
<?php
$ids = array(1,2,3,4,5,6);
$results = $this->Post->find('all', array(
'conditions' => array(
'Post.id' => $ids
)
));
我應該看看那裏。我傾向於避免這本書,因爲它時不時會令人困惑。謝謝 :) –
這正是我需要的。謝謝:) –
使用默認'遞歸'級別時,如果模型鏈接到其他模型,則僅指定字段名稱的'id''可能會失敗,因爲字段名稱可能不明確。 – nIcO
同意,爲了簡單起見,我只是在SQL查詢中發佈了id。 –