2

So here is my query:

public function fetchAd($adID){
    $row = $this->tableGateway->select(function(Select $select) use ($adID){
        $select->join('adDetails','adDetails.adID = ads.adID',array('*'),'inner');
        $select->where(array('ads.adID' => $adID));
    });

    return $row->current();
}

So what I'm doing I'm querying the ad table and join the adDetails table in order for me to get the details for a certain AD, the problem is that the entity AD which belongs to the model that I'm doing the query, it doesn't have the columns names(variables) from the adDetails table; so it will only return the columns from the AD table, because the entity doesn't have those fields in the exchangeArray()

I have tried to extend the AD entity to use the AdDetails entity but it returns now to object array but with the fields as null, because it can;t populate them.

So, how should I do this, in order for me to have all the columns available in the model for the tables that will join?

I'm planning to join other tables as well.

1
  • I think relations between objects is not yet possible in ZF2, I guess I will have to copy manually all the vars from the entity and set them in the exchangeArray()
    – Uffo
    Apr 7, 2014 at 20:15

2 Answers 2

2

Ok I solved the problem, the thing is that it will return an array, and it won't use the ORM style, but it does the job, for now relations are not supported in ZF2 like in ZF1;

    use Zend\Db\Sql\Sql,
        Zend\Db\Sql\Where;
    $sql = new Sql($this->tableGateway->getAdapter());
    $select = $sql->select();
    $select->from($this->tableGateway->table)
          ->join('adDetails','adDetails.adID = ads.adID',array('*'),'inner');

    $where = new  Where();
    $where->equalTo('ads.adID', $adID) ;
    $select->where($where);

    $statement = $sql->prepareStatementForSqlObject($select);
    $result = $statement->execute();

    return $result->current();
0
public function fetchJoin() 
{ 
    $select = new \Zend\Db\Sql\Select; 
    $select->from('tablea a'); 
    $select->columns(array('*')); 
    $select->join('tableb b', "b.id = a.b_id", array('field1'), 'left'); 
    // to display query string remove comment in next line
    //echo $select->getSqlString(); 
    $resultSet = $this->tableGateway->selectWith($select); 
    return $resultSet; 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.