la_*_*0ka 8 doctrine doctrine-orm
我正在尝试使用doctrine 2完成一些简单的CRUD,但是当需要更新一个属性设置为数组集合的记录时,我似乎没有让removeElement()按照预期的那样工作.我甚至尝试过这种荒谬可笑的方式:
foreach($entity->getCountries() as $c) {
$entity->getCountries()->removeElement($c);
$this->em->persist($entity);
$this->em->flush();
}
Run Code Online (Sandbox Code Playgroud)
它不起作用......任何人都知道如何处理这个?我已经要求以多种不同的形式解决这个问题,到目前为止还没有得到很好的回应......似乎缺乏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;
Run Code Online (Sandbox Code Playgroud)
roj*_*oca 12
我在一个项目中做了类似的事情,其活动的参与者与你的用户/国家关系不同.我将简单介绍这个过程,你可以看看是否有任何不同的做法.
在Participant实体上
/**
* @ManyToOne(targetEntity="Event", inversedBy="participants", fetch="LAZY")
* @JoinColumn(name="event_id", referencedColumnName="id", nullable="TRUE")
* @var Event
*/
protected $event;
Run Code Online (Sandbox Code Playgroud)
在Event实体上:
/**
* @OneToMany(targetEntity="Participant", mappedBy="event")
* @var \Doctrine\Common\Collections\ArrayCollection
*/
protected $participants;
Run Code Online (Sandbox Code Playgroud)
同样在Event#__constructor我初始化如下:
$this->participants = new \Doctrine\Common\Collections\ArrayCollection();
Run Code Online (Sandbox Code Playgroud)
以下是我更新活动的方式:
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();
}
Run Code Online (Sandbox Code Playgroud)
Event实体上的方法是:
public function removeParticipant(Participant $participant)
{
$this->participants->removeElement($participant);
$participant->unsetEvent();
}
public function addParticipant(Participant $participant)
{
$participant->setEvent($this);
$this->participants[] = $participant;
}
Run Code Online (Sandbox Code Playgroud)
Participant实体上的方法是:
public function setEvent(Event $event)
{
$this->event = $event;
}
public function unsetEvent()
{
$this->event = null;
}
Run Code Online (Sandbox Code Playgroud)
更新: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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20584 次 |
| 最近记录: |