Answers.Body: <p>All of the element saving events (like <code>onBeforeElementSave()</code>, <code>onElementSave()</code>, <code>onBeforeSaveEntry()</code>..., etc. are all wrapped in a <a href="https://en.wikipedia.org/wiki/Database_transaction" rel="nofollow noreferrer">database transaction</a> (specifically encapsulated in the <a href="http://www.yiiframework.com/doc/api/1.1/CDbTransaction" rel="nofollow noreferrer">CDbTransaction</a> class).</p>
  
  <p>When you call <code>exit</code>, you're killing the request and never giving a chance for the transaction to commit to the database.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-16T22:01:54.420Z
Answers.LastActivityDate: 2017-05-16T22:01:54.420Z
Answers.UserDisplayName: Brad Bell
Body: <p>I've created a database table that I am trying to add information too with a <code>onBeforeSaveElement()</code> hook.</p>
  
  <p>For some reason, although it is telling me that the rows have been added in the <code>$result</code>, there is no records being added. Interestingly, if I run the page an then manually add a record to the database table, the <code>id</code> has been incremented by the amount of rows <code>$result</code> says it has added.. so it seems that the rows are being added then instantly deleted?</p>
  
  <p>Thanks</p>
  
  <p><strong>SQL</strong></p>
  
  <pre><code>CREATE TABLE `craft_lsclearance` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `path` text,
    `cleared` int(11) DEFAULT '0',
    `dateCreated` datetime DEFAULT NULL,
    `dateUpdated` datetime DEFAULT NULL,
    `uid` char(36) DEFAULT '',
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=229 DEFAULT CHARSET=latin1;
  </code></pre>
  
  <p><strong>Function</strong></p>
  
  <pre><code>craft()-&gt;on('elements.onBeforeSaveElement', function(Event $event)
      {
          $element = $event-&gt;params['element'];
          $paths = craft()-&gt;plugin-&gt;getPaths($element);
  
          foreach ($paths as $path) {
              $newPath = preg_replace('/site:/', '', $path['path'], 1);
  
              if ($path['locale'] != 'en') {
                  $newPath = $path['locale'] . '/' . $newPath;
              }
  
              $model[] = array(UrlHelper::getSiteUrl($newPath));
          }
  
          $result = craft()-&gt;db-&gt;createCommand()-&gt;insertAll('lsclearance', array('path'), $model);
  
          print_r($model);
          echo '&lt;br /&gt; Inserted: ' . $result . 'row(s)';
  
          exit;
  
      }
  );
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>Array ( 
    [0] =&gt; Array ( [0] =&gt; http://mydomain.com/ ) 
    [1] =&gt; Array ( [0] =&gt; http://mydomain.com/ ) 
    [2] =&gt; Array ( [0] =&gt; http://mydomain.com/results?q=regatta+ac ) 
    [3] =&gt; Array ( [0] =&gt; http://mydomain.com/watch/regatta-ac-titanium/19225 ) 
    [4] =&gt; Array ( [0] =&gt; http://mydomain.com/mens-watches ) 
    [5] =&gt; Array ( [0] =&gt; http://mydomain.com/mens-watches/p2 ) 
    [6] =&gt; Array ( [0] =&gt; http://mydomain.com/watch/ac-ii/19282 ) 
  ) 
  Inserted: 7 rows
  </code></pre>
  
Tags: <plugin-development><plugins>
Title: insertAll() not inserting entries into database
ThreadText: # insertAll() not inserting entries into database
  
  (Asked by: username_0 on 2017-05-16)
  
  I've created a database table that I am trying to add information too with a `onBeforeSaveElement()` hook.
  
  For some reason, although it is telling me that the rows have been added in the `$result`, there is no records being added. Interestingly, if I run the page an then manually add a record to the database table, the `id` has been incremented by the amount of rows `$result` says it has added.. so it seems that the rows are being added then instantly deleted?
  
  Thanks
  
  **SQL**
  
  ```
  CREATE TABLE `craft_lsclearance` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `path` text,
    `cleared` int(11) DEFAULT '0',
    `dateCreated` datetime DEFAULT NULL,
    `dateUpdated` datetime DEFAULT NULL,
    `uid` char(36) DEFAULT '',
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=229 DEFAULT CHARSET=latin1;
  
  ```
  
  **Function**
  
  ```
  craft()->on('elements.onBeforeSaveElement', function(Event $event)
      {
          $element = $event->params['element'];
          $paths = craft()->plugin->getPaths($element);
  
          foreach ($paths as $path) {
              $newPath = preg_replace('/site:/', '', $path['path'], 1);
  
              if ($path['locale'] != 'en') {
                  $newPath = $path['locale'] . '/' . $newPath;
              }
  
              $model[] = array(UrlHelper::getSiteUrl($newPath));
          }
  
          $result = craft()->db->createCommand()->insertAll('lsclearance', array('path'), $model);
  
          print_r($model);
          echo '<br /> Inserted: ' . $result . 'row(s)';
  
          exit;
  
      }
  );
  
  ```
  
  **Output**
  
  ```
  Array ( 
    [0] => Array ( [0] => http://mydomain.com/ ) 
    [1] => Array ( [0] => http://mydomain.com/ ) 
    [2] => Array ( [0] => http://mydomain.com/results?q=regatta+ac ) 
    [3] => Array ( [0] => http://mydomain.com/watch/regatta-ac-titanium/19225 ) 
    [4] => Array ( [0] => http://mydomain.com/mens-watches ) 
    [5] => Array ( [0] => http://mydomain.com/mens-watches/p2 ) 
    [6] => Array ( [0] => http://mydomain.com/watch/ac-ii/19282 ) 
  ) 
  Inserted: 7 rows
  
  ```
  
  **Tags:** plugin-development, plugins
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-05-16. Score: 1 | Accepted answer
  
  All of the element saving events (like `onBeforeElementSave()`, `onElementSave()`, `onBeforeSaveEntry()`..., etc. are all wrapped in a [database transaction](https://en.wikipedia.org/wiki/Database_transaction) (specifically encapsulated in the [CDbTransaction](http://www.yiiframework.com/doc/api/1.1/CDbTransaction) class).
  
  When you call `exit`, you're killing the request and never giving a chance for the transaction to commit to the database.
  
  
  ---
  
  