douyue8685 2016-03-11 06:55
浏览 39
已采纳

在Doctrine中使用唯一ID设置为NULL创建的实体对象

I have a User entity class, on which I used php bin/console doctrine:generate:entities the usual way. This class has a unique ID among its variables :

class User 
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;
    (...)

Much to my surprise, when I call $user=new User(); in my code, the newly created object has its $id set to NULL. Given the annotations I gave, isn't Doctrine supposed to set it automatically ?

Sure, I am aware that I can add a setId method to my class, but I feel this is not the correct way to do things. Any advice appreciated.

  • 写回答

1条回答 默认 最新

  • douxian7117 2016-03-11 07:17
    关注

    Unique id will be set on $em->flush($user). i.e after inserting in table.

    You can get more information in doctrine's documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏