Category Archives: Magento

Magento load model by field name

Magento load model by field name

You can load model in magento by field name ,column name as below


$model = Mage::getModel('yourcompany_yourmodule/modelname')->load($fieldvalue,'field_name'); 

or


$model = Mage::getModel('yourcompany_yourmodule/modelname');
$model = $model->load($columnvalue,'column_name'); 

Exception ‘Mage_Core_Exception’ with message ‘Invalid callback:

Exception ‘Mage_Core_Exception’ with message ‘Invalid callback:

This Error Occurs when company_module name is invalid and not added properly in model tab in cron.



    
        
            1.0.0
        
    

    
        
            
                Yourcompany_Yourmodule_Model_Observer
            
        
    

    
        
            
                
                    */15 * * * *
                
                
                    Yourmodule/observer::updateAll
                
            
        
    

Change it to the

 
        
            
                
                    */15 * * * *
                
                
                     yourcompany_yourmodule/observer::updateAll
                
            
        
    

Magento cron not working

Magento cron not working

Adding cron job in magento is very simple. You have to add only few lines of code to run and schedule the cron
job.

add the following lines of code in the config.xml


		
			
				
					*/15 * * * *
				
				
					modulename/observer::methodtocall
				
			
		



The Above cron will run after each 15 minutes.

If Cron job is not running please check the module name and cron observer or method you are calling.

Item with the same id already exist magento collection

item with the same id already exist magento collection

If Magento collection has duplicate id it throws the above exception.
To fix this problem make sure your collection do’nt have the duplicate records.
Check that the collection have single column with the name id(ie. in case of fetching data using joins there should be no duplicate column). Take Alias of the column if same in case of joins.

ex.

  
        $collection = Mage::getModel('test/user')->getCollection()->distinct()
            ->addFieldToSelect('name')
            ->addFieldToSelect('id');
        $collection->getSelect()->join( array('uc'=> user_profile), 'uc.user_id = main_table.id', array('uc.id'));

Should be
>addFieldToSelect(‘id’) field should have alias as ‘userId’ otherwise it will through the exception

  
        $collection = Mage::getModel('test/user')->getCollection()->distinct()
            ->addFieldToSelect('name')
            ->addFieldToSelect('id','userId');
        $collection->getSelect()->join( array('uc'=> user_profile), 'uc.user_id = main_table.id', array('uc.id'));

magento admin system configuration 404

magento admin system configuration 404

magento admin configuration 404 error

There may be few reasons which throws the above error .

1. Cache Issue-
-Flush Magento Cache.

-Logout .
-Login Again.

2 . You Have not Defined the acl in config.php file. Make Sure You have added the field in acl.
example :

 
   
    
       
           
                
                   
                       
                         
                              
                              100
                         
                       
                   
                
          
      
    
 

Your Fields should be defined in the acl children tag as :


      
         100

Define the values and again clear cache, logout and login .
It Will Work As expected.

Fatal error: Call to a member function setSaveParametersInSession() on a non-object in \mage\app\code\core\Mage\Adminhtml\Block\Widget\Grid\Container.php on line 66

Fatal error: Call to a member function setSaveParametersInSession() on a non-object in \mage\app\code\core\Mage\Adminhtml\Block\Widget\Grid\Container.php on line 66

This error occurs due to invalid grid path in the block constructor

example :

public function __construct(){
$this->_controller = ‘adminhtml_advertisement’; /**this sould be the path of the admin html files*/
$this->_blockGroup = ‘test_advertisement’; /** this should be proper path**/
parent::__construct();
$this->_headerText = Mage::helper(‘test_advertisement’)->__(‘Advretisement’);
$this->_updateButton(‘add’, ‘label’, Mage::helper(‘test_advertisement’)->__(‘Add Advretisement’));

}

Please dont be confused about $this->_controller this is not controller name this is the path of the grid which will generate Adminhtml_Advertisement_Grid

$this->_blockGroup is block namespce which you specify in config.xml file.
Note : : If the block groups and controller points the correct path to the grid (Grid.php) file there will occur no error so make sure the both of them produces the correct class name of the Grid.php

$this->_controller = ‘adminhtml_advertisement’;
$this->_blockGroup = ‘test_advertisement’;
should produce the following class name :

Test_Advertisement_Block_Adminhtml_Advertisement_Grid

example :

       
            
                Test_Advertisement_Block
            
        

Magento get current date time

Magento get current date time

You Can get Current date and time (according to timezone) in magento as following


Mage::getModel('core/date')->date('Y-m-d H:i:s');

Or You Can use following method

date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));

Magento get current customer id

Magento get current customer id

Use the following code to get current logged in customer’s Id :

if(Mage::getSingleton('customer/session')->isLoggedIn()) {
     $customerData = Mage::getSingleton('customer/session')->getCustomer();
     $customerId = $customerData->getId();
 }

can’t retrieve entity config magento

can’t retrieve entity config magento

This Error occurs basically due to config.php file’s Model setup
First of all check that table name and model name is correct in config.xml

Example :

  
                Demo_Model_Resource
                                    
                    
                        table_name

In the above example :

table_name


The model name and the table name should be correct .