Wednesday, May 28, 2014

How to get sql query in magento module collection

Suppose we have a data collection and we want to get the sql query running behind it then the following code is the syntax to check it.
$collection = Mage::getModel('catalog/product')->getCollection();
echo $collection->getSelect()->__toString()

How to call a static block in phtml file in magento

We can use a simple code to call a block in any .phtm file. 
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('top_left_links')->toHtml() ?>

How many type of session having in magento?

There have three type of session in magento.
1) Filesystem Session storage
2) Database Session storage
3) Memcached Session storage

 Filesystem Session storage  

To enable file-based storage, choose it during installation or in your app/etc/local.xml file simply make sure you have a session_save tag like this:

<session_save><![CDATA[files]]></session_save>

Database Session storage
 To use the database for session storage simply have this in your local.xml:   
<session_save><![CDATA[db]]></session_save>


Memcached Session storage

Memcached session storage takes a bit more setup than either of the previous two options, which is probably why it’s not considered a ‘normal’ option during Magento install. For starters you need a Memcached server running.

Once you have it up and running, the memcached session storage offers a number of benefits. Firstly it is very cluster friendly. The session data can be shared by any number of webnodes, and to make things even better you can easily add more memcached server nodes so that even your session storage can be scaled to handle many 1000′s of concurrent sessions*. Secondly, it is (or can be) separate of the database and web node entirely, which offloads the work of storing sessions from busy nodes in a high-traffic environment.
To use a memcached session store in Magento you’ll need to have this in your app/etc/local.xml:   
<session_save><![CDATA[memcache]]></session_save>
<session_save_path><![CDATA[tcp://localhost:11211?persistent=1&weight=2&timeout=10&retry_interval=10]]></session_save_path>

Tuesday, May 27, 2014

What is Object Relational Mapping (ORM) php and magento

ORM stands for Object Relational Mapping. It’s a programming technique used to convert different types of data to Objects and vice versa.

In Magento, ORM is shown as Model (based on Zend Framework’s Zend_Db_Adapter), which further breaks down to two types of Models.

  • First is the “simple” i.e. Regular Models which is nothing but flat table or our regular table structure.

  • Second Model is EAV (Entity Attribute Value), which is quite complicated and expensive to query.

 All Magento Models interacting with database are inherited from Mage_Core_Model_Abstract class, which is further inherited from Varien_Object.

Difference between two Models is,  Simple Model is inherited from Mage_Core_Model_Resource_Db_Abstract class,
while EAV is inherited from Mage_Eav_Model_Entity_Abstract.


So, to end up this question, when you want to get some data in Magento, you call it like this: Mage::getModel('module/model')->load(1);

where 1 is the primary key id for some Regular/Simple table, while in EAV so many tables are joined to fetch just single row of data.

Magic function and it's use in magento

As we know in pho 5 they have implemented some magic function which is as follow. 
 __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state() and __clone().

In between that magento is using   __get, __set & __call


Thursday, May 22, 2014

Magento Custom single table extension

Suppose we have a table call "customer_product_like" having field (entity_id, store_id, customer_id, )

Now i want to insert data in this table then the code will be
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$fields = array();
$fields['status']= $postData['like'];
$fields['store_id']= Mage::app()->getStore()->getId();
$fields['product_id']= $postData['prodId'];
$fields['customer_id']= $customerId;
$connection->insert('customer_product_like', $fields);

for update
$where = "entity_id = '".$customerProductLikeId."'";
$connection->update('customer_product_like', $fields, $where);

For select
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
 $select = $connection->select()
                    ->from('customer_product_like', array('*'))
                    ->where("product_id = '".$prodId."' AND customer_id = '".$customerId."' AND store_id = '".$storeId."'");
$result = $connection->fetchRow($select);   OR   $result = $connection->fetchAll($select);

Wednesday, May 21, 2014

How to get all available shipping carrier rate by zip code and country code in Magento

If you have country code and zip code then you can find out the all available shipping carrier rate by following code in magento.


$zipcode = '711101';
$country = 'IN';                    
          
$cart = Mage::getSingleton('checkout/cart');
$address = $cart->getQuote()->getShippingAddress();

$address->setCountryId($country)->setPostcode($zipcode)->setCollectShippingrates(true);
$rates = $address->collectShippingRates()->getGroupedAllShippingRates();

foreach ($rates as $carrier) {
             foreach ($carrier as $rate) {
                            print_r($rate->getData());
               }
 }