0

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

    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子句出现错误: Column not found: 1054 Unknown column 'telephone.number' in 'where Clause' when I COMMENT it //->where('telephone.number = ?', $number); 开工。所以这个我的 WHERE 子句有问题。

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

4

1 回答 1

0

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

尝试这个:

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);         
}

那只是重用您现有的选择。

于 2013-10-20T22:38:51.083 回答