Question

I recently updated my Magento to 1.8.0.1 to 1.9.2.4, after that is some code which is not working the way it was working before. In one of my custom module(purchased module) there was a collection filter like this-

$weekCollection = Mage::getModel('mymodule/mymodel')->getCollection()
         ->addFieldToFilter('id',16)
         ->addFieldToFilter('week(created_time, 1)',$week);

it's sql query was

SELECT `main_table`.* FROM `table_name` AS `main_table` WHERE (`id` = '16') AND (week(`created_time`, 1) = '13')

this query was working fine in older veriosn

after updating magento the query for that code is

SELECT `main_table`.* FROM `table_name` AS `main_table` WHERE (`id` = '16') AND (`week(created_time, 1)` = '13')

this is returning errror of Unknown column 'week(created_time, 1)' in 'where clause'

What change should I make in addFieldToFilter code to make the sql query same?

Was it helpful?

Solution

Try below code, that will work for you

$weekCollection = Mage::getModel('mymodule/mymodel')->getCollection()
     ->addFieldToFilter('id',16);

$weekCollection->getSelect()->where("week(created_time, 1) = $week"); 

OTHER TIPS

You can use:

$weekCollection = Mage::getModel('mymodule/mymodel')->getCollection() ->addFieldToFilter('id',`16) ->getSelect() ->where(new Zend_Db_Expr("week(created_time, 1) = $week"));

Update your code:

$_week = week(created_time, 1);
$weekCollection = Mage::getModel('mymodule/mymodel')->getCollection()
             ->addFieldToFilter('id',16)
             ->addFieldToFilter($_week,$week);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top