views:

70

answers:

1

I need to joinLeft within Zend_Db ala:

$select->joinLeft(array('ATAG' => 'ad_tags'),
                  array('ADM.id = ATAG.ad_id AND ADM.site_id = ATAG.site_id AND ATAG.tag_id = ?', $input_vars['tag']),
                  array('tag_id'))
       ->order('ATAG.tag_id DESC')
       ->limit('1');

However, I can't use an array as the second parameter, because it only tags a string; how can I pass in the value, without actually embedding it in the string?

+1  A: 

Based on your example you need to use quoteInto (considering you're in Zend_Db_Table):

$select->joinLeft(array('ATAG' => 'ad_tags'),
                  'ADM.id = ATAG.ad_id AND ADM.site_id = ATAG.site_id AND ' . 
                  $this->getAdapter()->quoteInto('ATAG.tag_id = ?',$input_vars['tag']),
                  array('tag_id'))
       ->order('ATAG.tag_id DESC')
       ->limit('1');

You can also do the same with:

$select->from(array('ADM' => 'adm_table'),'*')
       ->joinLeft(array('ATAG' => 'ad_tags'), 'ADM.id = ATAG.ad_id', 'ATAG.tag_id')
       ->where('ADM.site_id =ATAG.site_id')
       ->where('ATAG.tag_id = ?',$input_vars['tag'])
       ->limit(1);

If you're in Zend_Db_Table you need to set

$select->setIntegrityCheck(false);

(Note that adm_table is just an example, since you did not tell me the table name)

Keyne
Unfortunately, this won't work, as I need the extra conditional in the `ON` clause.
gms8994
It will work since the conditional are specified in the WHERE clausule, if you specify in ON or WHERE doesn't matter. It's just readability. Any way, I edited my answer with the solution using your query. So please, make sure when down voting =)
Keyne
Actually, you'll find that it *does* matter whether the removal of rows happens in the ON vs the WHERE. As your original answer doesn't give me the results I need, you received a downvote. Now that you've amended your answer, you shall receive an upvote, and get the check.
gms8994
I didn't know that, if you could provide me some resources I'd apreciate. In my applications I always create the additional conditionals in the WHERE clausule. Maybe I'm missing something...
Keyne