How do you display a Magento sql query as a string?
Asked Answered
P

2

7

Magento constructs its SQL queries like

 $this->getSelect()->joinInner(
        array('sbao' => $this->getTable('sales/billing_agreement_order')),
        'main_table.entity_id = sbao.order_id',
        array()
    )

Is there a way to display the resulting query in a string format rather than printing out the huge object e.g.

echo $this->getSelect()->joinInner(
        array('sbao' => $this->getTable('sales/billing_agreement_order')),
        'main_table.entity_id = sbao.order_id',
        array()
    )->toString();
Pontiff answered 13/1, 2011 at 12:40 Comment(0)
M
13
$select = $this->getSelect()->joinInner(
        array('sbao' => $this->getTable('sales/billing_agreement_order')),
        'main_table.entity_id = sbao.order_id',
        array()
    );

echo $select;
Mcnully answered 13/1, 2011 at 12:57 Comment(2)
For the programmery folks, what's happening here is the select object is being cast as a string when used with echo. Using (string) $select or $seletct->__toString() would produce the same results.Rogerrogerio
It would not contain any limits or filters applied to the query. Any idea to get the full query including limits and filters?Botts
P
6

I nearly had it for those interested you need to use ->__toString() e.g.

echo $this->getSelect()->joinInner(
    array('sbao' => $this->getTable('sales/billing_agreement_order')),
    'main_table.entity_id = sbao.order_id',
    array()
)->__toString()
Pontiff answered 13/1, 2011 at 12:55 Comment(2)
echo is just enough as it has it's own toString methodOrten
and sometimes you need to cast it to trigger the __toString method automatically. Something like echo (string)$this->getSelect...Magistral

© 2022 - 2024 — McMap. All rights reserved.