dougui4325 2015-12-14 15:35
浏览 41
已采纳

CakePHP在MySQL中的错误日期

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

  • 写回答

1条回答 默认 最新

  • dsajdgjadwqe3247382 2015-12-14 20:34
    关注

    It worked adding in the controller:

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

    Thanks to ndm

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?