Reputation: 386
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
Reputation: 4210
Create your own ID generator.
Steps:
AbstractIdGenerator
generate
methodCUSTOM
strategy in your entity definitionCustomIdGenerator
annotation with your Id generator class nameExample:
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