简体   繁体   中英

zend framework get data from two tables

if I use

 $query = $this
        ->select()
        ->from(array('a' => 'aanvragen'))
        ->join(array('v' => 'verloven'),
                   'v.aanvraag_id = a.id' ,array())
        ->where('a.personeelslid_id = :personeelslid_id')
        ->where('v.type = 3')
        ->bind(array(
            'personeelslid_id' => $this->_personeelslid->id
        ))
    ;
    return $this->fetchAll($query, array('aanvragen','verloven'));

I get the correct record(s) but no data from table verloven(v) due to array() .

But if I remove the empty array like:

     $query = $this
        ->select()
        ->from(array('a' => 'aanvragen'))
        ->join(array('v' => 'verloven'),
                   'v.aanvraag_id = a.id' )
        ->where('a.personeelslid_id = :personeelslid_id')
        ->where('v.type = 3')
        ->bind(array(
            'personeelslid_id' => $this->_personeelslid->id
        ))
    ;
    return $this->fetchAll($query, array('aanvragen','verloven'));

I get an error:

Select query cannot join with another table.

What to do to get data from both tables?

assuming you are using zf 1+ , you can do following from your model,

 $sql=$this->select()
            ->setIntegrityCheck(false)
            ->from(array('a' => 'aanvragen'),array('field1','field2'))               
            ->join(array('v' => 'verloven'),'v.aanvraag_id = a.id',array('field3'))  
            ->where('a.personeelslid_id = :personeelslid_id')
            ->where('v.type = 3')   
            ->group('fieldname')
            ->order('fieldname');          

        $resultSet = $this->fetchAll($sql);
        return $resultSet; 

you need to set setIntgrityCheck to false to join select query.. hope this helps..

AFAIK, you should almost NEVER set setIntegrityCheck(false) if what you really need is something that is cross-table.

The right way to do this would be by using the Zend_Db_Adapter :

$query = $this
        ->getAdapter()
        ->select()
        ->from(array('a' => 'aanvragen'))
        ->join(array('v' => 'verloven'),
                   'v.aanvraag_id = a.id' ,array())
        ->where('a.personeelslid_id = :personeelslid_id')
        ->where('v.type = 3')
        ->bind(array(
            'personeelslid_id' => $this->_personeelslid->id
        ))
    ;
    return $this->getAdapter()->fetchAll($query, array('aanvragen','verloven'));

The reasoning behind this is, that what you are using (asuming that your model extends from Zend_Db_Table_Abstract ) is a Table context. Meaning, you are telling the framework, that you want to work with the specific table, and you do not care about other tables.

If you want to do a join, you should take a step up in the context ladder, and use the Db context, which can be obtained by using the getAdapter() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM