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();
woot586
  • 3,906
  • 10
  • 32
  • 40

2 Answers2

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

echo $select;
Max Pronko
  • 1,369
  • 11
  • 13
  • 13
    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. – Alana Storm Apr 27 '11 at 22:08
  • It would not contain any `limits` or `filters` applied to the query. Any idea to get the full query including `limits` and `filters`? – Basheer Kharoti Oct 01 '21 at 10:02
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()
woot586
  • 3,906
  • 10
  • 32
  • 40