Skip to content

You have an error in your SQL syntax from CampaignAuditService->addWarningForUnpublishedEmails() #15613

@Moongazer

Description

@Moongazer

Mautic Series

5.2.x series

Mautic installed version

5.2.6

Way of installing

I installed with composer using https://github.com/mautic/recommended-project

PHP version

8.1

What browsers are you seeing the problem on?

Firefox

What happened?

We migrating from Mautic v4 to v5. So far everything works fine, except if we try to edit any of our existing Campaings.

On each save (it does not matter how simple the Campaign is), the site crashes and displays "Uh oh! I think I broke it. If I do it again, please report me to the system administrator!"

In the mautic_prod-* log the following entry is added (shorted, see full line below): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1

Setting a debug breakpoint for Doctrine\DBAL\Connection, the following strack trace is shown (shorted):

//...
Connection.php:1100, Doctrine\DBAL\Connection->executeQuery()
BasicEntityPersister.php:995, Doctrine\ORM\Persisters\Entity\BasicEntityPersister->loadAll()
EntityRepository.php:224, Doctrine\ORM\EntityRepository->findBy()
CampaignAuditService.php:25, Mautic\CampaignBundle\Service\CampaignAuditService->addWarningForUnpublishedEmails()
CampaignSubscriber.php:38, Mautic\CampaignBundle\EventListener\CampaignSubscriber->onCampaignPostSave()
//...

Still, the changes of the campaign are saved. We checked if there are any unpublished emails or marketing-messages, but there are not.

Here is the SQL query which causes the issue (shorted):

SELECT t0.is_published AS is_published_1, t45.email_id AS email_id_50
FROM emails t0
LEFT JOIN emails_draft t45 ON t45.email_id = t0.id WHERE t0.id IN ()

Probably the reason here is the empty IN () condition. But we could not find out why this happens and why nobody else had this issue since v5 is out a while?!

Environment

Only the plugin "acquia/mc-cs-plugin-custom-objects": "5.x-dev" is installed, all other custom plugins are uninstalled for testing (cache cleared), but still the issue occurs.

How can we reproduce this issue?

  1. Create and edit a campaign (Edit 1: IMPORTANT, the campaign must be published to reproduce the issue, see CampaignSubscriber::onCampaignPostSave()!)
  2. Select a segment and add an action (e.g. Add DNC, but could be anything else)
  3. Click "save"
  4. Builder closes and described error is shown + log entry is created
Image Image

Relevant log output

# Full line in `mautic_prod-*` log:
[2025-11-05T14:36:06.955790+00:00] mautic.CRITICAL: Uncaught PHP Exception Doctrine\DBAL\Exception\SyntaxErrorException: "An exception occurred while executing a query: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1" at /var/www/html/vendor/doctrine/dbal/src/Driver/API/MySQL/ExceptionConverter.php line 86 {"exception":"[object] (Doctrine\\DBAL\\Exception\\SyntaxErrorException(code: 1064): An exception occurred while executing a query: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1 at /var/www/html/vendor/doctrine/dbal/src/Driver/API/MySQL/ExceptionConverter.php:86)\n[previous exception] [object] (Doctrine\\DBAL\\Driver\\PDO\\Exception(code: 1064): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1 at /var/www/html/vendor/doctrine/dbal/src/Driver/PDO/Exception.php:28)\n[previous exception] [object] (PDOException(code: 42000): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1 at /var/www/html/vendor/doctrine/dbal/src/Driver/PDO/Connection.php:71)"} {"hostname":"my-mautic-web","pid":1711}

Code of Conduct

  • I confirm that I have read and agree to follow this project's Code of Conduct




Care about this issue? Want to get it resolved sooner? If you are a member of Mautic, you can add some funds to the Bounties Project so that the person who completes this task can claim those funds once it is merged by a member of the core team! Read the docs here.

Edit 1:

I could fix the issue by adding an if-condition to check if $emailIds is empty in CampaignBundle/Service/CampaignAuditService->addWarningForUnpublishedEmails(). Should I commit a patch for this solution?

    public function addWarningForUnpublishedEmails(Campaign $campaign): void
    {
        $emailIds = $this->campaignRepository->fetchEmailIdsById($campaign->getId());

        if (!empty($emailIds)) { // add this condition to avoid the empty IN()
            $emails   = $this->emailRepository->findBy(['id' => $emailIds]);

            foreach ($emails as $email) {
                if (!$email->isPublished()) {
                    $this->setEmailWarningFlashMessage($email);
                }
            }
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssues or PR's relating to bugscampaignsAnything related to campaigns and campaign builder

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions