3

到目前为止获得所有结果,直到我添加了带有 join ON 子句的“IN”语句

我的代码是这样的:

// $this->table = 'category';
$sql = $this->getSql();
$select = $sql->select();
$select->join('user_category_subscriptions', 'user_category_subscriptions.category_id = category.id AND user_category_subscriptions.status IN (1,2,3,4)', array(), 'left');
$select->where(array('category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))'));

错误

ZEND\DB\ADAPTER\EXCEPTION\INVALIDQUERYEXCEPTION
File:E:\htdocs\myproj\vendor\zendframework\zendframework\library\Zend\Db\Adapter\Driver\Pdo\Statement.php:245
Message:Statement could not be executed

使用 'echo $select->getSqlString();' 打印查询:

SELECT "categories".* FROM "categories" 
LEFT JOIN "user_category_subscriptions" 
ON "user_category_subscriptions"."category_id" = "categories"."category_id" AND "user_category_subscriptions"."status" IN ("1""," "2""," "3""," "4")
WHERE category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))

所以问题是 zend 自动将(1,2,3,4)转换成("1"","2"","3"","4")

有什么办法解决这个问题吗?谢谢

4

2 回答 2

6

我认为您可以传递表达式而不是连接字符串:

// $this->table = 'category';
$sql = $this->getSql();
$select = $sql->select();

$join = new Expression('user_category_subscriptions.category_id = category.id AND user_category_subscriptions.status IN (1,2,3,4)');

$select->join('user_category_subscriptions', $join, array(), 'left');
$select->where(array('category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))'));
于 2013-03-14T09:12:11.880 回答
2

尝试这个

$select->where
       ->AND->NEST->equalTo('category.user_id', 2)
       ->addPredicate(new Sql\Predicate\In('user_category_subscriptions.status', array(2,4)));
于 2013-03-14T05:44:03.363 回答