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?
Bookmark this question. Show activity on this post. $products = Mage::getModel('catalog/product') ->addAttributeToFilter('status', array('eq' => 1)); echo $products->getSelect()->__toString();
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.
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.
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,
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.
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.
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.
Try
$collection->getSelect()->__toString()
See How do you display a Magento sql query as a string?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With