在'where子句'中的未知列'telephone.number'Zend_Paginator_Adapter_DbTableSelect

时间:2013-10-20 08:42:47

标签: mysql sql zend-framework

我有两个几乎相同的功能。

    public function getUserAndCommentAndTelephone($paged=NULL)
        {
     $select  = $this->select()->setIntegrityCheck(false)
             ->from('user',array('name','user_id'))
             ->join('comment', 'user.user_id = comment.user_id', array('comment_id','text','date'))
             ->join('telephone', 'telephone.telephone_id = comment.telephone_id', array('number'))
             ->order(array('comment.comment_id DESC'));
     if (null !== $paged) {
            $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
            $count = clone $select;
            $count->reset(Zend_Db_Select::COLUMNS);
            $count->reset(Zend_Db_Select::FROM);
            $count->from('comment', new Zend_Db_Expr('COUNT(*) AS `zend_paginator_row_count`'));
            $adapter->setRowCount($count);

            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(5)
                      ->setCurrentPageNumber((int) $paged);
            return $paginator;
    }
    return $this->fetchAll($select);

和$ select中的where子句的另一个。这只是一个不同的。

   public function getCommentAndUserByTelephone($number,$paged=null) {
    $select  = $this->select()->setIntegrityCheck(false)
             ->from('user',array('name','user_id'))
             ->join('comment', 'user.user_id = comment.user_id', array('text','comment_id','date'))
             ->join('telephone', 'telephone.telephone_id = comment.telephone_id', array('number'))
             ->order(array('comment.comment_id DESC'))
             ->where('telephone.number = ?', $number);
   if (null !== $paged) {
            $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
            $count = clone $select;
            $count->reset(Zend_Db_Select::COLUMNS);
            $count->reset(Zend_Db_Select::FROM);
            $count->from('comment', new Zend_Db_Expr('COUNT(*) AS `zend_paginator_row_count`'));
            $adapter->setRowCount($count);

            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(5)
                      ->setCurrentPageNumber((int) $paged);
            return $paginator;
    }

    return $this->fetchAll($select);         
        }

并且在 where 子句中出现错误: 未找到列:1054'where子句'中的未知列'telephone.number' 当我评论它//-> where('telephone.number =?',$ number);开工。 所以我的这个WHERE子句出了问题。

有人知道如何更改$ select,使用where子句使其工作。谢谢

1 个答案:

答案 0 :(得分:0)

我认为问题是您正在为计数创建一个新的选择对象,但是当您重置FROM子句时,这也会删除连接。您需要在计数中重复连接选择您必须工作的内容。但是,大部分代码都是不必要的,因为ZF可以根据您传入的select对象为您构建计数查询。

试试这个:

public function getCommentAndUserByTelephone($number,$paged=null) {
    $select  = $this->select()->setIntegrityCheck(false)
             ->from('user',array('name','user_id'))
             ->join('comment', 'user.user_id = comment.user_id', array('text','comment_id','date'))
             ->join('telephone', 'telephone.telephone_id = comment.telephone_id', array('number'))
             ->order(array('comment.comment_id DESC'))
             ->where('telephone.number = ?', $number);

   if (null !== $paged) {
        $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);

        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(5)
                  ->setCurrentPageNumber((int) $paged);
        return $paginator;
    }

    return $this->fetchAll($select);         
}

只需重新使用现有的选择。