教义中的两个生成值
我想在我的 Doctrine-Entity 中实现一个人类可读的 ID.我想保留 DB-ID 以用于工作并添加诸如PRE12-00005"之类的内容(带有前缀和年份,并且每年从 0 开始).我尝试添加一个自定义 ID 生成器,但似乎 Doctrine 无法在一个实体中使用两个生成的值.
i would like to implement a human-readable ID in my Doctrine-Entity. I want to keep the DB-ID for working and adding something like "PRE12-00005" (with Prefix and Year, and start at 0 every new year). I tried to add a Custom-ID-Generator, but it seems that Doctrine can't work with two generated Values in one Entity.
/**
* @var integer
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string
* @ORMColumn(name="name", type="string", length=25, unique=true)
* @ORMGeneratedValue(strategy="CUSTOM")
* @ORMCustomIdGenerator(class="NameGenerator")
*/
protected $name;
Doctrine 总是尝试将我的生成器的返回值保存到id"-Field 中,并将 null 保存到name"中.还有其他实现方式吗?
Doctrine always try to save the return value from my generator into the "id"-Field and null into "name". Is there another way to implement it?
推荐答案
据我所知,GeneratedValue 策略是为主键保留的,这意味着每个实体只能使用一次.
From what I know the GeneratedValue strategy is reserved for the primary key, meaning you can only use it once per Entity.
根据您的需要,您有几个选择:
Depending on your needs you have a few options though:
你总是可以有一个 prePersist 生命周期事件,在第一次持久化之前为名称设置您喜欢的任何值.
You can always have a prePersist lifecycle event, setting any value you like for the name before you persist it the first time.
如果你依赖 id 从中生成另一个唯一的 id,你可以实现一个 postPersist 事件,在那里设置你的名字并确保你刷新两次(第一次生成主键,第二次保存名称).
If you depend on the id to generate another unique id from it, you could implement a postPersist event, set your name there and make sure you flush twice (The first time to generate the primary key, the second time to save the name).
如果名称在数据库中一段时间为空对您来说没有问题,则可以实现 postLoad 事件,如果名称为空,则填充该名称.这样,您的应用程序始终可以看到名称(因为它要么是从数据库加载的,要么是由 postLoad 事件填充的),并且当您在初始保存后第一次添加或编辑信息时,您的名称也将被保存
If it's ok for you that the name is empty in the database for some time, it could be ok to implement a postLoad event, which fills the name if it's empty. This way your application always sees the name (because it's either loaded from the database or filled by the postLoad event) and when you add or edit information the first time after the initial save, your name will also be saved
可以不保存名称并让它由某些 cronjob/deamon/queue 生成,这样您的应用程序就不必处理它.您唯一需要做的就是确保缺少的名称不会搞砸.
It could be ok to not save the name and have it generated by some cronjob/deamon/queue so your application does not have to deal with it. The only thing you would need to do is make sure a missing name does not screw up something.
也许可以生成一个不依赖于主键的键,因此可以由 全局事件处理程序.您当然有这样的缺点,因为他是全局的,因此会为您保留的每个对象调用此类事件处理程序,无论它是否是正确的实体.
Maybe it might be ok to generate a key which does not depend on the primary key and thus can be generated by an global event handler. You of course have the drawback that such an event handler, because he is global, gets called for every object you persist, no matter if it's the correct entity.
最后但并非最不重要的一点是,可以回退到存储过程/触发器让数据库处理这个问题.这样你就不必在你的应用程序中搞砸了.但请注意,在此过程中可能存在陷阱(例如开发人员忘记了这一点,因为它不在代码中,而是在数据库中!).
Last, but not least, it might be ok to fallback to Stored Procedures/Triggers to let the database handle this. This way you don't have to mess with this inside your application. But beware, there might be pitfalls on the way (like a developer forgetting about this because it's not in the code but in the database!).
可能还有其他方法.我想说的是:不要将生成的值用于非主键属性!
There may be other ways. What I was trying to say is: Don't use generatedValue for non-primary key properties!
相关文章