在具有数组集合属性的原则 2 中,removeElement() 和clear() 不起作用

发布于 2024-11-16 16:55:31 字数 874 浏览 1 评论 0原文

我正在尝试使用原则 2 完成一些简单的 CRUD,但是当需要更新将一个属性设置为数组集合的记录时,我似乎没有让 removeElement() 按预期工作。我什至尝试以这种极其丑陋的方式做到这一点:

foreach($entity->getCountries() as $c) {
        $entity->getCountries()->removeElement($c);
        $this->em->persist($entity);
        $this->em->flush();
}

但它不起作用......有人知道如何处理这个吗?我已经以多种不同的形式要求解决这个问题,但到目前为止还没有得到很好的回应……似乎缺乏 Doctrine 2 CRUD 处理的好例子。我将根据要求发布更多代码。

编辑

//in user entity
/**
 * 
 * @param \Doctring\Common\Collections\Collection $property
 * @OneToMany(targetEntity="Countries",mappedBy="user", cascade={"persist", "remove"})
 */
private $countries;

//in countries entity
/**
 *
 * @var User
 * @ManyToOne(targetEntity="User", inversedBy="id") 
 * @JoinColumns({
 *  @JoinColumn(name="user_id", referencedColumnName="id")
 * })
 */
private $user;

I'm trying to get some simple CRUD done with doctrine 2 but when it's time to update a record with one property set as an array collection I don't seem to get removeElement() to work as it's supposed to. I even tried doing it in this ridiculously ugly way:

foreach($entity->getCountries() as $c) {
        $entity->getCountries()->removeElement($c);
        $this->em->persist($entity);
        $this->em->flush();
}

and it didn't work... Anyone knows how to handle this? I've asked for a solution to this in many different forms and haven't got a good response so far... seems there's lack of good examples of Doctrine 2 CRUD handling. I'll post more code at request.

Edit

//in user entity
/**
 * 
 * @param \Doctring\Common\Collections\Collection $property
 * @OneToMany(targetEntity="Countries",mappedBy="user", cascade={"persist", "remove"})
 */
private $countries;

//in countries entity
/**
 *
 * @var User
 * @ManyToOne(targetEntity="User", inversedBy="id") 
 * @JoinColumns({
 *  @JoinColumn(name="user_id", referencedColumnName="id")
 * })
 */
private $user;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2024-11-23 16:55:31

我在一个活动项目中做了类似的事情,其参与者与您的用户/国家关系不同。我将列出该过程,您可以看看是否有什么不同的做法。

Participant 实体上

/**
 * @ManyToOne(targetEntity="Event", inversedBy="participants", fetch="LAZY")
 * @JoinColumn(name="event_id", referencedColumnName="id", nullable="TRUE")
 * @var Event
 */
protected $event;

Event 实体上:

/**
 * @OneToMany(targetEntity="Participant", mappedBy="event")
 * @var \Doctrine\Common\Collections\ArrayCollection
 */
protected $participants;

同样在 Event#__constructor 中,我像这样初始化:

$this->participants = new \Doctrine\Common\Collections\ArrayCollection();

以下是我更新事件的方式:

public function update(Event $event, Event $changes)
{
    // Remove participants
    $removed = array();
    foreach($event->participants as $participant)
    {
        if(!$changes->isAttending($participant->person))
        {
            $removed[] = $participant;
        }
    }

    foreach($removed as $participant)
    {
        $event->removeParticipant($participant);
        $this->em->remove($participant);
    }

    // Add new participants
    foreach($changes->participants as $participant)
    {
        if(!$event->isAttending($participant->person))
        {
            $event->addParticipant($participant);
            $this->em->perist($participant);
        }
    }

    $event->copyFrom($changes);
    $event->setUpdated();
    $this->em->flush();
}

方法Event 实体上的方法有:

public function removeParticipant(Participant $participant)
{
    $this->participants->removeElement($participant);
    $participant->unsetEvent();
}

public function addParticipant(Participant $participant)
{
    $participant->setEvent($this);
    $this->participants[] = $participant;
}

Participant 实体上的方法有:

public function setEvent(Event $event)
{
    $this->event = $event;
}

public function unsetEvent()
{
    $this->event = null;
}

UPDATE:isAttending 方法

/**
 * Checks if the given person is a 
 * participant of the event
 * 
 * @param Person $person
 * @return boolean 
 */
public function isAttending(Person $person)
{
    foreach($this->participants as $participant)
    {
        if($participant->person->id == $person->id)
            return true;
    }

    return false;
}

I do something similar in a project with Events which have participants not unlike your User/Country relationship. I will just lay out the process and you can see if there's anything you are doing differently.

On the Participant entity

/**
 * @ManyToOne(targetEntity="Event", inversedBy="participants", fetch="LAZY")
 * @JoinColumn(name="event_id", referencedColumnName="id", nullable="TRUE")
 * @var Event
 */
protected $event;

On the Event entity:

/**
 * @OneToMany(targetEntity="Participant", mappedBy="event")
 * @var \Doctrine\Common\Collections\ArrayCollection
 */
protected $participants;

Also in Event#__constructor I initialize like this:

$this->participants = new \Doctrine\Common\Collections\ArrayCollection();

Here is how I update an event:

public function update(Event $event, Event $changes)
{
    // Remove participants
    $removed = array();
    foreach($event->participants as $participant)
    {
        if(!$changes->isAttending($participant->person))
        {
            $removed[] = $participant;
        }
    }

    foreach($removed as $participant)
    {
        $event->removeParticipant($participant);
        $this->em->remove($participant);
    }

    // Add new participants
    foreach($changes->participants as $participant)
    {
        if(!$event->isAttending($participant->person))
        {
            $event->addParticipant($participant);
            $this->em->perist($participant);
        }
    }

    $event->copyFrom($changes);
    $event->setUpdated();
    $this->em->flush();
}

The methods on the Event entity are:

public function removeParticipant(Participant $participant)
{
    $this->participants->removeElement($participant);
    $participant->unsetEvent();
}

public function addParticipant(Participant $participant)
{
    $participant->setEvent($this);
    $this->participants[] = $participant;
}

The methods on the Participant entity are:

public function setEvent(Event $event)
{
    $this->event = $event;
}

public function unsetEvent()
{
    $this->event = null;
}

UPDATE: isAttending method

/**
 * Checks if the given person is a 
 * participant of the event
 * 
 * @param Person $person
 * @return boolean 
 */
public function isAttending(Person $person)
{
    foreach($this->participants as $participant)
    {
        if($participant->person->id == $person->id)
            return true;
    }

    return false;
}
执妄 2024-11-23 16:55:31

新答案

在您的国家/地区实体中,您不应该使用:

@ManyToOne(targetEntity="User", inversedBy="countries") 

而不是 inversedBy="id" 吗?

初始答案

您需要将实体中的国家/地区字段设置为删除级联。例如,在双向一对多关系中:

class Entity
{
    /**
     * 
     * @OneToMany(targetEntity="Country", mappedBy="entity", cascade={"remove"})
     */
    private $countries;
}

这样,在保存实体时,学说还将保存附加到实体(例如国家/地区)的集合中的更改。否则,您必须在刷新之前明确删除要删除的国家/地区,例如,

$this->em()->remove($aCountry);

这对于持久、合并和分离操作也有效。更多信息 这里

New answer

In your countries entity, should you not have:

@ManyToOne(targetEntity="User", inversedBy="countries") 

instead of inversedBy="id"?

Initial answer

You need to set the countries field in your entity as remove cascade. For example, on a bidirectional one to many relationship:

class Entity
{
    /**
     * 
     * @OneToMany(targetEntity="Country", mappedBy="entity", cascade={"remove"})
     */
    private $countries;
}

This way, when saving your entity, doctrine will also save changes in collections attached to your entity (such as countries). Otherwise you have to explicitly remove the countries you want to remove before flushing, e.g.

$this->em()->remove($aCountry);

This is also valid for persist, merge and detach operations. More information here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文