Doctrine orm 从命令行生成具有字符串id的条令实体
我喜欢使用命令行为条令生成实体存根 对于特定实体,id应该是唯一的字符串,而不是自动递增的默认整数 1:如何在如下命令中指定此选项Doctrine orm 从命令行生成具有字符串id的条令实体,doctrine-orm,doctrine,symfony,Doctrine Orm,Doctrine,Symfony,我喜欢使用命令行为条令生成实体存根 对于特定实体,id应该是唯一的字符串,而不是自动递增的默认整数 1:如何在如下命令中指定此选项 php bin/console generate:doctrine:entity --entity=AppBundle:Game 2:在entity类中,是否可以指定一些将返回此字符串的函数(类似于“strategy”属性)? 一件很有趣的事 @ORM\GeneratedValue(customFunc="getUniqueString") 创建自己的ID生成器
php bin/console generate:doctrine:entity --entity=AppBundle:Game
2:在entity类中,是否可以指定一些将返回此字符串的函数(类似于“strategy”属性)?
一件很有趣的事
@ORM\GeneratedValue(customFunc="getUniqueString")
创建自己的ID生成器 步骤:
AbstractIdGenerator
generate
方法CustomIdGenerator
注释class UniqueStringGenerator extends AbstractIdGenerator
{
public function generate(EntityManager $em, $entity)
{
// Your logic here
}
}
实体:
/**
* @ORM\Id
* @ORM\Column(name="id", type="string")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="MYMODULE\Doctrine\UniqueStringGenerator")
*/
private $id;
对于第二点,我已经仔细阅读了这个问题,但是找不到我想要的答案。似乎不可能完全按照我的要求执行操作,我应该研究将strategy设置为none,并确保在持久化项之前设置id。(参考:)这很有效,谢谢!为了完整性,UniqueStringGenerator类必须添加以下use语句:use-doctor\ORM\Id\AbstractIdGenerator;使用条令\ORM\EntityManager;