Eric Houvenaghel
Eric Houvenaghel

Reputation: 13

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.

Upvotes: 0

Views: 945

Answers (2)

Eric Houvenaghel
Eric Houvenaghel

Reputation: 13

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.

Upvotes: 1

Reisclef
Reisclef

Reputation: 2148

I wasn't 100% clear what you were trying to accomplish (see my comments), but I'm guessing that you want the fromUser() function instead, i.e.

$SugarTimeDate = $TimeDate->fromUser($lead->date_entered);

The reason why, is that Sugar prepares the data for the GUI (including formatting the date as per user preferences) at the point your code is being called. This includes stripping out the seconds. Doing the above fromUser() function will return a SugarDateTime object based on the current user's configured date format with a full date string as a "date" property. This, in turn, could be dealt with elsewhere by using this standard format.

Upvotes: 0

Related Questions