datetimesugarcrmsugarbean

SugarCRM Get an effective TimeDate from a SugarBean field


I must be missing something obvious, but it seems that I'm unable to find a way to get the TimeDate object from the value of a SugarBean field.

Let's say I get a specific Lead with this kind of call:

$lead = BeanFactory::retrieveBean('Leads', "18bfc69e-8cd4-11e7-ad08-000c29b1a36e");

then any call to this:

$lead->date_entered

will return a string value: "2017-08-29 16:05" (note the absence of seconds).

So then, for example, if I try to use such value to create a SugarTimeDate:

$TimeDate = new TimeDate();
$SugarTimeDate = $TimeDate->fromDb($lead->date_entered);

it will return false, since the value provided to fromDb() is not in the proper format (the seconds are missing).

When looking at the SQL table with Toad, I can see that the information is effectively stored in the database as a DateTime, with the value 08/29/2017 16:05:56. But the SugarBean object provides it as a text with a format that is incomplete.

So how can you get the effective SugarTimeDate, TimeDate or DateTime from a Field in a given SugarBean, ideally as an object?

I searched, and all the example I found was about creating a new date object from Now to set to a field in a SugarBean, but none to set a datetime field from an existing datetime field.

Any hint would be highly appreciated.


Solution

  • By playing around, and with some help from Patrick McQueen, it appears there 2 ways to get the effective date value of a field.

    First solution I found was to do a SugarQuery with a select on the needed fields, which then returns the full date information, so "2017-08-29 16:05:56". A bit overkill, but it does the job.

    The other solution brought up by Patrick is to use the fetcher_row array from the bean object, which will return the full date information also. So:

    $lead->fetched_row['date_entered']
    

    will returns also "2017-08-29 16:05:56".

    So in any case an effective date is required ("round-trip" with a get then a set, or some sync requirement), the fetched_row[] is the solution, and the "direct" call to the field $bean->field is to be definitely avoided.