doumen1883 2012-08-20 08:11
浏览 20
已采纳

是否有可能在持续存在之后和在Doctrine2中刷新之前获取实体的ID?

I have the User Login Registration Page.

Now on the same Page i have one more Forms i.e UserInterests.

Now i have the PostPersist function which creates new UserProfile after User is Persisted

Now UserProfile is linked with User ID and UserInterests is linked with UserProfile ID

Now the client wants UserInterests on same User page but i have problem that UserProfile is not yet created. Now how can persist them. Is there any way

  • 写回答

1条回答 默认 最新

  • duanlinzhen7235 2012-08-20 08:25
    关注

    I don't think you can get the ID before flushing.

    You could create an association between the models, that way Doctrine will take care of the id's when saving and you can retrieve your UserInterests with something like:

    $user->getProfile()->getInterests();
    

    So you would have your User model with a property that holds your UserProfile:

    /**
     * @OneToOne(targetEntity="UserProfile")
     * @JoinColumn(name="profile_id", referencedColumnName="id")
     **/
    private $profile;
    

    and your UserProfile class should have a property to hold the UserInterests model.

    /**
     * @OneToOne(targetEntity="UserInterests")
     * @JoinColumn(name="interests_id", referencedColumnName="id")
     **/
    private $interests;
    

    You can now create an empty $userProfile model (to link the others together, the actual filling can be done in your postPersist function) and a $userInterests model, associate them by

    $interests = new UserInterests();
    
    // create an empty UserProfile, and fill it in your PostPersist function, 
    // that way it can already be used to link the User and UserInterests
    $profile = new UserProfile();
    
    $profile->setInterests($interests);
    $user->setProfile($profile);
    

    Now Doctrine will fill in the ids when persisting and you don't need to worry about them.

    More information here

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

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug