Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output raw SQL query from Magento collection

I have the following collection and want to view the raw SQL within my IDE Xdebug tool -

$collection = Mage::getResourceModel('rp/organisation_collection')
                ->searchByPostcodeLastname($postcode, Slastname)
                ->addFieldToSelect(array('organisation_id'))
                ->setPageSize(1);

$qry = $organisation->load()->getSelect();

This while does show the Varien object doesn't seem to give me the raw SQL for me to check - can someone confirm what I am doing wrong?

like image 220
Zabs Avatar asked May 23 '14 16:05

Zabs


People also ask

How do I print a collection query in Magento 2?

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

What is raw SQL queries?

Raw SQL queries are useful if the query you want can't be expressed using LINQ. Raw SQL queries are also used if using a LINQ query is resulting in an inefficient SQL query. Raw SQL queries can return regular entity types or keyless entity types that are part of your model.

How do I write a query in Magento 2?

You can write a select query in Magento 2 to retrieve records from the table. The select query can be written with best practice to avoid security breaches. You can direct use above string as query and pass it to the query() method but it's not a secure way to write a select query.

How to use direct SQL query in Magento 2?

When you use Direct SQL Query you don’t need to worry about Model/Factory Pattern. By default, Magento will automatically connect to its database using class Magento\Framework\App\ResourceConnection with getConnection () function. Using the below code snippet to run the direct SQL query to fetch Table name with a prefix,

How to optimize Magento get collection query for better performance?

There are many situations where executing direct raw SQL queries would be simple and much quicker leading to a more optimized Magento get collection query as a performance basis. On a large data set of an entity, Saving each individual entity can take a long time with resource-hungry and therefore make the system unusable.

How to get table name with a prefix in Magento 2?

By default, Magento will automatically connect to its database using class Magento\Framework\App\ResourceConnection with getConnection () function. Using the below code snippet to run the direct SQL query to fetch Table name with a prefix, You need to use the $this->resourceConnection object to run any Direct SQL Query.

What are the benefits of using data models in Magento?

Magento's use of data models provide a great way to access and modify data. Using aptly named methods and clever abstraction, Varien hide away the complex SQL needed to perform data operations.


1 Answers

Try

$collection->getSelect()->__toString()

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

like image 86
Renon Stewart Avatar answered Oct 17 '22 12:10

Renon Stewart