mySQL Insert failing : UUID becoming null on save() method with Doctrine and Symfony 6.0

Issue

I’ve been scratching my head over the past hour or so on this bug. I’m new to a Symfony project (and Symfony in general) and we are storing our data in a mySQL database, and some of our tables are using UUIDS for id in both primary and foreign key fields.

Here is some sample code from the project :

    public function createChannelHistory($consentParams, string $channelId, string $channelLabel): void
    {
        $channelHistory = new ChannelHistory();
        $channelHistoryId = Uuid::v6();
        $channelHistory->setId((string) $channelHistoryId);
        $channelHistory->setChannelId($channelId);
        $channelHistory->setLabel('Promotional ' . $channelLabel);
        $channelHistory->setIsOptIn($consentParams[$channelLabel . '_opt_in']);
        $channelHistory->setCreatedAt(new Carbon());

        var_dump('channelId : ' . $channelId, 'channelHistoryId : ' .     $channelHistory->getId());


        $this->channelHistoryRepository->save($channelHistory);
    }

Dumping both variables yields this : "string(48) "channelId : 1ecec3c8-71aa-61be-8f38-2f921a413b0f" string(60) " /// channelHistoryId : 1ecec3c8-71c8-6448-924f-2f921a413b0f"".

However, when reaching the save method, it seems $channelId gets replaced to null somehow, and insertion fails because our channel_id column in table is set to NOT NULL.

dev.log file contains this :

[2022-06-15T01:51:26.554679+02:00] doctrine.DEBUG: Executing statement: INSERT INTO channel_history (id, channel_id, label, is_opt_in, created_at, created_by) VALUES (?, ?, ?, ?, ?, ?) (parameters: array{"1":"\u001e���u�g������\b?�","2":null,"3":"Promotional mail","4":0,"5":"2022-06-15 01:51:26","6":null}, types: array{"1":2,"2":2,"3":2,"4":5,"5":2,"6":2}) {"sql":"INSERT INTO channel_history (id, channel_id, label, is_opt_in, created_at, created_by) VALUES (?, ?, ?, ?, ?, ?)","params":{"1":"\u001e���u�g������\b?�","2":null,"3":"Promotional mail","4":0,"5":"2022-06-15 01:51:26","6":null},"types":{"1":2,"2":2,"3":2,"4":5,"5":2,"6":2}} []
[2022-06-15T01:51:26.560858+02:00] app.ERROR: An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'channel_id' cannot be null

As you can see, right before the query both variables exist and are of type string, with values resembling what they should be.
I’m at a loss as to why the second id becomes null when running the query.

Help would be much appreciated.

Solution

What is the relationship between ChannelHistory and channelId? If Channel is an entity, check the doc about ManyToOne and entity, you should pass the instance of the entity, not an id. Doctrine will handle it.

Set the association between ChannelHistory and Channel:

// src/Entity/ChannelHistory.php
namespace App\Entity;

// ...
class ChannelHistory
{
    // ...

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Channel", inversedBy="histories")
     */
    private $channel;

    public function getChannel ?Channel
    {
        return $this->channel;
    }

    public function setChannel(?Channel $channel): self
    {
        $this->channel = $channel;

        return $this;
    }
}

Then you will be able to pass the entity directly to the function and to the entity:

    public function createChannelHistory($consentParams, Channel $channel, string $channelLabel): void
    {
        $channelHistory = new ChannelHistory();
        // …
        $channelHistory->setChannel($channel);
        // …
    }

Answered By – A.L

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published