X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   General questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=66)
-   -   Date as column type? (https://forum.x-cart.com/showthread.php?t=77143)

Ed B. 09-04-2019 11:52 PM

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.

cflsystems 09-05-2019 04:31 AM

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.

Ed B. 09-15-2019 10:00 AM

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;
}

?

Ed B. 09-18-2019 08:48 AM

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?

saboor 10-16-2019 12:12 AM

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

Ed B. 12-18-2019 10:38 AM

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?

Ed B. 12-31-2019 10:42 AM

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.


All times are GMT -8. The time now is 02:36 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.