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


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()) {


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.