Skip to content

Commit

Permalink
Merge pull request doctrine#565 from he-li/doctrine#563
Browse files Browse the repository at this point in the history
Check keys instead of values
  • Loading branch information
lsmith77 committed Sep 17, 2014
2 parents ee55db7 + c6c2067 commit 16eaca3
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/PHPCR/UnitOfWork.php
Expand Up @@ -2621,7 +2621,7 @@ private function executeUpdates($documents, $dispatchEvents = true)
}

if ($referencingNode->hasProperty($referencingField['property'])) {
if (! in_array($uuid, $referencingNode->getPropertyValue($referencingField['property']), PropertyType::STRING)) {
if (!array_key_exists($uuid, $referencingNode->getPropertyValue($referencingField['property']))) {
if (!$collection instanceof PersistentCollection || !$collection->isDirty()) {
// update the reference collection: add us to it
$collection->add($document);
Expand Down
35 changes: 35 additions & 0 deletions tests/Doctrine/Tests/Models/CMS/CmsArticle.php
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Tests\Models\CMS;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;

/**
Expand All @@ -19,11 +20,45 @@ class CmsArticle
public $user;
public $comments;

/** @PHPCRODM\ReferenceMany(targetDocument="CmsArticlePerson") */
public $persons;

/** @PHPCRODM\Binary(nullable=true) */
public $attachments;

function __construct()
{
$this->persons = new ArrayCollection();
}

public function setAuthor(CmsUser $author)
{
$this->user = $author;
}

/**
* @param CmsArticlePerson $person
*/
public function addPerson(CmsArticlePerson $person){
$this->persons->add($person);
}

/**
* @param mixed $persons
*/
public function setPersons($persons)
{
$this->persons = $persons;
}

/**
* @return mixed
*/
public function getPersons()
{
return $this->persons;
}



}
89 changes: 89 additions & 0 deletions tests/Doctrine/Tests/Models/CMS/CmsArticlePerson.php
@@ -0,0 +1,89 @@
<?php

namespace Doctrine\Tests\Models\CMS;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
use Doctrine\ODM\PHPCR\DocumentRepository;
use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface;

/**
* @PHPCRODM\Document(repositoryClass="Doctrine\Tests\Models\CMS\CmsArticlePersonRepository", referenceable=true)
*/
class CmsArticlePerson
{
/** @PHPCRODM\Id(strategy="repository") */
public $id;
/** @PHPCRODM\String(nullable=true) */
public $name;
/** @PHPCRODM\Referrers(referencedBy="persons", referringDocument="Doctrine\Tests\Models\CMS\CmsArticle", cascade="persist") */
public $articlesReferrers;

public function __construct()
{
$this->articlesReferrers = new ArrayCollection();
}

/**
* @param mixed $articlesReferrers
*/
public function setArticlesReferrers($articlesReferrers)
{
$this->articlesReferrers = $articlesReferrers;
}

/**
* @return mixed
*/
public function getArticlesReferrers()
{
return $this->articlesReferrers;
}

/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}

/**
* @return mixed
*/
public function getId()
{
return $this->id;
}

/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

}

class CmsArticlePersonRepository extends DocumentRepository implements RepositoryIdInterface
{
/**
* Generate a document id
*
* @param object $document
* @return string
*/
public function generateId($document, $parent = null)
{
return '/functional/'.$document->name;
}
}
44 changes: 44 additions & 0 deletions tests/Doctrine/Tests/ODM/PHPCR/Functional/CascadePersistTest.php
Expand Up @@ -289,4 +289,48 @@ public function testCascadeReferenceManyNoObject()
$this->dm->persist($user);
$this->dm->flush();
}

/**
* Test Referrers MantyToMany cascade Flush
*/
public function testCascadeManagedDocumentReferrerMtoMDuringFlush()
{
$article1 = new \Doctrine\Tests\Models\CMS\CmsArticle();
$article1->text = "foo";
$article1->topic = "bar";
$article1->id = '/functional/article_m2m_referrer_1';
$this->dm->persist($article1);

$article2 = new \Doctrine\Tests\Models\CMS\CmsArticle();
$article2->text = "foo2";
$article2->topic = "bar2";
$article2->id = '/functional/article_m2m_referrer_2';
$this->dm->persist($article2);

$superman = new \Doctrine\Tests\Models\CMS\CmsArticlePerson();
$superman->name = "superman";

$this->dm->persist($superman);

$article1->addPerson($superman);

$this->dm->flush();

$this->dm->refresh($superman);

$this->assertEquals($superman, $article1->getPersons()->first());

// we want to attach article2 to superman
// in the form of edition, we will submit article1 and article2 at the same time
$superman->getArticlesReferrers()->add($article1);
$superman->getArticlesReferrers()->add($article2);
$this->dm->flush();
$this->dm->refresh($superman);


$this->assertEquals(1, $article1->getPersons()->count());
$this->assertEquals(2, $superman->getArticlesReferrers()->count());

$this->dm->clear();
}
}

0 comments on commit 16eaca3

Please sign in to comment.