LEFT JOIN in ZF2 using TableGateway

前端 未结 7 1750
无人共我
无人共我 2021-01-03 23:01

I have a table:

*CREATE TABLE IF NOT EXISTS `blogs_settings` (
  `blog_id` int(11) NOT NULL AUTO_INCREMENT,
  `owner_id` int(11) NOT NULL,
  `title` varchar(         


        
7条回答
  •  清酒与你
    2021-01-03 23:18

    Adding to @samsonasik's answer and addressing the issues in its comments. You won't be able to get the joined values out of what is returned from that statement. That statement returns the model object which won't have the joined rows. You'll need to execute it as SQL at a level which will prepare it as raw SQL and return you each resulting row as an array rather than an object:

    $sqlSelect = $this->tableGateway->getSql()->select();
    $sqlSelect->columns(array('column_name_yourtable'));
    $sqlSelect->join('othertable', 'othertable.id = yourtable.id', array('column_name_othertable'), 'left');
    
    $statement = $this->tableGateway->getSql()->prepareStatementForSqlObject($sqlSelect);
    $resultSet = $statement->execute();
    return $resultSet;
    
    //then in your controller or view:
    
    foreach($resultSet as $row){
        print_r($row['column_name_yourtable']);
        print_r($row['column_name_othertable']);
    }
    

提交回复
热议问题