Archive for the ‘Tips’ Category

Magento default settings

In config.xml <?xml version="1.0"?> <config> <default> <modulename> <parametergroup> <param>value</param> </parametergroup> </modulename> </default> </config>

Magento collection add join and filter

$tableDataCollection = Mage::getResourceModel(’modulename/tablename_collection’)->setPageSize(50);   $tableDataCollection->getSelect() ->join(array(’alias’ => Mage::getSingleton(’core/resource’)->getTableName(’modulename/tablename’)), ‘main_table.id=alias.id’, ‘*’) ->where(’alias.condition = ?’, true);

Get table name

Mage::getSingleton(’core/resource’)->getTableName(’tablename’)

Yet another raw sql example

$resource = Mage::getSingleton(’core/resource’); $readConnection = $resource->getConnection(’core_read’);   $query = ‘SELECT COUNT(id) AS rows FROM table’; $sql = $readConnection->prepare($query); $sql->execute(); $data = $sql->fetch(); printf("Rows: %d\n", $data[’rows’]);

Difference between addFieldToFilter and addAttributeToFilter

addFieldToFilter() is used to filter Non-EAV collections. addAttributeToFilter() is used to filter EAV collections.

Between

gteq – greater than or equal (>=) lteq – less then or equal () lt – less (

Magento Iterate with Pages

$tableDataCollection = Mage::getResourceModel(’modulename/tablename_collection’)->setPageSize(50);   // Set number of pages and current page $pages = $tableDataCollection->getLastPageNumber(); $currentPage = 1;   // Interate do { $tableDataCollection->setCurPage($currentPage); $tableDataCollection->load();   foreach ($tableDataCollection as $tableRow) { echo $tableRow->getId()."\n"; }   $currentPage++;   //Clear collection and free memory $tableDataCollection->clear(); } while ($pages => $currentPage);

Running Magento from command line

Method 1 <?php   require_once dirname(__DIR__) . ‘/../../../../../app/Mage.php’; $app = Mage::app(); Mage::register(’isSecureArea’, true); // your code goes here or <?php   require_once dirname(__DIR__) . ‘/../../../../../app/Mage.php’;   /** * Class My Shell Class */ class My_Shell_Class {   /** * Constructor */ public function __construct() { Mage::app(); Mage::register(’isSecureArea’, true); }   /** * Custom function */ […]

Add a sql like to filter column in grid

  <?php   protected function _addColumnFilterToCollection($column) { if ($column->getId() == ‘name’) { $this->getCollection()->addFieldToFilter($column->getId(), array(’like’ => $column->getFilter()->getValue() . ‘%’)); … $this->getCollection()->addFieldToFilter($column->geId(), $column->getFilter()->getCondition()); … $this->getCollection()->addFieldToFilter(’name’, array(’like’ => ‘%’ . $name . ‘%’)); else { parent::_addColumnFilterToCollection($column); }   return $this; }

Pass information from Observer via session

Step 1: Mage::dispatchEvent(’my_event’); Step 2: link method MyEvent to event “my_event” in your module’s config.xml file Observer.php public function MyEvent(Varien_Event_Observer $observer) { Mage::getSingleton(’customer/session’)->setData(’my_event’, ‘results’); } } Step 3: $result = Mage::getSingleton(’customer/session’)->getData(’my_event’));