使用Zend Framework限制JOIN

时间:2011-04-04 11:54:27

标签: php mysql zend-framework select join


我有两张表parentchildren,关系为1:M。我需要一个分页。我使用SELECT和JOIN。对于1位父母,我有一些孩子。如果我尝试使LIMIT 10此查询总共获得10行。但我需要从表parent获得所有关系的10行。我该怎么办?很抱歉,我的英文。提前谢谢。

Mysql查询:

SELECT `o`.`id` AS `order_id`, `od`.`id` AS `order_destination_id` FROM `order` AS `o`
 LEFT JOIN `order_destination` AS `od` ON o.id = od.order_id LIMIT 5

Zend框架:

$select = $this->select()
    ->setIntegrityCheck(false)
    ->from(array('o' => 'order'), array('order_id' => 'id'))
    ->joinLeft(array('od' => 'order_destination'), 'o.id = od.order_id', array('order_destination_id' => 'id'))
    ->limit(5);

2 个答案:

答案 0 :(得分:1)

我不知道Zend Framework。这是一个简单的SQL解决方案:

SELECT `o`.`id` AS `order_id`, `od`.`id` AS `order_destination_id` FROM `order` AS `o`
LEFT JOIN `order_destination` AS `od` ON o.id = od.order_id 
where (select count(*) from order o2 where o2.id > o.id) < 10

答案 1 :(得分:1)

最好使用Zend_Paginator通过一个表进行分页,然后为每个项目请求子项:

$paginator = Zend_Paginator::factory($this->select(), 'DbTableSelect');
$paginator->setItemsCountPerPage(5);
foreach ($paginator as $row) {
    $children = $row->getDependentRowset('ChildrenRule');
}