CakePHP在MySQL中错误的日期 [英] CakePHP wrong date in MySQL

查看:107
本文介绍了CakePHP在MySQL中错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL中有一个名为issue_date的字段,该字段在MySQL中是datetime类型,我更改为date。



当我试图将日期保存到数据库正在使用

保存它(例如2196),当实际日期是2003时,我在保存对象之前进行了输出,并显示实际日期(2003-06-30)



字段为 issue_date



函数保存:

  public function populate(){
$ this-> tangoModel = $ this-> loadModel('Tango');
$ tango = $ this-> tangoModel-> getInvoices();
$ this-> obrasModel = $ this-> loadModel('Obras');
$ obras = $ this-> obrasModel-> getObras();
foreach($ tango as $ invoice){
$ data [] =([
'issue_date'=> date('Ym-d',strtotime($ invoice ['issue_date' ])),
'type'=> $ invoice ['type']
]
);
}
print_r($ data);
$ invoices = $ this-> Invoices-> newEntities($ data);
foreach($ invoices as $ invoice){
if($ this-> Invoices-> save($ invoice)){
$ this-> Flash-> success __('发票已保存。'));
};
}


}

数据数组的一个对象的输出:

  [0] => array 

[issue_date] => 2003-06-30,
[type] => CRE

/ pre>

这是如何在MySQL中插入的输出

  [{id:1,
issue_date:2196-11-06,
type:CRE
}]

这是在 / Model / Table

  public function validationDefault(Validator $ validator)
{
$ validator
- > add('id','valid',['rule'=>'numeric'])
- > allowEmpty('id','create');

$ validator
- > add('issue_date','valid',['rule'=>'date'])
- > allowEmpty ');
$ validator
- > allowEmpty('type');

return $ validator;
}
}

这是实体文件Invoice.php in / Model / Entity

  / ** 
*发票实体。
*
* @property int $ id
* @property \Cake\I18n\Time $ issue_date
* @property string $ type
* /
类发票extends Entity
{

/ **
*可以使用newEntity()或patchEntity()大量分配的字段。
*
*请注意,当'*'设置为true时,这允许所有未指定的字段
*进行质量分配。为了安全起见,建议将*设置为false
*(或删除它),并根据需要显式地使各个字段可访问。
*
* @var array
* /
protected $ _accessible = [
'*'=> true,
'id'=> false,
];
}

感谢

解决方案

它在控制器中添加了:

 使用Cake \Database\Type; 
Type :: build('date') - > setLocaleFormat('yyyy-MM-dd');

感谢ndm


I had a field called issue_date in a table that was datetime type in MySQL and I changed to date.

When I am trying to save the date to the database is saving it with
weird years(for example 2196), when the real date is 2003, I made an output of the object before saving it and is showing the real date (2003-06-30)

The field is issue_date.

This is my function to save:

public function populate(){
        $this->tangoModel=$this->loadModel('Tango');
        $tango=$this->tangoModel->getInvoices();
        $this->obrasModel=$this->loadModel('Obras');
        $obras=$this->obrasModel->getObras();
        foreach($tango as $invoice){
        $data[] = ([
        'issue_date' => date('Y-m-d', strtotime($invoice['issue_date'])),
            'type' => $invoice['type']
        ]
        );
        }
        print_r($data);
        $invoices = $this->Invoices->newEntities($data);
        foreach($invoices as $invoice){
        if ($this->Invoices->save($invoice)) {
                $this->Flash->success(__('The invoice has been saved.'));
        };
        }


    }

This is the output of one object of data array:

[0] => Array
    (
        [issue_date] => 2003-06-30,
        [type] => CRE
    )

And this is the output of how is inserted in MySQL

    [{"id":"1",
    "issue_date":"2196-11-06",
    "type":"CRE"
    }]

This is how the field is validated in the InvoicesTable file in /Model/Table:

public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('id', 'create');

        $validator
            ->add('issue_date', 'valid', ['rule' => 'date'])
            ->allowEmpty('issue_date');
         $validator
            ->allowEmpty('type');

        return $validator;
    }
}

This is the entity file Invoice.php in /Model/Entity

   /**
 * Invoice Entity.
 *
 * @property int $id
 * @property \Cake\I18n\Time $issue_date
 * @property string $type
 */
class Invoice extends Entity
{

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * Note that when '*' is set to true, this allows all unspecified fields to
     * be mass assigned. For security purposes, it is advised to set '*' to false
     * (or remove it), and explicitly make individual fields accessible as needed.
     *
     * @var array
     */
    protected $_accessible = [
        '*' => true,
        'id' => false,
    ];
}

Thanks

解决方案

It worked adding in the controller:

use Cake\Database\Type; 
Type::build('date')->setLocaleFormat('yyyy-MM-dd');

Thanks to ndm

这篇关于CakePHP在MySQL中错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆