CakePHP 3.0: How to do an insert on duplicate key update?

风格不统一 提交于 2019-12-09 17:56:01

问题


I have an array $articles like this

[
    (int) 0 => [
        'id' => '-@940039',
        'xe_dsca' => 'ÜP2768G/1',
        'xe_citg' => '1F0200',
        'xe_cuni' => 'stk',
        'xe_seak' => 'ÜP2768G/1',
        'xe_seab' => '',
        'xe_wght' => '0.0153',
        'xe_cwun' => 'kg',
        'xe_wgap' => '2',
        'xe_seri' => '2',
        'xe_ltct' => '2',
        'xe_qual' => '2',
        'xe_hama' => '2',
        'xe_ctyo' => 'DE',
        'xe_ccde' => '85045095',
        'xe_cpln' => '240000',
        'xe_spar' => '2',
        'xe_wear' => '2',
        'xe_ctyo_de' => null,
        'xe_cean' => null,
        'xe_ewm_dsce' => null,
        'xe_cood' => null,
        'xe_ewm_dsne' => null,
        'xe_ewm_dsge' => null,
        'xe_ewm_dsen' => null,
        'xe_ewm_dscz' => null,
        'xe_wgh1' => null,
        'xe_wgh2' => null,
        'xe_wgh3' => null
    ],
    (int) 1 => [
        'id' => '000-000500-00000',
        'xe_dsca' => 'DUMMY ZEITBUCHUNG',
        'xe_citg' => '1F0800',
        'xe_cuni' => 'stk',
        'xe_seak' => 'DUMMY ZEITBUCHUN',
        'xe_seab' => '000-000500-00000',
        'xe_wght' => '0',
        'xe_cwun' => 'kg',
        'xe_wgap' => '2',
        'xe_seri' => '2',
        'xe_ltct' => '2',
        'xe_qual' => '2',
        'xe_hama' => '2',
        'xe_ccde' => '000',
        'xe_cpln' => '930000',
        'xe_spar' => '2',
        'xe_wear' => '2',
        'xe_ctyo' => null,
        'xe_ctyo_de' => null,
        'xe_cean' => null,
        'xe_ewm_dsce' => null,
        'xe_cood' => null,
        'xe_ewm_dsne' => null,
        'xe_ewm_dsge' => null,
        'xe_ewm_dsen' => null,
        'xe_ewm_dscz' => null,
        'xe_wgh1' => null,
        'xe_wgh2' => null,
        'xe_wgh3' => null
    ]

In my Controller I have these lines

foreach ($articles AS $article) {
    $query = $this->Articles->query();
    $query
        ->insert($required_article_fields)
        ->values($article)
        ->execute();
}

This code works fine, but has anyone a suggestion for me how to do an insert on duplicate key update? And what is the best practice to save many rows of data at once?


回答1:


You can use the query epilog() method

foreach ($articles AS $article) {
  $query = $this->Articles->query();
  $query
    ->insert($required_article_fields)
    ->values($article)
    ->epilog('ON DUPLICATE KEY UPDATE field=field+1')
    ->execute();
}

you can also pass a QueryExpression object to the method if you need to safely pass values.




回答2:


As suggested by https://stackoverflow.com/a/26590483/80353

you can do

$query = $this->Articles->query();
$query->insert($required_article_fields);


// need to run clause('values') AFTER insert()
$valuesExpression = $query->clause('values')->values($articles);

$query->values($valuesExpression)
      ->epilog('ON DUPLICATE KEY UPDATE `field1`=VALUES(`field1`) ... ')
      ->execute();

I didn't fill in all the fields you want to update because I don't know which field is part of your unique index.



来源:https://stackoverflow.com/questions/24852904/cakephp-3-0-how-to-do-an-insert-on-duplicate-key-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!