使用Zend Framework 2 tableGateway进行Sql查询

时间:2015-12-11 19:25:21

标签: mysql zend-framework zend-framework2 zend-db tablegateway

我是Zend Framework 2的新手,我目前正在编写一个学校项目论坛。 我有一个无法用Db Sql Select对象和tableGateway管理的查询。我只是使用了Db适配器,它工作正常,但我正在寻找一种方法来通过使用Db Sql Select对象传递我的查询,因为我以后需要使用一个分页器。

这就是我所拥有的:

public function fetchAll()
 {
    $select = $this->adapter->query(
           'SELECT theme.nom AS nom, theme.idTheme AS idTheme, theme.TidAdmin AS TidAdmin, theme.description AS description, users.username, Dnom, date, admin.pseudo AS apseudo
            FROM theme
            LEFT join (select DidUsers, Dnom, DidTheme, date from discussion group by date ) D ON `DidTheme` = theme.`idTheme` 
            LEFT JOIN users on DidUsers = idUsers
            LEFT JOIN admin on TidAdmin = idAdmin'
            );
    $statement = $select;
    $results = $statement->execute();
    return iterator_to_array($results);
 }

有关如何使用Zend \ Db \ Sql \ Select执行此查询的任何想法? 谢谢 !

1 个答案:

答案 0 :(得分:0)

$subquery = new \Zend\Db\Sql\Select ('discussion');
$subquery->group ("date");
$subquery->columns (["DidUsers" , "Dnom", "DidTheme", "date"]);

$query = new \Zend\Db\Sql\Select ('theme');
$query->columns (['nom', 'idTheme', 'TidAdmin', 'description']);
$query->join ($subquery, 'idTheme = DidTheme');
$query->join ('users', 'idUsers = DidUsers', ['username']);
$query->join ('admin', 'idUsers = TidUsers', ['pseudo' => 'apseudo']);