Answers.Body: <p>The <code>init</code> function should only trigger once for each instance. So when you do something like <code>new Plugin</code> it is executed multiple times. </p>
  
  <p>But keep in mind that it is executed in <strong>every</strong> request. So when you are in the CP there are many different ajax calls (every time when you reload entries at the index page for example). You'll create a new row for each of those requests</p>
  
  <p>In order to disable your function you can check </p>
  
  <pre><code>$request = Craft::$app-&gt;getRequest();
  if (!$request-&gt;getIsAjax() &amp;&amp; !$request-&gt;getIsConsoleRequest()) {
  
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-26T14:09:52.320Z
Answers.LastActivityDate: 2018-04-26T14:09:52.320Z
Answers.UserDisplayName: Robin Schambach
Body: <p>To test a service method, I'm executing it in my init function. The method adds a row to the database table with a mail address and variant id:</p>
  
  <pre><code>$this-&gt;waitingListService-&gt;add(7, 'email@mailserver.com');
  </code></pre>
  
  <p>However, when I refresh a page in the control page to execute the plugin init function 2, or sometimes 3 rows are being added to the database table.</p>
  
  <p>Is this the default behaviour and if not, how can I fix it?</p>
  
  <p>Thanks in advance,
  Sam</p>
  
Tags: <plugin-development><craft3>
Title: Plugin init function executed more than once
ThreadText: # Plugin init function executed more than once
  
  (Asked by: username_0 on 2018-04-26)
  
  To test a service method, I'm executing it in my init function. The method adds a row to the database table with a mail address and variant id:
  
  ```
  $this->waitingListService->add(7, 'email@mailserver.com');
  
  ```
  
  However, when I refresh a page in the control page to execute the plugin init function 2, or sometimes 3 rows are being added to the database table.
  
  Is this the default behaviour and if not, how can I fix it?
  
  Thanks in advance,
  Sam
  
  **Tags:** plugin-development, craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-04-26. Score: 3 | Accepted answer
  
  The `init` function should only trigger once for each instance. So when you do something like `new Plugin` it is executed multiple times.
  
  But keep in mind that it is executed in **every** request. So when you are in the CP there are many different ajax calls (every time when you reload entries at the index page for example). You'll create a new row for each of those requests
  
  In order to disable your function you can check
  
  ```
  $request = Craft::$app->getRequest();
  if (!$request->getIsAjax() && !$request->getIsConsoleRequest()) {
  
  }
  
  ```
  
  
  ---
  
  