zend-framework2 – 如何使用多个on子句加入zend框架2

前端之家收集整理的这篇文章主要介绍了zend-framework2 – 如何使用多个on子句加入zend框架2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在做这样的sql进入zend框架的sql模式.
SELECT 
    jobs . *,c.id AS cid,c.name AS name,c.companyImage AS companyImage,c.logo AS logo,count(app.userId) AS t_app,app.applyStatus AS applyStatus,app.userId AS appUserId
FROM
    jobs
        LEFT JOIN
    companies AS c ON jobs.companyName = c.id
        LEFT JOIN
    applicants AS app ON jobs.id = app.jobId AND app.applyStatus = 1
WHERE
    jobs.ownerId = 16 AND jobs.draftId != 0 
GROUP BY jobs.id
ORDER BY jobs.id DESC
LIMIT 3

对于这个sql,我已经为zend框架2编写了这个代码

$adapter = $this->tableGateway->getAdapter();
        $sql     = new sql($adapter);

        $select = $sql->select();
        $select->from('jobs')
                ->join(array('c' => 'companies'),'jobs.companyName = c.id',array('cid' => 'id','name','companyImage','logo'),'left')
                ->join(array('app' => 'applicants'),' jobs.id = app.jobId AND app.applyStatus = 1',array('t_app'     => new Expression('count(app.userId)'),'applyStatus','appUserId' => 'userId'),'left')
                ->where("jobs.ownerId ={$userId} AND jobs.draftId != 0")
                ->group('jobs.id')
                ->order('jobs.id DESC')
                ->limit(3);

        $statement = $sql->getsqlStringForsqlObject($select);
        $results   = $adapter->query($statement,$adapter::QUERY_MODE_EXECUTE);

但不能正常工作,并发出如下的信息.

sqlSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'on clause'
问题是这部分:
app.applyStatus = 1

框架转义为1,就像它是列名一样.

您也需要将此部分包含在表达式中

new Expression('jobs.id = app.jobId AND app.applyStatus = 1')

我认为在连接方法的’ON’参数中使用表达式可能取决于你正在使用的ZF2的版本,我认为它被添加了2.1

猜你在找的PHP相关文章