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 offline
 

X-Adept
  
Join Date: Apr 2016
Posts: 446
 

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: 14,197
 

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 offline
 

X-Adept
  
Join Date: Apr 2016
Posts: 446
 

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 offline
 

X-Adept
  
Join Date: Apr 2016
Posts: 446
 

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
  #5  
Old 10-16-2019, 12:12 AM
 
saboor saboor is offline
    
Join Date: Oct 2019
Posts: 1
 

Default Re: Date as column type?

* @Column (type="integer")
*/
protected $mydate


public getMydate {
return $this->mydate
}


public setMydate($value) {
$this->mydate=$value;
return $this;
}

Regards
PPCexpo
__________________
Saboor
Reply With Quote
  #6  
Old 12-18-2019, 10:38 AM
 
Ed B. Ed B. is offline
 

X-Adept
  
Join Date: Apr 2016
Posts: 446
 

Default Re: Date as column type?

Now it only works for 4 out of 8 formats. I don't know if this is because of x-cart update to 5.4.0.8 or upgrade of php/mariadb/apache or whatsoever server component, but the format 19-12-2019 (the one we were using!) works no longer.


Any idea where this comes from and how to solve it, other than switching the format?
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
  #7  
Old 12-31-2019, 10:42 AM
 
Ed B. Ed B. is offline
 

X-Adept
  
Join Date: Apr 2016
Posts: 446
 

Default Re: Date as column type?

Quote:
Originally Posted by saboor
* @Column (type="integer")
*/
protected $mydate



Thank you very much, but
  • I have data in cvs file to import where the column is dates.
  • I would like to be view/edit the data directly with mysql (using command line or phpmyadmin), and seeing timestamp instead of dates doesn't help
So I would like to keep the column type "date" if possible.
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
  #8  
Old 09-02-2022, 11:48 PM
 
david osei david osei is offline
    
Join Date: Sep 2022
Posts: 1
 

Default Re: Date as column type?

It now only works for four out of eight formats. It doesn't seem to be due to the latest version of X-Cart, nor has it been changed by any recent software updates. You may want to contact our support team for assistance.

* @Column (type="integer")
*/
protected $mydate


public getMydate {
return $this->mydate
}


public setMydate($value) {
$this->mydate=$value;
return $this;
}

Regards
Ways101
__________________
Ways101.com
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > General questions (X-Cart 5)



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 06:40 PM.

   

 
X-Cart forums © 2001-2020