Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you display a Magento sql query as a string?

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();
like image 728
woot586 Avatar asked Jan 13 '11 12:01

woot586


People also ask

How do I display a string in SQL?

Note: You can use literal string (enclosed in single or double quotation mark) just like we use a column name in the SELECT statement. If you use the literal string with a column then it will be displayed in every row of the query results.

How do I print a SQL query in magento2?

Save this question. Show activity on this post. $products = Mage::getModel('catalog/product') ->addAttributeToFilter('status', array('eq' => 1)); echo $products->getSelect()->__toString();

How do I print a Searchcriteriabuilder query in Magento 2?

When Magento builds a product collection, it layers the filters in one at a time so the full query is not assembled until time of collection load. You might want to do the following to force the collection to load and then print the query: $collection->getItems(); echo $collection->getSelectSql(true);


2 Answers

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

echo $select;
like image 159
Max Pronko Avatar answered Sep 19 '22 11:09

Max Pronko


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()
like image 35
woot586 Avatar answered Sep 22 '22 11:09

woot586