title: Yii2 Insert Ignore / Insert duplicates
body: <p>I'm relatively new to Yii2 and so far I'm enjoying it very much.
  I'm however finding an issue related with Database Inserts, more specifically MySQL.</p>
  
  <p>The question is fairly simple: I'm inserting values into a table and I need to check for duplicates so my idea was to use an <strong>INSERT IGNORE</strong> or <strong>ON DUPLICATE KEY UPDATE</strong>.</p>
  
  <p>This is the code I'm using for the insert:</p>
  
  <pre><code>$db-&gt;createCommand()
  -&gt;insert('sdg_fb_apps', [
  'fb_id' =&gt; $app-&gt;id,
  'page_id' =&gt; $page_id,
  'name' =&gt; $app-&gt;name,
  'app_id' =&gt; $app_id,
  'app_name' =&gt; $app_name,
  ])
  -&gt;execute();
  </code></pre>
  
  <p>I have to unique keys: id and app_id and if I run this code I'll obviously be thrown an Exception from Yii.</p>
  
  <p>There's this extension but as far as I understood only works with Yii 1.0: <a href="http://www.yiiframework.com/extension/rdbcommand" rel="nofollow">http://www.yiiframework.com/extension/rdbcommand</a></p>
  
  <p>I also found this code snippet on <a href="https://github.com/yiisoft/yii2/issues/5138" rel="nofollow">GitHub</a> that looks like this:</p>
  
  <pre><code>class MyQueryBuilder extends yii\db\mysql\QueryBuilder
  {
      public function batchInsert($table, $columns, $rows)
      {
          $sql = parent::batchInsert($table, $columns, $rows);
          $sql .= 'ON DUPLICATE KEY UPDATE';
          return $sql;
      }
  }
  </code></pre>
  
  <p>I didn't understand however how to extend the Querybuilder, can it be through a component?</p>
  
  <p>I kind of accomplished what I was looking for by doing a query to see if the app_id was already in the database before and then only insert if they don't exist, however this is supposed to have to scale to huge numbers and this method might not be the best.</p>
  
  <p>Any guidance will be appreciated, thanks in advance.</p>