Magento: problem with addFieldToFilter

After updating magento 1.9.0.1 with some patches, an extension from a third party suddenly created problems. When calling the page, magento reported the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CAST(CONCAT(year, "-", month, "-01") AS DATE)' in 'where clause'

This worked perfectly before and so I searched and found the problem in lib/Varien/Data/Collection/Db.php. In the third party extension there was the following code:

$entry->addFieldToFilter('CAST(CONCAT(year, "-", month, "-01") AS DATE)', array('from' => $fromDate, 'to' => $toDate));

Since the patch addFieldToFilter has changed, and all values are now put in quotes, which made the query look like this:

SELECT * FROM table WHERE `CAST(CONCAT(year, "-", month, "-01") AS DATE)` ...

This doesn’t work, of course. So it’s necessary to make Magento not quote this specific value and what I found was a function named addFilterToMap which I used to achieve what I needed:

$entry->addFilterToMap('CAST(CONCAT(year, "-", month, "-01") AS DATE)', 'CAST(CONCAT(year, "-", month, "-01") AS DATE) '); //mit einem leerzeichen danach 
$entry->addFieldToFilter('CAST(CONCAT(year, "-", month, "-01") AS DATE)', array('from' => $fromDate, 'to' => $toDate));

And it works. I’m sure this is not ideal but it helped me temporarily.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.