0

I'm trying to return a list of Time formatted fields from a MySQL database and using the Cake\I18n\Time class to manipulate them. Currently, when the data is taken from the database, the current date is used for these fields. I'd like to set my own date on them (as the data can be for multiple dates) but I can't seem to find an elegant solution.

So far my solution during testing has been to set the date using Time::setTestNow($now) and then creating a function in my Entity to set the date correctly, like so:

$newTime = new Time();
$newTime->hour($savedTime->format("H"))
->minute($savedTime->format("i"))
->second($savedTime->format("s"));

Obviously this seems like a fairly cumbersome method of setting the date for these fields. I have other functions in my application that will need to compare dates as well as times (the date can be taken from another table) so Time::setTestNow() seems like the wrong way to properly set the correct date, and setting the day, month and year for each new time also seems to be a cumbersome solution.

Are there other methods I can be using to set the date when the time is pulled from the database? I'd much rather have the correct time in my Model than have to convert it each time I want to use it).

Thanks.

Matthew Simpson
  • 153
  • 1
  • 16
  • I don't get our problem? You have dates stored in the DB and they're not what you want and you want to manipulate them when echoing them? Why not storing the right date in the first place? What is the format of the date in the DB and the field type? – floriank Jan 29 '16 at 15:37
  • Hi, I have times stored in the database as time fields, the dates are stored in another table as date fields. I need to be able to set the correct date on the time so that I can do comparisons to them. – Matthew Simpson Jan 29 '16 at 16:11
  • This sounds like a flawed design approach. – floriank Jan 29 '16 at 16:22
  • I'm trying to implement a [GTFS](https://en.wikipedia.org/wiki/General_Transit_Feed_Specification) style system, so I need to keep my dates and times separate. – Matthew Simpson Jan 29 '16 at 16:57
  • Interesting. And my answer doesn't resolve this problem? – floriank Jan 29 '16 at 17:20
  • Possible duplicate of [this](http://stackoverflow.com/questions/30960843/cakephp-3-time-column-gets-date-added)? – Greg Schmidt Jan 30 '16 at 17:53

1 Answers1

0

Are there other methods I can be using to set the date when the time is pulled from the database?

Entity Accessors. See virtual properties as well to implement this right. Example taken from the book:

class Article extends Entity
{
    protected function _getTitle($title)
    {
        return ucwords($title);
    }
}

Just concat the date and time inside your customized getter.

I have times stored in the database as time fields, the dates are stored in another table as date fields. I need to be able to set the correct date on the time so that I can do comparisons to them.

This sounds like a bad design in the first place. Why can't you save them combined in a third field on save if you have to keep them separated? However, you could probably concat the fields in the DB query itself (Combining (concatenating) date and time into a datetime) and map a datetime type to it using the CakePHP ORMs type mapping. Maybe you're lucky and it will automatically detect it as the correct type.

Community
  • 1
  • 1
floriank
  • 25,546
  • 9
  • 42
  • 66