Search code examples
mysqlzend-frameworkzend-framework2zend-dbtablegateway

Sql Query with Zend Framework 2 tableGateway


I'm new to Zend Framework 2 and I'm currently coding a Forum for a school project. I have a query that I can't manage with a Db Sql Select object and tableGateway. I just used a Db Adapter and it works fine but I'm looking for a way to pass my query by using a Db Sql Select object because I'll need to use a paginator later.

Here's what I've got :

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

Any Ideas on how I can have this query executed with Zend\Db\Sql\Select ? Thank you !


Solution

  • $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']);