我有两个几乎相同的功能。
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子句,一个got错误:列not : 1054未知列'telephone.number‘中的'where子句’当我注释它//->where('telephone.number = ?',$number);开始工作。所以这个矿场
有人知道如何更改$select,使用where子句使其成为working.Thank
发布于 2013-10-20 22:38:51
我认为问题在于您正在为计数创建一个新的select对象,但是当您重置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);
}它只是重用您现有的select。
https://stackoverflow.com/questions/19475483
复制相似问题