学说 2 从关系数组集合中删除对象
class Lists extends \Entities\AbstractEntity {
/**
* @Id @Column(name="id", type="bigint",length=15)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ManyToMany(targetEntity="\Entities\Users\Usercomments")
* @JoinColumn(name="id", referencedColumnName="id")
*/
protected $comments;
public function getComments() {
return $this->comments;
}
public function addComments($comment) {
$this->comments->add($comment);
}
public function deleteComments(\Entities\Users\Comments $comments) {
$this->comments->removeElement($comments);
}
/** @PreUpdate */
public function updated() {
//$this->updated_at = new \DateTime("now");
}
public function __construct() {
$this->entry = new \Doctrine\Common\Collections\ArrayCollection();
}
}
我有一个由学说创建的多对多表..我可以通过以下方式设法向该表添加注释:
$getList = $this->_lis->findOneBy((array('userid' => $userid)));
$getComments = $this->_doctrine->getReference('\Entities\Users\Comments', $commentid);
$getList->addComments($getComments);
$this->_doctrine->flush();
但我无法删除... 我尝试过:removeElement 但没有快乐.. 有人告诉我,我可以取消数组集合中的某些内容,但我不明白......
class Lists extends \Entities\AbstractEntity {
/**
* @Id @Column(name="id", type="bigint",length=15)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ManyToMany(targetEntity="\Entities\Users\Usercomments")
* @JoinColumn(name="id", referencedColumnName="id")
*/
protected $comments;
public function getComments() {
return $this->comments;
}
public function addComments($comment) {
$this->comments->add($comment);
}
public function deleteComments(\Entities\Users\Comments $comments) {
$this->comments->removeElement($comments);
}
/** @PreUpdate */
public function updated() {
//$this->updated_at = new \DateTime("now");
}
public function __construct() {
$this->entry = new \Doctrine\Common\Collections\ArrayCollection();
}
}
i have a many to many table create by doctrine.. i can manage to add comments to this table by:
$getList = $this->_lis->findOneBy((array('userid' => $userid)));
$getComments = $this->_doctrine->getReference('\Entities\Users\Comments', $commentid);
$getList->addComments($getComments);
$this->_doctrine->flush();
but i cant delete...
i tried: removeElement but no joy..
someone told me that i can unset soemthing in my array collection, i dont get it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 ArrayCollection 元素上使用简单的 PHP“unset()”。执行此操作的最佳位置是在实体类中定义一个新方法。
下面是一个从 ArrayCollection 属性中删除所有元素的示例:
您可能还可以定义一个 Entity 方法来删除单个元素:
You can use a simple PHP "unset()" on the ArrayCollection element. Best place to do this would be to define a new method in your Entity class.
Here is an example which removes all elements from an ArrayCollection property:
You could probably also define an Entity method to remove a single element: