Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Date as column type?
 
Reply
   X-Cart forums > X-Cart 5 > General questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 09-04-2019, 11:52 PM
 
Ed B. Ed B. is online now
 

eXpert
  
Join Date: Apr 2016
Posts: 221
 

Default Date as column type?

I am writing a module with custom model, whose entities have dates as property.
It looks like x-cart handles dates as integers via timestamp, but I will be importing the entities from a CSV file, in which they are entered as dates. So my question is, can we do something like
Code:
/** *@Column (type="date") */ protected $name
I don't need to do much with stored dates, I just need to store, view, modify.
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
  #2  
Old 09-05-2019, 04:31 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,602
 

Default Re: Date as column type?

Sure you can. But then you will have to covert it to timestamp every time you need to use it (or within its get method) unless you create your own controls to work with this field.
It is probably best to just either modify the csv file and covert the dates to timestamp or do this on the fly with the import.
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote

The following user thanks cflsystems for this useful post:
Ed B. (09-05-2019)
  #3  
Old 09-15-2019, 10:00 AM
 
Ed B. Ed B. is online now
 

eXpert
  
Join Date: Apr 2016
Posts: 221
 

Default Re: Date as column type?

It turns out the things are a bit more complicated. The FormField class

View/FormField/Input/Text/Date.php, as its path suggests, treats dates as strings. So,
I can't do
Code:
* @Column (type="integer") */ protected $mydate public getMydate { return $this->mydate } public setMydate($value) { $this->mydate=$value; return $this; }
in the model class (or type="date" for that matter)
and use View/FormField/Input/Text/Date.php for editing the entity, i.e.
Code:
protected $schemaDefault = array( ... 'mydate. => array( self::SCHEMA_CLASS => 'XLite\View\FormField\Input\Text\Date' self::SCHEMA_LABEL => 'My Date', self::SCHEMA_REQUIRED => false,), ...
it won't save the date. For the dates to be saved this way, I need to declare the column type as string. The problem is, the "dates" saved this way becomes inaccessible once I change the default formatting of the dates for the store.


The only place, it seems to me, where there is the date to be input manually in X-Cart's original code, is the arrival date for products, but there, the class View/FormModel/Type/DatepickerType.php is used. My entities don't use the DTO, so I can't use this class. So, the question now is: how do I get around this problem?

I would need something like
Code:
public setMydate($value) { $value=strtotime($value); $this->mydate=$value; return $this; }
?
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
  #4  
Old 09-18-2019, 08:48 AM
 
Ed B. Ed B. is online now
 

eXpert
  
Join Date: Apr 2016
Posts: 221
 

Default Re: Date as column type?

I have found out a bit more. If I want to use XLite\View\FormField\Input\Text\Date and I use the date as the data type, then

the following code for setter works.




Code:
/** * Birth Date * @var string * * @Column (type="date") */ protected $birthdate = ''; public function setBirthdate($value) { $formats = \XLite\Core\Converter::getDateFormatsByStrftimeFormat(); $format = $formats[phpFormat]; $value = date_create_from_format($format, $value); $this->birthdate=$value; return $this; }
However, for the getter, I have a problem. If I write
Code:
public function getBirthdate() { $formats = \XLite\Core\Converter::getDateFormatsByStrftimeFormat(); $format = $formats[phpFormat]; return $this->birthdate->format($format); }
then it works for the 5 formats out of 8 possible, that is it doesn't work
for the formats 09-18-2019, 18.09.2019, 18-09-2019.


Any idea on how to solve this?
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > General questions (X-Cart 5)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 03:33 PM.

   

 
X-Cart forums © 2001-2018