Magento question

0
257

difference between Mage::app and Mage:: in Magento

As a more general explanation and further to Alan’s answer Mage::app() and Mage::getModel() are both static methods of the Mage class (app/Mage.php).

The Mage::app() function is used to bootstrap your Magento application (setting up configuration, autoloading etc) and is useful when wanting to access Magento models in your own custom script for example.

It is a static method so it can also be called at any point in your application to return the current Mage_Core_Model_App object which you can use for example to get current configuration values e.g. Mage::app()->getStore() will return the current Mage_Core_Model_Store store object.

Mage::app() is similar to Mage::run() found in your index.php file. The difference is that Mage::run() will amongst other things also invoke the MVC, routing etc part of your application as per default and control the request/response directing you to a page and instantiating the blocks and layout template rendering.

Mage::getModel() is simply a factory method that will return a new instance of a class based on the class alias you provide. For example Mage::getModel(‘customer/customer’) will return a new Mage_Customer_Model_Customer object. If you want the same object back each time throughout your request you can use Mage::getSingleton() instead.

Difference between Mage::getSingleton() and Mage::getModel() in Magento

Mage::getSingleton() will first check the same class instance is exits or not in memory. If the instance is created then it will return the same object from memory. So Mage::getSingleton() faster then Mage::getModel().

Mage::getSingleton()

 

1

2

$product1 = Mage::getSingleton(‘catalog/product’);

$product2 = Mage::getSingleton(‘catalog/product’);

Mage::getgetModel()

Mage::getModel() will create a new instance of an object each time even such object exists in configuration.

Example

1

2

$product1 = Mage::getModel(‘catalog/product’);

$product2 = Mage::getModel(‘catalog/product’);

$product1 and $product2 both have different instant of same object and also occupy different memory .

Magento: Very Useful Collection Functions

http://blog.chapagain.com.np/magento-collection-functions/

There are different important functions that you can implement in your Collection object. The functions are present in Varien_Data_Collection_Db class. The class file is present in lib/Varien/Data/Collection/Db.php

Here are some of the functions that you can use in your collection object:-

/**

* Get Zend_Db_Select instance

*/

$collection->getSelect();

Â

/**

* Get collection size

*/

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

Â

/**

* Get sql select string or object

*/

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

Â

/**

* Add select order

*/

$collection->getSelect()->setOrder($field, $direction);

Â

/**

* Add field filter to collection

*

* If $attribute is an array will add OR condition with following format:

* array(

*     array(‘attribute’=>’firstname’, ‘like’=>’test%’),

*     array(‘attribute’=>’lastname’, ‘like’=>’test%’),

* )

*/

$collection->getSelect()->setOrder($field, $condition);

Â

/**

* Set select distinct

*/

$collection->getSelect()->distinct($flag);

Â

/**

* Get all data array for collection

*/

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

Â

/**

* Reset loaded for collection data array

*/

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

Â

/**

* Print and/or log query

*/

$collection->getSelect()->printLogQuery(true, true);

39. How will you log current collection’s SQL query?

$collection->printLogQuery(true); OR $collection->getSelect()->__toString();

40. How to get first item or last item from the collection?

$collection->getFirstItem() and $collection->getLastItem();

Question:How to add an external javascript/css file in Magento? ?
Add Css File

<action method="addCss"><stylesheet>css/yourstyle.css</stylesheet></action>

Add jS File

<action method="addJs"><script>js/yourfile.js</script></action>

Question: How to change the theme for login user?

if(Mage::getSingleton('customer/session')->isLoggedIn()):
Mage::getDesign()->setPackageName('package_name')->setTheme('themename');
endif;

17) type of session in magento

customer/session, checkout/session, core/session

Question: How to run Custom Query in Magento ?

$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$result=$db->query('SELECT * FROM users where id=4');