I have following table (type, id, title).
When new object is created and type=1 id should be 1, next object with type=1 id=2... But when I create object with type=2 id should be 1.
How can I do it with Doctrine2 and Symfony2?
I have following table (type, id, title).
When new object is created and type=1 id should be 1, next object with type=1 id=2... But when I create object with type=2 id should be 1.
How can I do it with Doctrine2 and Symfony2?
Like other have said, Doctrine 2 does not do this out of the box. The common practice is to make the ID field auto increment and a primary key in which case this is not possible. Primary keys must be unique.
With that said, a solution could be to use the entity manage to find the maximum value of ID where the type is the selected type and incrementing that value by 1.
For example:
// Retrieve the current maximum ID for the selected type
$id = $em->createQuery("SELECT MAX(e.id) AS id FROM Entities\entity e WHERE e.type = ?1")
->setParameter(1, $type)
->getSingleScalarResult();
// Set your current entity's ID
$entity->setId(++$id);
// Save the entity
...