2009-07-06 12:42:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
2009-07-28 11:43:42 +00:00
|
|
|
use Doctrine\ORM\Mapping\AssociationMapping;
|
2010-08-09 13:13:21 +02:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
2016-12-08 18:01:04 +01:00
|
|
|
use Doctrine\ORM\Proxy\Proxy;
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
|
2016-05-11 02:41:26 +07:00
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase;
|
2009-07-06 12:42:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a self referential one-to-one association mapping (without inheritance).
|
2011-12-19 22:56:19 +01:00
|
|
|
* Relation is defined as the mentor that a customer choose. The mentor could
|
2009-07-06 12:42:14 +00:00
|
|
|
* help only one other customer, while a customer can choose only one mentor
|
|
|
|
* for receiving support.
|
|
|
|
* Inverse side is not present.
|
|
|
|
*/
|
2016-05-11 02:41:26 +07:00
|
|
|
class OneToOneSelfReferentialAssociationTest extends OrmFunctionalTestCase
|
2009-07-06 12:42:14 +00:00
|
|
|
{
|
|
|
|
private $customer;
|
|
|
|
private $mentor;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->useModelSet('ecommerce');
|
|
|
|
parent::setUp();
|
|
|
|
$this->customer = new ECommerceCustomer();
|
|
|
|
$this->customer->setName('Anakin Skywalker');
|
|
|
|
$this->mentor = new ECommerceCustomer();
|
|
|
|
$this->mentor->setName('Obi-wan Kenobi');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
|
|
|
|
$this->customer->setMentor($this->mentor);
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($this->customer);
|
2009-07-18 11:41:37 +00:00
|
|
|
$this->_em->flush();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-06 12:42:14 +00:00
|
|
|
$this->assertForeignKeyIs($this->mentor->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemovesOneToOneAssociation()
|
|
|
|
{
|
|
|
|
$this->customer->setMentor($this->mentor);
|
2009-07-19 16:54:53 +00:00
|
|
|
$this->_em->persist($this->customer);
|
2009-07-06 12:42:14 +00:00
|
|
|
$this->customer->removeMentor();
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertForeignKeyIs(null);
|
|
|
|
}
|
|
|
|
|
2011-07-09 15:40:10 +02:00
|
|
|
public function testFind()
|
|
|
|
{
|
|
|
|
$id = $this->_createFixture();
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$customer = $this->_em->find(ECommerceCustomer::class, $id);
|
|
|
|
$this->assertNotInstanceOf(Proxy::class, $customer->getMentor());
|
2011-07-09 15:40:10 +02:00
|
|
|
}
|
|
|
|
|
2009-07-28 11:43:42 +00:00
|
|
|
public function testEagerLoadsAssociation()
|
2009-07-06 12:42:14 +00:00
|
|
|
{
|
2009-07-28 11:43:42 +00:00
|
|
|
$this->_createFixture();
|
2009-07-06 12:42:14 +00:00
|
|
|
|
2009-07-18 11:41:37 +00:00
|
|
|
$query = $this->_em->createQuery('select c, m from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c left join c.mentor m order by c.id asc');
|
2009-08-03 17:18:37 +00:00
|
|
|
$result = $query->getResult();
|
2009-07-06 12:42:14 +00:00
|
|
|
$customer = $result[0];
|
2009-07-28 11:43:42 +00:00
|
|
|
$this->assertLoadingOfAssociation($customer);
|
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-08-13 10:13:06 +00:00
|
|
|
/**
|
|
|
|
* @group mine
|
|
|
|
* @return unknown_type
|
|
|
|
*/
|
2009-07-28 11:43:42 +00:00
|
|
|
public function testLazyLoadsAssociation()
|
2011-12-19 22:56:19 +01:00
|
|
|
{
|
2009-07-28 11:43:42 +00:00
|
|
|
$this->_createFixture();
|
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$metadata = $this->_em->getClassMetadata(ECommerceCustomer::class);
|
2010-08-09 13:13:21 +02:00
|
|
|
$metadata->associationMappings['mentor']['fetch'] = ClassMetadata::FETCH_LAZY;
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-08-13 10:13:06 +00:00
|
|
|
$query = $this->_em->createQuery("select c from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c where c.name='Luke Skywalker'");
|
2009-08-03 17:18:37 +00:00
|
|
|
$result = $query->getResult();
|
2009-07-28 11:43:42 +00:00
|
|
|
$customer = $result[0];
|
|
|
|
$this->assertLoadingOfAssociation($customer);
|
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-15 11:42:05 +00:00
|
|
|
public function testMultiSelfReference()
|
|
|
|
{
|
|
|
|
try {
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->_schemaTool->createSchema(
|
|
|
|
[
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->_em->getClassMetadata(MultiSelfReference::class)
|
2016-12-07 23:33:41 +01:00
|
|
|
]
|
|
|
|
);
|
2009-11-15 11:42:05 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
// Swallow all exceptions. We do not test the schema tool here.
|
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-15 11:42:05 +00:00
|
|
|
$entity1 = new MultiSelfReference();
|
|
|
|
$this->_em->persist($entity1);
|
|
|
|
$entity1->setOther1($entity2 = new MultiSelfReference);
|
|
|
|
$entity1->setOther2($entity3 = new MultiSelfReference);
|
|
|
|
$this->_em->flush();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-15 11:42:05 +00:00
|
|
|
$this->_em->clear();
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-15 11:42:05 +00:00
|
|
|
$entity2 = $this->_em->find(get_class($entity1), $entity1->getId());
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(MultiSelfReference::class, $entity2->getOther1());
|
|
|
|
$this->assertInstanceOf(MultiSelfReference::class, $entity2->getOther2());
|
2009-11-15 11:42:05 +00:00
|
|
|
$this->assertNull($entity2->getOther1()->getOther1());
|
|
|
|
$this->assertNull($entity2->getOther1()->getOther2());
|
|
|
|
$this->assertNull($entity2->getOther2()->getOther1());
|
|
|
|
$this->assertNull($entity2->getOther2()->getOther2());
|
|
|
|
}
|
2009-07-28 11:43:42 +00:00
|
|
|
|
|
|
|
public function assertLoadingOfAssociation($customer)
|
|
|
|
{
|
2016-12-08 18:01:04 +01:00
|
|
|
$this->assertInstanceOf(ECommerceCustomer::class, $customer->getMentor());
|
2009-07-06 12:42:14 +00:00
|
|
|
$this->assertEquals('Obi-wan Kenobi', $customer->getMentor()->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function assertForeignKeyIs($value) {
|
2016-12-07 23:33:41 +01:00
|
|
|
$foreignKey = $this->_em->getConnection()->executeQuery('SELECT mentor_id FROM ecommerce_customers WHERE id=?', [$this->customer->getId()]
|
|
|
|
)->fetchColumn();
|
2009-07-06 12:42:14 +00:00
|
|
|
$this->assertEquals($value, $foreignKey);
|
|
|
|
}
|
2009-07-28 11:43:42 +00:00
|
|
|
|
|
|
|
private function _createFixture()
|
|
|
|
{
|
|
|
|
$customer = new ECommerceCustomer;
|
|
|
|
$customer->setName('Luke Skywalker');
|
|
|
|
$mentor = new ECommerceCustomer;
|
|
|
|
$mentor->setName('Obi-wan Kenobi');
|
|
|
|
$customer->setMentor($mentor);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-28 11:43:42 +00:00
|
|
|
$this->_em->persist($customer);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-07-28 11:43:42 +00:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-07-09 15:40:10 +02:00
|
|
|
|
|
|
|
return $customer->getId();
|
2009-07-28 11:43:42 +00:00
|
|
|
}
|
2009-07-06 12:42:14 +00:00
|
|
|
}
|
2009-11-15 11:42:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
*/
|
|
|
|
class MultiSelfReference {
|
|
|
|
/** @Id @GeneratedValue(strategy="AUTO") @Column(type="integer") */
|
|
|
|
private $id;
|
|
|
|
/**
|
|
|
|
* @OneToOne(targetEntity="MultiSelfReference", cascade={"persist"})
|
|
|
|
* @JoinColumn(name="other1", referencedColumnName="id")
|
|
|
|
*/
|
|
|
|
private $other1;
|
|
|
|
/**
|
|
|
|
* @OneToOne(targetEntity="MultiSelfReference", cascade={"persist"})
|
|
|
|
* @JoinColumn(name="other2", referencedColumnName="id")
|
|
|
|
*/
|
|
|
|
private $other2;
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-15 11:42:05 +00:00
|
|
|
public function getId() {return $this->id;}
|
|
|
|
public function setOther1($other1) {$this->other1 = $other1;}
|
|
|
|
public function getOther1() {return $this->other1;}
|
|
|
|
public function setOther2($other2) {$this->other2 = $other2;}
|
|
|
|
public function getOther2() {return $this->other2;}
|
|
|
|
}
|