1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +03:00

Made single identifier One-To-One + Id work also and added a test-case

This commit is contained in:
Benjamin Eberlei 2010-08-07 20:07:10 +02:00
parent c697a2d47f
commit 10f47389ae
2 changed files with 98 additions and 3 deletions

View file

@ -1839,7 +1839,11 @@ class UnitOfWork implements PropertyChangedListener
} }
$idHash = implode(' ', $id); $idHash = implode(' ', $id);
} else { } else {
$idHash = $data[$class->identifier[0]]; if (isset($class->associationMappings[$class->identifier[0]])) {
$idHash = $data[$class->associationMappings[$class->identifier[0]]->joinColumns[0]['name']];
} else {
$idHash = $data[$class->identifier[0]];
}
$id = array($class->identifier[0] => $idHash); $id = array($class->identifier[0] => $idHash);
} }

View file

@ -15,6 +15,7 @@ class DDC117Test extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC117Article'), $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC117Article'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC117Reference'), $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC117Reference'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC117Translation'), $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC117Translation'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC117ArticleDetails'),
)); ));
} catch(\Exception $e) { } catch(\Exception $e) {
@ -56,6 +57,45 @@ class DDC117Test extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertSame($refRep, $this->_em->find(__NAMESPACE__."\DDC117Reference", array('source' => 1, 'target' => 2))); $this->assertSame($refRep, $this->_em->find(__NAMESPACE__."\DDC117Reference", array('source' => 1, 'target' => 2)));
} }
/**
* @group DDC-117
*/
public function testInverseSideAccess()
{
$article1 = new DDC117Article("Foo");
$article2 = new DDC117Article("Bar");
$this->_em->persist($article1);
$this->_em->persist($article2);
$this->_em->flush();
$reference = new DDC117Reference($article1, $article2, "Test-Description");
$this->_em->persist($reference);
$this->_em->flush();
$this->_em->clear();
$article1 = $this->_em->find(__NAMESPACE__."\DDC117Article", $article1->id());
$this->assertEquals(1, count($article1->references()));
foreach ($article1->references() AS $reference) {
$this->assertType(__NAMESPACE__."\DDC117Reference", $reference);
$this->assertSame($article1, $reference->source());
}
$this->_em->clear();
$dql = 'SELECT a, r FROM '. __NAMESPACE__ . '\DDC117Article a INNER JOIN a.references r WHERE a.id = ?1';
$articleDql = $this->_em->createQuery($dql)
->setParameter(1, $article1->id())
->getSingleResult();
$this->assertEquals(1, count($article1->references()));
foreach ($article1->references() AS $reference) {
$this->assertType(__NAMESPACE__."\DDC117Reference", $reference);
$this->assertSame($article1, $reference->source());
}
}
/** /**
* @group DDC-117 * @group DDC-117
*/ */
@ -86,6 +126,20 @@ class DDC117Test extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertType(__NAMESPACE__ . '\DDC117Translation', $translation); $this->assertType(__NAMESPACE__ . '\DDC117Translation', $translation);
} }
/**
* @group DDC-117
*/
public function testOneToOneForeignObjectId()
{
$article1 = new DDC117Article("Foo");
$this->_em->persist($article1);
$this->_em->flush();
$details = new DDC117ArticleDetails($article1, "Very long text");
$this->_em->persist($details);
$this->_em->flush();
}
} }
/** /**
@ -103,12 +157,22 @@ class DDC117Article
*/ */
private $references; private $references;
/**
* @OneToOne(targetEntity="DDC117ArticleDetails", mappedBy="article")
*/
private $details;
public function __construct($title) public function __construct($title)
{ {
$this->title = $title; $this->title = $title;
$this->references = new \Doctrine\Common\Collections\ArrayCollection(); $this->references = new \Doctrine\Common\Collections\ArrayCollection();
} }
public function setDetails($details)
{
$this->details = $details;
}
public function id() public function id()
{ {
return $this->id; return $this->id;
@ -118,6 +182,33 @@ class DDC117Article
{ {
$this->references[] = $reference; $this->references[] = $reference;
} }
public function references()
{
return $this->references;
}
}
/**
* @Entity
*/
class DDC117ArticleDetails
{
/** @Id @OneToOne(targetEntity="DDC117Article", inversedBy="details") */
private $article;
/**
* @Column(type="text")
*/
private $text;
public function __construct($article, $text)
{
$this->article = $article;
$article->setDetails($this);
$this->text = $text;
}
} }
/** /**
@ -126,12 +217,12 @@ class DDC117Article
class DDC117Reference class DDC117Reference
{ {
/** /**
* @Id @ManyToOne(targetEntity="DDC117Article") * @Id @ManyToOne(targetEntity="DDC117Article", inversedBy="references")
*/ */
private $source; private $source;
/** /**
* @Id @ManyToOne(targetEntity="DDC117Article") * @Id @ManyToOne(targetEntity="DDC117Article", inversedBy="references")
*/ */
private $target; private $target;