Archive for the ‘Tips’ Category

SQL Query IN and NOT IN

Magento uses keywords in for IN and nin for NOT IN …   $productIds = array(1251,1253,1256);   if ($column->getFilter()->getValue()) { $this->getCollection()->addFieldToFilter(’entity_id’, array(’in’=>$productIds)); } else { $this->getCollection()->addFieldToFilter(’entity_id’, array(’nin’=>$productIds)); } …

What is the difference of addAttributeToFilter() Vs addFieldToFilter()?

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

Re-use existing layout

tag is used for re-usage of existing layout   <vendorname_modulename_constrollername_indexaction>   <reference name="myreferencename"> <block type="vendorname/blockname" name="modulename_blockname" template="storelocator/template.phtml"> </block> </reference>   </vendorname_modulename_constrollername_indexaction>   <vendorname_modulename_constrollername_listaction>   <update handle="vendorname_modulename_constrollername_indexaction"/>   </vendorname_modulename_constrollername_listaction>

Redirecting in Magento

Redirect to the list Action in the same Controller $this->getResponse()->setRedirect(Mage::getUrl(’*/*/list’));

How to get a table name in Magento Resource Collection

Method 1:   <?php … $this->getResource()->getTable(’catalog/category_product’); Method 2:   … Mage::getSingleton(’core/resource’)->getTableName(’catalog/category_product’);

Another method of getting Customer Data

  <?php   … public function getCustomer() { $customerId = $this->_getCustomerId();   return Mage::getModel(’customer/customer’)->load($customerId); }   private function _getCustomerId() { return Mage::getSingleton(’customer/session’)->getCustomerId(); } …

Check if Customer Logged In and get the details from Session

  <?php   // require_once ‘/var/www/magento/app/Mage.php’; // Mage::init();   function getSession() { return Mage::getSingleton(’customer/session’); }   if (!getSession()->isLoggedIn()) { $this->_redirect(’customer/account/login’); return; }   if (getSession()->isLoggedIn()) { echo ‘loggedIn’;   $customerData = Mage::getSingleton(’customer/session’)->getCustomer();   printf("%s\n %s\n %s\n %s", $customerData->getId(), $customerData->getWebsiteId(), $customerData->getStoreId(), $customerData->getIsActive()); //Zend_Debug::dump($customerData); }

Magento Core Tables

1) eav_attribute whenever you create a attribute this table gets entry which stores all important data which helps to make a relation, like entity_type_id,attribute_code,backend_type and many more but this are the important fields. 2) eav_entity_type Magento has 8 types of entity, by which, type of attribute is decided. in your case it is catalog_product 1) […]

Comparing EAV to Relational Table

EAV makes a lot of sense for a generic e-commerce solution. A store that sells laptops (which have a CPU speed, color, ram amount, etc) is going to have a different set of needs than a store that sells yarn (yarn has a color, but no CPU speed, etc.). Even within our hypothetical yarn store, […]

How-to get SQL query string from Mage

  <?php   $collection = Mage::getResourceModel(’mybrands/orders’) ->addFieldToSelect(’name’) ->addFieldToSelect(’order_id’);   echo ‘<pre>’; print_r($collection->getSelect()->__toString());