henrik242
henrik242

Reputation: 386

Generate doctrine entity with string id from command line

I like to use the command line to generate entity stubs for doctrine.

For a specific entity the id should be a unique string rather than the default integer with auto increment.

1: How do I specify this in a command like the following?

php bin/console generate:doctrine:entity --entity=AppBundle:Game

2: In the entity class, is it possible to specify some function that will return this string (similar to the 'strategy' attribute)? Something a la...

@ORM\GeneratedValue(customFunc="getUniqueString")

Upvotes: 1

Views: 4875

Answers (1)

xurshid29
xurshid29

Reputation: 4210

Create your own ID generator.

Steps:

  1. create ID generator class which extends AbstractIdGenerator
  2. implement generate method
  3. use CUSTOM strategy in your entity definition
  4. Add CustomIdGenerator annotation with your Id generator class name

Example:

ID generator

class UniqueStringGenerator extends AbstractIdGenerator
{
    public function generate(EntityManager $em, $entity)
    {
        // Your logic here
    }
}

Entity:

/**
* @ORM\Id
* @ORM\Column(name="id", type="string")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="MYMODULE\Doctrine\UniqueStringGenerator")
*/
private $id;

Upvotes: 1

Related Questions