0

I have a document that has a ReferenceMany attribute to another document. The reference is setup fine, and the data is returned from the query fine, but each document in the arraycollection is returned as a proxy. On this site, I saw it was mentioned I should add ->prime(true) in order to return the actual referenced documents.

When that ArrayCollection of documents is returned, I am running a loop of ids I have submitted to the server to remove them from the referenced collection. The removeElement method is not working b/c the returned documents are proxies, and I am comparing an actual document vs. those proxies. So basically I am trying to:

  1. Look up a single document
  2. Force all documents in the ReferenceMany attribute to be actual documents and not Proxy documents
  3. Loop through my array of id's and load each document
  4. Send the document to the removeElement method

On the first getSingleResult query method below, I am getting an error cannot modify cursor after beginning iteration. I saw a thread on this site mention you should prime the results in order to get actual documents back instead of proxies, and in his example, he used getSingleResult.

$q = $this->dm->createQueryBuilder('\FH\Document\Person')->field('target')->prime(true)->field('id')->equals($data->p_id);
$person = $q->getQuery()->getSingleResult();

foreach($data->loc_id as $loc) {
    $location = $this->dm->createQueryBuilder('\FH\Document\Location')->field('id')->equals(new \MongoId($loc))->getQuery()->getSingleResult();
    $person->removeTarget($location);
}

....
....
....
public function removeTarget($document)
{
    $this->target->removeElement($document);

    return $this;
}

If I remove ->prime(true) from the first query, it doesn't throw an error, yet it doesn't actually remove any elements even though I breakpoint on the method, compare the two documents, and the data is exactly the same, except in $this->target they are Location Proxy documents, and the loaded one is an actual Location Document.

Can I prime the single result somehow so I can use the ArrayCollection methods properly, or do I need to just do some for loop and compare ids?

UPDATE

So here is an update showing the problem I am having. While the solution below would work just using the MongoId(s), when I submit an actual Document class, it never actually removes the document. The ArrayCollection comes back from Doctrine as a PersistentCollection. Each element in $this->coll is of this Document type:

DocumentProxy\__CG__\FH\Document\Location

And the actual Document is this:

FH\Document\Location

The removeElement method does an array_search like this:

public function removeElement($element)
{
    $key = array_search($element, $this->_elements, true);

    if ($key !== false) {
        unset($this->_elements[$key]);

        return true;
    }

    return false;
}

So because the object types are not exactly the same, even though the proxy object should be inheriting from the actual Document I created, $key always returns 0 (false), so the element is not removed. Everything between the two documents are exactly the same, except the object type.

Like I said, I guess I can do it by MongoId, but why isn't it working by submitting the entire object?

4

1 に答える 1

1

今のところ、素数(真)のものについて心配する必要はありません。参照されるデータを今すぐプルするようにdoctrineに指示するだけなので、カーソルを反復処理するときにデータベースを複数回呼び出す必要はありません。

私がすることは、あなたのremoveTargetメソッドを次のように変更することです。

$this->dm->createQueryBuilder('\FH\Document\Person')->field('id')->equals($data->p_id);
$person = $q->getQuery()->getSingleResult();
$person->removeTargets($data->loc_id);

Person.php

public function removeTargets($targets)
{
    foreach ($targets as $target) {
        $this->removeTarget($target);
    }
}

public function removeTarget($target)
{
    if ($target instanceof \FH\Document\Location) {
        return $this->targets->removeElement($target);
    }

    foreach ($this->targets as $t) {
        if ($t->getId() == $target) {
            return $this->targets->removeElement($t);
        }
    }
    return $this;
}

これは、2番目のクエリを手動で実行する必要がないことを意味します。これは、Doctrineが、参照を反復処理するときに、その参照のデータをプルする必要があることを認識しているためです。次に、prime(true)呼び出しを使用して、オブジェクトを要求するときに動的に実行するのではなく、1回の呼び出しで必要な情報を取得することで、この操作を高速化できます。

于 2012-11-23T11:41:56.510 に答える