【问题标题】:Doctrine EM - How to insert into a columnDoctrine EM - 如何插入列
【发布时间】:2014-01-05 21:54:52
【问题描述】:

我有两个实体:俱乐部和用户。在用户实体中,我创建了一个多对多表:

 /**
 * @ORM\ManyToMany(targetEntity="Club", inversedBy="users")
 * @ORM\JoinTable(name="sms_followers")
 **/
 private $smsfollower;

现在我想将数据添加到表中。我用这个:

//Get the club (no error handling created
$getclub = $this->em->getRepository('Club')->findOneBy(array('id' => $clubid));

//Get the user by login ID
$getuser = $this->em->getRepository('User')->findOneBy(array('id' => '7'));

//Insert the userID and ClubID to the table sms_followers
$getuser = $getclub->addSmsfollower();

我无法完成这项工作...谁能帮帮我?

请让英语尽可能简单,我来自荷兰

【问题讨论】:

    标签: php symfony join doctrine-orm


    【解决方案1】:

    您最可能想要的是将 SmsFollower 添加到您已有的集合中。

    // in your User enitity
    public function addClub(Club $club)
    {
        $this->clubs->add($club);
    }
    
    // in your Club entity
    public function addSmsFollower(User $user)
    {
        $this->sms_followers->add($user);
    }
    

    然后在您的实体管理器中,您可以持久化对象,然后刷新以保存它们。

    提示:我将字段命名为 sms_followers 和 clubs(复数,meervoud),因为它是多对多的关系。

    【讨论】:

    • 请给我一个例子吗?
    • 在您的情况下,它将是: $getclub->addSmsfollower($getuser); $this->em->persist($getclub); $this->em->flush();您应该阅读有关此的 Doctrine2 文档:docs.doctrine-project.org/en/2.0.x/reference/…
    • 谢谢!这对我帮助很大。
    • 如何从该表中检索数据?我已经正确完成了 2 次,但第三次无法完成:S
    猜你喜欢
    • 2023-03-11
    • 2022-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多