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 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重