title: How to set multi select value from array object in yii2 while updating
body: <p>I have table which have multiple reference to ohter tables like </p>
  
  <pre><code>user
  id         name         email
  categories
  id         title
  user_categories
  user_id    category_id
  </code></pre>
  
  <p>Here a user will have multiple category associated with him/her</p>
  
  <p>I am able to save these successfully with new records like following</p>
  
  <p><strong>View File:</strong></p>
  
  <pre><code>echo $form-&gt;field($package_categories, 'category_id')-&gt;dropDownList( ArrayHelper::map(
  StudyMaterialCategories::find()-&gt;all(), 'id', 'title'), 
  ['multiple' =&gt; true]
  );
  </code></pre>
  
  <p><strong>Save New record:</strong></p>
  
  <pre><code>$model = new Packages();
  $package_categories = new PackageCategories();
  $request = Yii::$app-&gt;request;
  if ($request-&gt;isPost) {
      $transaction = Yii::$app-&gt;db-&gt;beginTransaction();
      try {
          $post = $request-&gt;post();
          $model-&gt;load($post);
          $model-&gt;save();
          foreach ($post['PackageCategories']['category_id'] as $key =&gt; $value) {
              $package_categories = new PackageCategories();
              $package_categories-&gt;category_id = $value;
              $package_categories-&gt;package_id = $model-&gt;id;
              $package_categories-&gt;save();
          }
          $transaction-&gt;commit();
          return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;id]);
      } catch (Exception $ex) {
          $transaction-&gt;rolback();
          Yii::$app-&gt;session-&gt;setFlash("error", $ex-&gt;getMessage());
      }
  }
  </code></pre>
  
  <p>Till now It's running successfully. </p>
  
  <p>But I'm stuck when going to update the table. The problem part is dropdown list. How to set multiple selected option as per database if I'm coming with array of object.
  Have a look on the following code</p>
  
  <pre><code>$package_categories = PackageCategories::find()
  -&gt;where('package_id=:package_id', ['package_id' =&gt; $id])-&gt;all();
  if (count($package_categories) &lt; 1) {
      $package_categories = new PackageCategories();
  }
  $request = Yii::$app-&gt;request;
  if ($request-&gt;isPost) {
      $transaction = Yii::$app-&gt;db-&gt;beginTransaction();
      try {
          $post = $request-&gt;post();
          $model-&gt;load($post);
          $model-&gt;save();
          $package_categories = new PackageCategories();
          $package_categories-&gt;deleteAll(
              "package_id=:package_id", 
              [':package_id' =&gt; $model-&gt;id]
          );
          foreach ($post['PackageCategories']['category_id'] as $key =&gt; $value) {
              $package_categories = new PackageCategories();
              $package_categories-&gt;category_id = $value;
              $package_categories-&gt;package_id = $model-&gt;id;
              $package_categories-&gt;save();
          }
          $transaction-&gt;commit();
          return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;id]);
      } catch (Exception $ex) {
          $transaction-&gt;rolback();
          Yii::$app-&gt;session-&gt;setFlash("error", $ex-&gt;getMessage());
      }
  }
  </code></pre>
  
  <p>if I try to get first object of the array <code>$package_categories</code> of only able to set selected one option</p>