D
D
Denis2022-01-10 16:43:30
Doctrine ORM
Denis, 2022-01-10 16:43:30

Symfony migration can't figure out how it works?

Meet Symphony. But I'm confused about migrations. Symphony version 6. Since. then this version will have to be used.
I create entity through make. By default it is created with attributes. I think because 6 version"php": ">=8.0.2",

#[Column(type: 'string', length: 255)]
    private $prop;

I need to add a default value, I take an example from the doctrine docks, it turns out like this

#[Column(type: 'string', length: 255, options: ["default" => "value"])]
    private $prop;

I create a migration, I look, everything seems to be fine, I roll the migration into the database.
I think we need to check how the record is added to the database, whether everything is fine. I created a test controller, and I add a record like this with my hands. (for the test, entity 2 properties name is required and prop is optional where the default value is specified)
#[Route('/test', name: 'test')]
    public function test(EntityManagerInterface $entityManager): Response
    {
        $test = new Test();
        $test->setName('test name'.);

        $entityManager->persist($test);
        $entityManager->flush();

        return new Response(sprintf('New test id #%d', $test->getId()));
    }

I get an error prop cannot be null. It's weird I think what the hell.
As a result, it turned out to fix this error only by adding annotations instead of an attribute. (the table in the database still exists)
/**
* @Column(type="string", length=255, options={"default":"value"})
*/

after that the data is added to the table without errors. I think probably the attributes do not work correctly, it is necessary to do annotations.

I create another entity, let's say Test2, where there is also a default field and there I immediately correct the attribute for the annotation.
I want to make a migration of a new entity, I write in the console
doctrine:migrations:diff

a second migration is created, I immediately rolled it into the database, and here I did not immediately, but later noticed a strange thing for me.
the second migration removed the field from migration 1 to which I set the default, and created migration 2 without a field with a default value. Just ignored him is all.

As a result, while I was trying to understand what was happening, I found out the following by testing.
1. By creating an entity via make and adding a default value field to the attribute. The table is created correctly, but adding a record does not work.
2. Creating an entity through make and replacing the attribute with annotations does not correctly create a table. There are no fields where default values ​​are specified.

Can anyone suggest what is wrong? How should it be done correctly? It turns out that you need to do it first with attributes, create migrations and then change the attributes to annotations, and then it only seems to work. Or maybe I messed up something there while I was already figuring it out .. But I even tried to create a project from scratch to create two entities and it still doesn’t work without replacement.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question