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

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

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程