Symfony Tips #11 - Use Uuids as identifiers

Symfony Tips #11 - Use Uuids as identifiers

Subscribe to my newsletter and never miss my upcoming articles

🖥 Symfony Tips: Quick and practical tricks to develop solid backend systems.

Now we have to wait until flush occurs to have a valid id for our Entities and sometimes we need that Id before that happens.

Also the urls to you resources will probably be consecutive numbers...bad idea.

This is OK

class User
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
    private $id;

This is better

class User
     * @ORM\Id
     * @ORM\Column(type="uuid")
    private Uuid $uuid;


Setting your entities Ids it's an important task. It shouldn't be database work, your domain logic should take care of it.

Now we can also test it.

    public function testGivenCorrectDataThenSaveUser()

        $expectedUser = (new User())


        $returnedUser = $this->useCase->__invoke(new Request([
            'uuid' => 'd9e7a184-5d5b-11ea-a62a-3499710062d0',
            'username' => 'username',
            'email' => ''

        $this->assertEquals($expectedUser, $returnedUser);

You can run php ./vendor/bin/phpunit to run the tests.

Symfony tip completed 👍! Check the final code and leave a ⭐️!

Next Tip -> Symfony Tips #12 - Move all business logic to your entities

Previous Tip -> Symfony Tips #10 - Test your Controllers

HEY! Follow me at @albertobeiz if you found this tip useful or have any question.

Share this