cancel
Showing results for 
Search instead for 
Did you mean: 

Run ORM php Magento 1.9

Run ORM php Magento 1.9

1. Our first step is going to be to instantiate a product collection:

$collection = Mage::getModel('catalog/product' )->getCollection();

2. Then we will specifically tell Magento to select the name attribute:

$collection->addAttributeToSelect('name');

3. Then, we will ask it to sort the collection by name:

$collection->setOrder('name', 'asc');

4. Finally, we will tell Magento to load the collection:

$collection->load();

5. The end result is a collection of all products in the store sorted by name. We can inspect the actual SQL query by running the following code:

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

In just three lines of code, we are telling Magento to grab all the products in the store, to specifically select the name, and finally order the products by name. The last line

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

allows to see the actual query that Magento is executing in our behalf. The actual query being generated by Magento is as follows:

SELECT `e`.*. IF( at_name.value_id >0, at_name.value, at_name_default.value ) AS `name` FROM `catalog_product_entity` AS `e` LEFT JOIN `catalog_product_entity_varchar` AS `at_name_default` ON (`at_name_default`.`entity_id` = `e`.`entity_id`) AND (`at_name_default`.`attribute_id` = '65') ORM and Data Collections [ 50 ] AND `at_name_default`.`store_id` =0 LEFT JOIN `catalog_product_entity_varchar` AS `at_name` ON ( `at_name`.`entity_id` = `e`.`entity_id` ) AND (`at_name`.`attribute_id` = '65') AND (`at_name`.`store_id` =1) ORDER BY `name` ASC

This is an extract from tutorial , my problem is that I cant execute the php script below how can I run it, I have tested to run but it shows me class Mage not found

Thanks in advance

4 REPLIES 4

Re: Run ORM php Magento 1.9

@ahmed_chouihi if you are creating the php script on root of magento then just add the below code on the top of you script.

 

require_once('app/Mage.php');
Mage::app();

Thanks

Re: Run ORM php Magento 1.9

Thanks for your answer but I am still getting this error

 

 Uncaught PDOException: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/ahmedcx/dev/magento/lib/Zend/Db/Adapter/Pdo/Abstract.php:128

 

 

Re: Run ORM php Magento 1.9

This is the full code that I have put in magento directory and from terminal

php filename.php

<?php
require_once('app/Mage.php');
Mage::app();
$collection = Mage::getModel('catalog/product'
)->getCollection();
$collection->addAttributeToSelect('name');
$collection->setOrder('name', 'asc');
$collection->load();
echo $collection->getSelect()->__toString();

 

Re: Run ORM php Magento 1.9

@ahmed_chouihi i have used the same code and it is working fine.

 

I have created a test.php file on magento root directory and then access it from browser using 

 example.com/test.php and i got the below result.

 

screenshot 

 

Please let me know your file location.

 

 

Thanks