Zend Db Select ? Substitution in join* condition
Asked Answered
H

2

7

It doesn't look like there's any parameter substitution in Zend_Db_Select's on clause.

It's highly annoying that I can't just do something like:

$select->joinLeft('st_line_item','st_line_item.order_id = st_order.id and st_line_item.status = ?')

So what's the idiomatic alternative that works within the fluent interface? I could do something like prepare the join clause on the outside but that's not the point.

Hospitalet answered 2/2, 2010 at 1:22 Comment(0)
B
11

This should work:

$select->joinLeft(
    'st_line_item',
    $this->_db->quoteInto(
        'st_line_item.order_id = st_order.id and st_line_item.status = ?', 
        $param
    )
)

Basically, anytime you want to escape a variable where a Zend_Db_* method doesn't do it automatically, you just use Zend_Db::quoteInto() to do the job.

Bagnio answered 2/2, 2010 at 20:19 Comment(0)
P
1

This is how I always do it, it's not a work of art but it gets the job done:

$param = $db->quote($param);
$select->joinLeft(
    'st_line_item',
    'st_line_item.order_id = st_order.id and st_line_item.status = ' . $param
);
Pannier answered 2/2, 2010 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.