removeElement()和clear()在数组集合属性的doctrine 2中不起作用 [英] removeElement() and clear() doesn't work in doctrine 2 with array collection property
问题描述
我正在尝试用doctrine 2完成一些简单的CRUD,但是当它将时间更新一个属性集作为数组集合的记录时,我似乎没有将removeElement()按原样工作。我甚至尝试以这种荒谬的方式做到这一点:
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();
}
它没有工作...任何人都知道如何处理这个?我已经以许多不同的形式提出了解决方案,迄今为止还没有得到很好的回应...似乎没有Doctrine 2 CRUD处理的很好的例子。我会根据要求发布更多的代码。
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.
修改
//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 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.
在参与者
实体
/**
* @ManyToOne(targetEntity="Event", inversedBy="participants", fetch="LAZY")
* @JoinColumn(name="event_id", referencedColumnName="id", nullable="TRUE")
* @var Event
*/
protected $event;
在事件
实体:
/**
* @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();
}
事件
实体是:
public function removeParticipant(Participant $participant)
{
$this->participants->removeElement($participant);
$participant->unsetEvent();
}
public function addParticipant(Participant $participant)
{
$participant->setEvent($this);
$this->participants[] = $participant;
}
参与者
实体是:
public function setEvent(Event $event)
{
$this->event = $event;
}
public function unsetEvent()
{
$this->event = null;
}
更新:isAttending方法
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;
}
这篇关于removeElement()和clear()在数组集合属性的doctrine 2中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!