title: How to do a bulk database insert in Yii2?
body: <p>I'm wondering how you go about doing a bulk database <code>INSERT</code> in <code>Yii2</code>?</p>
  
  <p>For example a normal <code>INSERT</code> I would like so:</p>
  
  <pre><code>$sql = $this-&gt;db("INSERT INTO some_table(id,my_value) VALUES(:id,:my_value)");
  $sql-&gt;bindValues([':id' =&gt; $id, ':my_value' =&gt; $my_value]);
  $sql-&gt;execute();
  </code></pre>
  
  <p>Now what if I wanted to create a bulk <code>INSERT</code>?</p>
  
  <p>Without binding values you could it something like:</p>
  
  <pre><code>foreach ($foo as $bar) {
      $data_sql .= '(' . $id . ',' "'" . $bar . "'),"
  }
  
  $data_sql = rtrim($data_sql, ',');
  
  $sql = $this-&gt;db("INSERT INTO some_table(id,my_value) VALUES" . $data_sql);
  $sql-&gt;execute();
  </code></pre>
  
  <p>But how can you achieve this if you still want to bind the values?</p>
  
  <p><strong>Edit:</strong> This question is not the same as the linked one as I am not using <code>ActiveRecord</code>.</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>Ideally it would be good if there was a solution that offered some flexibility, such as being able to write most of your own syntax, as one of the answers posted below:</p>
  
  <pre><code>Yii::$app-&gt;db-&gt;createCommand()-&gt;batchInsert('tableName', ['id', 'title', 'created_at'], [
      [1, 'title1', '2015-04-10'],
      [2, 'title2', '2015-04-11'],
      [3, 'title3', '2015-04-12'],
  ])-&gt;execute();
  </code></pre>
  
  <p>...doesn't offer that. For this particular situation I need to use the <code>IGNORE</code> statement, which the above solution doesn't offer.</p>