removeElement()和clear()在带有数组集合属性的doctrine 2中不起作用

时间:2011-06-23 23:59:12

标签: doctrine doctrine-orm

我正在尝试使用doctrine 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;

2 个答案:

答案 0 :(得分:12)

我在一个项目中做了类似的事情,其活动的参与者与你的用户/国家关系不同。我将简单介绍这个过程,你可以看看是否有任何不同的做法。

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;
}

更新: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;
}

答案 1 :(得分:1)

新答案

在您所在的国家/地区实体中,您是否应该:

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

而不是inversedBy =“id”?

初步回答

您需要将实体中的countries字段设置为remove cascade。例如,在双向一对多关系中:

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

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

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

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