Comments (13)
I've bypassed this issue by adding an event listener to my code. My application is based on the Symfony Framework so your code might be different.
Let's say we have EntityA
and EntityB
and both have the following code:
/**
* @var \Ramsey\Uuid\Uuid
*
* @ORM\Column(type="uuid")
*/
private $uuid;
(Don't forget to create setUuid($uuid)
and getUuid()
on the entity classes)
Event listener:
<?php
namespace AppBundle\EventListener;
use AppBundle\Entity\EntityA;
use AppBundle\Entity\EntityB;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Ramsey\Uuid\Doctrine\UuidGenerator;
class UuidSubscriber implements EventSubscriber
{
/**
* @var UuidGenerator
*/
private $generator;
/**
* @param UuidGenerator $uuidGenerator
*/
public function __construct(UuidGenerator $uuidGenerator)
{
$this->generator = $uuidGenerator;
}
/**
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return [
'prePersist',
'preUpdate',
];
}
/**
* @param LifecycleEventArgs $args
*/
public function prePersist(LifecycleEventArgs $args)
{
$this->updateUuid($args);
}
/**
* @param LifecycleEventArgs $args
*/
public function preUpdate(LifecycleEventArgs $args)
{
$this->updateUuid($args);
}
/**
* @param LifecycleEventArgs $args
*/
public function updateUuid(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if (!$entity instanceof EntityA
&& !$entity instanceof EntityB
) {
return;
}
if (empty($entity->getUuid())) {
$uuid = $this->generator->generate($args->getEntityManager(), $entity);
$entity->setUuid($uuid);
}
}
}
This will automatically add a new UUID to the $uuid
field of the entities EntityA
and EntityB
if no UUID has been manually set.
from uuid-doctrine.
Is there a solution if you're mixing a fixed primary key property with another auto increment (sequence) property? I cannot get this sorted out.
from uuid-doctrine.
Thanks for the update. I wasn't ignoring this issue, though I should have spoken up to acknowledge that I saw it. I just wasn't sure exactly how to address it, but your solution looks good.
from uuid-doctrine.
Why don't use a simple constructor to initialise uuid ?
public function __construct()
{
$this->uuid = Uuid::uuid4();
}
from uuid-doctrine.
@ramsey It would be nice if this would be added to the documentation.
You can find official information about doctrine events here:
http://doctrine-orm.readthedocs.io/en/latest/reference/events.html
from uuid-doctrine.
@JHGitty, would you like to send a PR to update the README with examples? You might be able to explain the issue better than I can.
from uuid-doctrine.
Can this issue be reopened? It is still not possible (at least in certain cases) to add a second GeneratedValue when the database already exists and is already populated. This results in the "doctrine:schema:update" command trying to alter the existing GeneratedValue column.
Imagine you have a populated table with this schema:
/**
* @ORM\Entity
**/
class MyEntity
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
**/
private $id;
}
You then add a new GeneratedValue column to it.
/**
* @ORM\Entity
**/
class MyEntity
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
**/
private $id;
/**
* @var UuidInterface
*
* @ORM\Column(name="public_id", type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
**/
private $publicId;
}
Then you try to update your schema:
/var/www bin/console doctrine:schema:update --force
This fails with following message:
SQLSTATE[HY000]: General error: 1833 Cannot change column 'id': used in a foreign key constraint 'FK_2D25F90A9E6B1585' of table 'mydb.other_table'
If I let the command dump the SQL then I see that it tries to do an alter statement on the id column, even though from the SQL you can see that it didn't change at all.
/var/www bin/console doctrine:schema:update --dump-sql
ALTER TABLE my_entity ADD public_id CHAR(36) NOT NULL COMMENT '(DC2Type:uuid)', CHANGE id id INT NOT NULL;
Where does the ALTER statement come from? There is nothing to change: the name is already "id", it is already an INT and not nullable.
If I remove GeneratedValue from the public_id column then everything is fine.
/**
* @ORM\Entity
**/
class MyEntity
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
**/
private $id;
/**
* @var UuidInterface
*
* @ORM\Column(name="public_id", type="uuid")
**/
private $publicId;
}
/var/www bin/console doctrine:schema:update --dump-sql
ALTER TABLE my_entity ADD public_id CHAR(36) NOT NULL COMMENT '(DC2Type:uuid)';
from uuid-doctrine.
Might be caused by or related to doctrine/orm#7215
from uuid-doctrine.
Might be caused by or related to doctrine/orm#7215
Related, yes. And, sadly, this is still an issue.
from uuid-doctrine.
you may use https://github.com/KnpLabs/DoctrineBehaviors/blob/master/docs/uuidable.md instead
from uuid-doctrine.
@alebec Have you tried using the constructor for this in a production system, and what would the possible downsides be compared to using a subscriber?
from uuid-doctrine.
The proposed solution seems correct to me (although I had to explicitly do new UuidGenerator()
rather than injecting it because of argument "$uuidGenerator" of method "__construct()" references class "Ramsey\Uuid\Doctrine\UuidGenerator" but no such service exists
, probably due to some changes in the lib).
Nevertheless, as mentionned by @alebec, one could directly use Uuid::uuid4();
because this is basically all the generator does. This makes the generator quite useless in this particular case, so I think the doc should reflect that and redirect to ramsey/uuid instead with a prePersist
ready-to-use example.
@ramsey If this seems fine by you, I am ready to pull-request this documentation change
As for @pluk77 question whether using the constructor rather than the event listener/subscriber system, I have no answer for this now but I would feel more comfortable with a prePersist
listener.
from uuid-doctrine.
@Arnaud-J, please feel free to open a PR. Thanks!
from uuid-doctrine.
Related Issues (20)
- doctrine.yaml configuration issue HOT 1
- Type missmatch in generator with static PHP Mapping (LazyUuidFromString to property Uuid) HOT 3
- There is a bug in MariaDB 10.5.9, extremly slowing queris with UUID in binary form HOT 2
- uuid_binary_ordered_time not working with IN expression HOT 8
- Doctrine 3 compatibility HOT 1
- Imcompatible with the latest Doctrine 2.10 HOT 2
- doctrine.dbal.default_connection is set to 'default' and cannot be changed anymore HOT 2
- Deprecation notice after updating from Symfony 5.3 to 5.4 HOT 2
- Tag new release HOT 1
- Supportfor typed property. HOT 4
- Support other types of `EntityManagerInterface` too HOT 2
- Fetching metadata from an entity using the custom strategy is throwing an error. HOT 2
- Node of UUID is 0 HOT 1
- Additional usage of UUID v6/v7 for UuidBinaryOrderedTimeType HOT 7
- Converting my entities ids to uuids
- Tag a new release HOT 1
- Fetching association throws "Unknown parameter type, 116 given" exception HOT 6
- PostgreSQL UUID type returns a binary stream; DB to PHP conversion is always null HOT 5
- Object of class Ramsey\Uuid\Lazy\LazyUuidFromString could not be converted to int HOT 1
- Doctrine/dbal v4 is out!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uuid-doctrine.