LoginSignup
4
6

More than 5 years have passed since last update.

#CakePHP で BULK INSERT を試みる

Posted at

これは何か?

CakePHP 2.3.9では、まとめてsave()する仕組み(saveManyとか)でBULK INSERTにならないようなのでBULK INSERTを試みた際のメモ。

保存しようとしているデータ

$data = array();
$data[] = array(
    'group_id' => 1,
    'name' => 'NAME11',
);
$data[] = array(
    'group_id' => 2,
    'name' => 'NAME22',
);

BULK INSERT文を作って実行する

PHP 5.1.3以降

$ds = $this->Model->getDataSource();

$query  = 'INSERT INTO ' . $this->Model->useTable . '(' . implode(',', array_keys($data[0])) . ') VALUES';
$query .= '(' . implode('),(', array_fill(0, count($data), implode(',', array_fill(0, count($data[0]), '?')))) . ')';

$ds->query($query, iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($data)), false), false);

参考

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6