title: How to update/insert array of values to database using Yii2 ( many values for one id that repeats )
body: <p>I am collecting some data from database and then display it in index view. There I have check box near every row, so I can allow users to pick their favorites. </p>
  
  <p><img src="https://i.stack.imgur.com/PhGnl.png" alt="enter image description here"></p>
  
  <p>Picture and text is selected from one table <code>cpv</code>. That table has fields: <code>id</code>, <code>title</code>, <code>image</code>. Since logged in user can pick up to 3 of the entries as favorites, I am having table to store relation between user and cpv.id he chose. <code>user_cpv</code> table has columns: <code>id</code>, <code>user_id</code>, <code>cpv_id</code>.</p>
  
  <p>I made some dirty way of displaying data + checkboxes, and I made some way of passing information ( cpv.id ) to the actionUpdate that should save cpv.id's. But I can not figure out how to save all of this to <code>user_cpv</code> table. Can someone give me some idea about how to do this properly and how to do validation that user can not select more than 3 boxes? Here is my code:</p>
  
  <p>index view : </p>
  
  <pre><code>&lt;?php foreach ($values as $data): ?&gt;
      &lt;tr&gt;
          &lt;td&gt;Img goes here&lt;/td&gt;
          &lt;td&gt;Title goes here&lt;/td&gt;
  
          &lt;?php // here I have some dirty code, for each row displayed I am executing query that should find entry in 
                // user_cpv table where cpv_id there is == with the cpv.id taken drom cpv table.
                // can this be done more efficient ?
           ?&gt;
  
          &lt;?php $cpv = UserCpv::getCpvByCpvId($data['cpvId']) ?&gt;
          &lt;?php if ($cpv): ?&gt;
              &lt;td&gt;&lt;?= Html::checkbox('favorite[]', true, ['value' =&gt; $data['cpvId']]) ?&gt;&lt;/td&gt;
          &lt;?php else: ?&gt;
              &lt;td&gt;&lt;?= Html::checkbox('favorite[]', false, ['value' =&gt; $data['cpvId']]) ?&gt;&lt;/td&gt;
          &lt;?php endif ?&gt;
  
      &lt;/tr&gt;
  &lt;?php endforeach ?&gt;
  </code></pre>
  
  <p>Form is opened with Html::form:</p>
  
  
  
  <p>My actionUpdate:</p>
  
  <pre><code>public function actionUpdate()
  {
      $userId = Yii::$app-&gt;user-&gt;identity-&gt;id;
  
      // array of checked cpv.ids (['0' =&gt; someId, ['1' =&gt; otherCheckedId]])
      $favorites = Yii::$app-&gt;request-&gt;post('favorite');
  
      $model = UserCpv::findOne(['user_id' =&gt; $userId]);
  
      if ($model) 
      {
          // this does not work
          $update = UserCpv::updateAll(['cpv_id' =&gt; $favorites], "user_id = $userId");
  
          return $this-&gt;render('index', [
              'model' =&gt; $model
          ]);
      } 
      else 
      {
          # code...
      }
      // ???
  }
  </code></pre>
  
  <p>Do anyone have any idea how this should be done properly ?</p>