removeElement() 和 clear() 在具有数组集合属性的学说 2 中不起作用 [英] removeElement() and clear() doesn't work in doctrine 2 with array collection property

查看:12
本文介绍了removeElement() 和 clear() 在具有数组集合属性的学说 2 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用学说 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 DoctringCommonCollectionsCollection $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;

关于Event实体:

/**
 * @OneToMany(targetEntity="Participant", mappedBy="event")
 * @var DoctrineCommonCollectionsArrayCollection
 */
protected $participants;

同样在 Event#__constructor 我这样初始化:

Also in Event#__constructor I initialize like this:

$this->participants = new DoctrineCommonCollectionsArrayCollection();

这是我更新事件的方式:

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

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方法

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() 在具有数组集合属性的学说 2 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆