SQL到Zend_Db_Table

时间:2011-05-14 02:38:36

标签: mysql sql zend-db zend-db-table

我正在尝试将SQL转换为Zend_Db_Table

SELECT c1.* FROM beneficios c1
left join beneficios c2 on c1.document_id = c2.document_id and c1.versao <       c2.versao
where c1.id_projeto = 8 and c2.document_id is null order by ordem ASC;

我在zend db table class中有一个方法

$info = $this->info();
    $select = $this->select()
         ->from(array('c1' => $info['name']))
         ->joinLeft(array('c2' => $info['name']),
                'c1.document_id = c2.document_id and c1.versao < c2.versao')
            ->where('c2.document_id is null')
            ->where('c1.id_projeto = ?', $id_projeto)
            ->order('ordem ASC');

    return $this->fetchAll($select);

我收到以下错误

Zend_Db_Statement_Exception: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'ordem' in order clause is ambiguous

如果我删除订单

Zend_Db_Statement_Exception: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound

转换该SQL的正确方法是什么?

如果有人可以帮助我,谢谢!

2 个答案:

答案 0 :(得分:1)

正如它所说:“秩序中的列'ordem'是不明确的”。使用ordemc1.前缀c2.,具体取决于您要排序的表格ordem列。

答案 1 :(得分:0)

而不是$ this-&gt; select()使用$ this-&gt; getAdapter() - &gt; select()。您还可以通过将空数组传递给joinLeft函数来指定您不希望表c2中的任何列:

$info = $this->info();
    $select = $this->getAdapter->select()
         ->from(array('c1' => $info['name']))
         ->joinLeft(array('c2' => $info['name']),
                'c1.document_id = c2.document_id and c1.versao < c2.versao', array())
            ->where('c2.document_id is null')
            ->where('c1.id_projeto = ?', $id_projeto)
            ->order('ordem ASC');

return $this->fetchAll($select);