我有一个发票模型,它有2个字段确定其处理状态。一个是technician_id,一个是date_processed。它们都用于稍微不同的东西,每个都可以为空。
我想检索所有那些date_processed为null并且technician_id为null或与当前用户的id相同的发票。
我以为我知道自己在做什么,但很明显我有星期五的头脑或其他东西,因为我无法让它发挥作用。这就是我现在所拥有的:
$conditions = array(
'Invoice.date_processed' => null,
'Invoice.technician_id' => array(null, $user_id)
)
);
我也尝试过:
$conditions = array(
'Invoice.date_processed' => null,
'OR' => array(
'Invoice.technician_id' => null,
'Invoice.technician_id' => $user_id
)
);
但是,这两个都只返回technician_id与user_id匹配的那些发票,而不返回technician_id为null的那些发票(date_processed字段被正确过滤)。
谁能告诉我我做错了什么?
答案 0 :(得分:1)
我认为解决办法可能就是这样:
$conditions = array(
'OR' =>
array(
array('AND' => array(
array('Invoice.date_processed' => null),
array('Invoice.technician_id' => null)
)),
array('AND' => array(
array('Invoice.date_processed' => null),
array('Invoice.technician_id' => $user_id)
))
)
);
答案 1 :(得分:0)
你的第一个“或”语句不起作用的原因是因为数组在php中的工作方式,定义一个键两次会覆盖第一个键。
所以当你这样做时
$conditions = array(
'Invoice.date_processed' => null,
'OR' => array(
'Invoice.technician_id' => null,
'Invoice.technician_id' => $user_id
)
);
更像是在说这个
$conditions = array(
'Invoice.date_processed' => null,
'OR' => array(
'Invoice.technician_id' => $user_id
)
);
在cakephp中执行'OR'语句而没有过多'AND'语句的正确方法是
$conditions = array(
'Invoice.date_processed' => null,
'OR' => array(
'Invoice.technician_id' => array(null, $user_id)
)
);