我想在我的项目中使用Zend Paginator与原始sql查询(而不是Dbselect)。 我无法使用Dbselect,因为我在主sql查询中有几个子查询。 从手册我发现的唯一办法就是使用分页导致阵列上这样zend paginator问题
$paginator = Zend_Paginator::factory($array);
,但我在查询中有很多的记录,它看起来像一个坏主意给我。 有什么办法可以解决这个问题吗?
我想在我的项目中使用Zend Paginator与原始sql查询(而不是Dbselect)。 我无法使用Dbselect,因为我在主sql查询中有几个子查询。 从手册我发现的唯一办法就是使用分页导致阵列上这样zend paginator问题
$paginator = Zend_Paginator::factory($array);
,但我在查询中有很多的记录,它看起来像一个坏主意给我。 有什么办法可以解决这个问题吗?
如果您的查询是真的复杂,你不能使用Zend_Db_Select
你可以诉诸写你自己的Zend_Paginator_Adapter
这是not as complicated as it might sound。
您的相应类只能执行Zend_Paginator_Adapter_Interface
以允许您将该类传递到Zend_Paginator
构造函数中。
下面是一个简单的例子 - 你可以把它作为一个起点...
class App_Model_ListOfItems implements Zend_Paginator_Adapter_Interface
{
/**
* @var Zend_Db_Adapter_Abstract
*/
protected $_db;
/**
* @param Zend_Db_Adapter_Abstract $db
*/
public function __construct(Zend_Db_Adapter_Abstract $db)
{
$this->_db = $db;
}
/**
* @implements Zend_Paginator_Adapter_Interface
* @return integer
*/
public function count()
{
return (int)$this->_db->fetchOne('SELECT COUNT(*) FROM <<add your query to determine the total row count here>>');
}
/**
* @implements Zend_Paginator_Adapter_Interface
* @param integer $offset
* @param integer $itemCountPerPage
* @return array
*/
public function getItems($offset, $itemCountPerPage)
{
$sql = 'SELECT <<add your columns, tables, joins and subqueries as needed>> LIMIT ' .
(int)$offset . ', ' . (int)$itemCountPerPage;
return $this->_db->fetchAll($sql);
}
}
$paginator = new Zend_Paginator(new App_Model_ListOfItems($db));
您可以使用子查询的数据库选择这样的:
$select = $this->_db->select();
$select->from('customer', array('id', 'first_name', 'family_name', 'company', 'phone', 'email'));
$select->joinLeft('sale', 'customer.id = sale.customer_id and sale.id in (select max(id) from sale group by customer_id)', array('sale_id' => 'id', 'billing_phone', 'shipping_phone', 'billing_city', 'billing_country', 'created_on'));
(上面的查询将返回客户名称,再加上从他们最近购买的详细联系方式)
谢谢,这是一个很好的答案 – mik 2009-10-07 06:06:19