1

A while ago I created a simple blog addon for Magento. Due to neeidng isotope it was kept simplistic.

However a while later it appears that the sort order isn't quite working. In both the backed and on the front end sorting by date is... weird.

I click on the date fiel in the back end to sort descending, and I get aug 13, jul 12, apr13 and so on. While this is all fine ad it has no impact on the front end is is worrying.

Weird date issue

The front end however is following these queer dates. Here is the code I'm using to get the model and sort by date:

<?php $posts = Mage::getModel('blog/blog')->getCollection()->setOrder('date', 'DESC'); ?>

From what I can tell it's working, but its sorting by these weird dates. Could this have been caused by a user manually typing in dates and not using a date picker?

One splution I can see is adding an "order" field to the module, however I used a module creator (http://www.silksoftware.com/magento-module-creator/) to build it and previously had a horrible time with updating modules and currently there is no dev version of the site I'm working on due to it's now grown size so I'm having to do small changes to a live version (yes horrible idea I know).

It's been so long since I made any modules for Magento and I really can;t think of where to start for debugging this so any help would be appreciated.

2
  • 1
    Can you post the format of the dates that are saved in the database? and the type of field Commented Sep 13, 2013 at 10:39
  • Well seems the module creator makes the date as"13/03/2012 12:19" instead of a timestamp. So yeah it's sorting it by the date correctly, by sorting numerically then alphabetically. Damn that plugin. Is there any way to parse the text date as a timestmp in setOrder at all or am I doomed to remake the date part of the module at this point? Commented Sep 13, 2013 at 10:46

1 Answer 1

2

It's probably due to the fact that month is stored as a string and not as a numeric value We've looked at 3 solutions

  1. Reformat the date value to use [yyyy]-[mm]-[dd] [hh]:[ii]:[ss] numeric values
  2. Converting the field to a time stamp so you order a simple integer, then format the date on the frontend
  3. Creating a second table field for the sorting containing one of the above options as values

We want with the third option in combination with a time stamp in the and as not to break the date displaying on the frontend.

Optionally check this answer on stack overflow on formatting a date value on the fly when querying to make it sortable / comparable.

7
  • Yeah it was a stupid oversight. I'm going to try adding an order field to the database. God only knows how much this will break after my simple Banner module broke 5 times every time thee client wanted another feature and columns needed adding. Commented Sep 13, 2013 at 11:01
  • set the default value of the database field to CURRENT_TIMESTAMP in MySQL so you don't need to change your adminhtml code. This will give you the numeric timestamp that you then can sort by in the collection Commented Sep 13, 2013 at 11:05
  • How would that effect the front end being set up as a date field with a date picker? Commented Sep 13, 2013 at 11:11
  • @ChrisMorris I'm not sure what you mean Commented Sep 13, 2013 at 11:12
  • In the back end of Magento if the user edits one of the blog posts they cna select the date picker popup and chose a date. This would then revert the field contents back to DD/MM/YYYY MM:SS instead of a time stamp would it not? Commented Sep 13, 2013 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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