Answers.Body: <p>There isn't an explicit <code>sync</code> command, but Yii does support <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.arr#declaring-relationship" rel="nofollow">MANY_MANY</a> relationships through Active Record and will <a href="https://craftcms.stackexchange.com/questions/1738/plugins-managing-related-records/1739#1739">keep the data in the join table up-to-update automatically</a> if you stick purely with Active Record.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-08T17:39:54.370Z
Answers.LastActivityDate: 2015-07-08T17:39:54.370Z
Answers.UserDisplayName: Brad Bell
Body: <p>I have a record for an entity my plugin needs and that may be related to many Entries, so I have a pivot table, much like Craft's usergroups_user table.</p>
  
  <p>I'm creating and updating my table fine and I'm not looking at my pivot table. I notice the the user group approach is to delete any entries before creating new ones:</p>
  
  <pre><code>public function assignUserToGroups($userId, $groupIds = null)
  {
      craft()-&gt;db-&gt;createCommand()
          -&gt;delete('usergroups_users', array('userId' =&gt; $userId));
  
      if ($groupIds)
      {
          if (!is_array($groupIds))
          {
              $groupIds = array($groupIds);
          }
  
          foreach ($groupIds as $groupId)
          {
              $values[] = array($groupId, $userId);
          }
  
          craft()-&gt;db-&gt;createCommand()-&gt;insertAll('usergroups_users', array('groupId', 'userId'), $values);
      }
  
      return true;
  }
  </code></pre>
  
  <p>I take it this is the best way to handle pivot tables and there is no 'sync' functionality similar to that found on Laravel's Eloquent class <a href="http://laravel.com/docs/5.1/eloquent-relationships" rel="nofollow">http://laravel.com/docs/5.1/eloquent-relationships</a>?</p>
  
Tags: <plugin-development><records>
Title: How to create plugin pivot table entries?
ThreadText: # How to create plugin pivot table entries?
  
  (Asked by: username_0 on 2015-07-08)
  
  I have a record for an entity my plugin needs and that may be related to many Entries, so I have a pivot table, much like Craft's usergroups\_user table.
  
  I'm creating and updating my table fine and I'm not looking at my pivot table. I notice the the user group approach is to delete any entries before creating new ones:
  
  ```
  public function assignUserToGroups($userId, $groupIds = null)
  {
      craft()->db->createCommand()
          ->delete('usergroups_users', array('userId' => $userId));
  
      if ($groupIds)
      {
          if (!is_array($groupIds))
          {
              $groupIds = array($groupIds);
          }
  
          foreach ($groupIds as $groupId)
          {
              $values[] = array($groupId, $userId);
          }
  
          craft()->db->createCommand()->insertAll('usergroups_users', array('groupId', 'userId'), $values);
      }
  
      return true;
  }
  
  ```
  
  I take it this is the best way to handle pivot tables and there is no 'sync' functionality similar to that found on Laravel's Eloquent class <http://laravel.com/docs/5.1/eloquent-relationships>?
  
  **Tags:** plugin-development, records
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-07-08. Score: 1
  
  There isn't an explicit `sync` command, but Yii does support [MANY\_MANY](http://www.yiiframework.com/doc/guide/1.1/en/database.arr#declaring-relationship) relationships through Active Record and will [keep the data in the join table up-to-update automatically](https://craftcms.stackexchange.com/questions/1738/plugins-managing-related-records/1739#1739) if you stick purely with Active Record.
  
  
  ---
  
  