SQL в Zend_Db_Table - PullRequest
       39

SQL в Zend_Db_Table

0 голосов
/ 14 мая 2011

Я пытаюсь преобразовать 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

$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 ]

1 голос
/ 14 мая 2011

Это как говорится: «Столбец 'ordem' в предложении order является неоднозначным".Префикс ordem с c1. или c2., в зависимости от столбца ordem таблицы, по которой вы хотите отсортировать.

0 голосов
/ 14 мая 2011

Вместо $ this-> select () используйте $ this-> getAdapter () -> select (). Также вы можете указать, что вам не нужны никакие столбцы из таблицы c2, передав пустой массив в функцию joinLeft:

$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);
...