2

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?

2

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"); 
1

You can use:

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

1

Update your code:

$_week = week(created_time, 1);
$weekCollection = Mage::getModel('mymodule/mymodel')->getCollection()
             ->addFieldToFilter('id',16)
             ->addFieldToFilter($_week,$week);
  • it returns fatal error Call to undefined function week() – Piyush Mar 30 '17 at 8:50
  • I've updated my answer, try again – St3phan Mar 30 '17 at 8:53
  • same error again – Piyush Mar 30 '17 at 8:59
  • I've updated my answer, try again – St3phan Mar 30 '17 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.