在具有数组集合属性的原则 2 中,removeElement() 和clear() 不起作用
我正在尝试使用原则 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在一个活动项目中做了类似的事情,其参与者与您的用户/国家关系不同。我将列出该过程,您可以看看是否有什么不同的做法。
在
Participant
实体上在
Event
实体上:同样在
Event#__constructor
中,我像这样初始化:以下是我更新事件的方式:
方法
Event
实体上的方法有:Participant
实体上的方法有:UPDATE:isAttending 方法
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
entityOn the
Event
entity:Also in
Event#__constructor
I initialize like this:Here is how I update an event:
The methods on the
Event
entity are:The methods on the
Participant
entity are:UPDATE: isAttending method
新答案
在您的国家/地区实体中,您不应该使用:
而不是 inversedBy="id" 吗?
初始答案
您需要将实体中的国家/地区字段设置为删除级联。例如,在双向一对多关系中:
这样,在保存实体时,学说还将保存附加到实体(例如国家/地区)的集合中的更改。否则,您必须在刷新之前明确删除要删除的国家/地区,例如,
这对于持久、合并和分离操作也有效。更多信息 这里。
New answer
In your countries entity, should you not have:
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:
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 is also valid for persist, merge and detach operations. More information here.