最初发布在cakephp Q&A上,但我会把它放在这里,希望得到一些答案.
我有一些公司,其默认状态为0,但有时会获得更高的地位.现在我想使用高状态,如果存在,但如果没有,则恢复为0.我尝试了一堆不同的方法,但我总是得到状态为0的那些或者我想要的状态,如果存在则永远不给我状态,如果不存在则给0.
只给我指定的状态,而不是给我状态为0的状态:
'Company' => array (
'conditions' => array (
'OR' => array(
'Company.status' => 0,
'Company.status' => $status,
)
)
)
Run Code Online (Sandbox Code Playgroud)
只给我0的状态:
'Company' => array (
'conditions' => array (
'OR' => array(
'Company.status' => $status,
'Company.status' => 0
)
)
)
Run Code Online (Sandbox Code Playgroud)
状态定义和代码中的数据检索:
function getCountry($id = null, $status = null) {
// Bunch of code for retrieving country with $id and all it's companies etc, all with status 0.
$status_less_companies = $this->Country->find...
if ($status) { …Run Code Online (Sandbox Code Playgroud) 我有一个会话购物车,我在其中添加ID和数量.
我的数组看起来像这样 $cart = [[1,1], [108,1] ,[50,2], [109,1]]
第一个索引是产品ID,第二个索引是数量.
我正在尝试找到id等于我的发现 array[0]
作为一个测试我明确设置我想要的ID
我搜索曼努埃尔,我真的找不到任何东西.我看见matching()但那也不是.
$query = $this->Carts->Products->find('all')
->where(['id' => [1,108,50,109] ]);
Run Code Online (Sandbox Code Playgroud)
一个想法是在$ cart上做一个foreach而只是为每个查询得到(id)?但这听起来效率不高.
我需要在多列上创建一个 IN 条件,就像这样
...
WHERE
(order_date, order_number) IN (
('2016-03-11', 3455453),
('2016-03-18', 83545454),
('2016-06-17', 5354544)
)
Run Code Online (Sandbox Code Playgroud)
从这样的数组开始:
$orders = [
['2016-03-11', 3455453],
['2016-03-18', 83545454],
['2016-06-17', 5354544]
];
Run Code Online (Sandbox Code Playgroud)
使用 cake3 查询生成器。我试过
->where(['(order_date, order_number) IN' => $orders]);
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
无法将值转换为字符串
我知道手动创建操作数组的查询并不难,但我想知道是否有一种蛋糕的方法来做到这一点。