2009-07-01 16:03:41 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
|
2009-07-18 17:15:54 +04:00
|
|
|
use Doctrine\ORM\Mapping\AssociationMapping;
|
2009-10-15 18:39:43 +04:00
|
|
|
use Doctrine\ORM\Query;
|
2009-07-01 16:03:41 +04:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a unidirectional one-to-one association mapping (without inheritance).
|
|
|
|
* Inverse side is not present.
|
|
|
|
*/
|
|
|
|
class OneToOneUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|
|
|
{
|
|
|
|
private $product;
|
|
|
|
private $shipping;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->useModelSet('ecommerce');
|
|
|
|
parent::setUp();
|
|
|
|
$this->product = new ECommerceProduct();
|
|
|
|
$this->product->setName('Doctrine 2 Manual');
|
|
|
|
$this->shipping = new ECommerceShipping();
|
|
|
|
$this->shipping->setDays('5');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
|
|
|
|
$this->product->setShipping($this->shipping);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->product);
|
2009-07-18 15:41:37 +04:00
|
|
|
$this->_em->flush();
|
2009-07-01 16:03:41 +04:00
|
|
|
|
|
|
|
$this->assertForeignKeyIs($this->shipping->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemovesOneToOneAssociation()
|
|
|
|
{
|
|
|
|
$this->product->setShipping($this->shipping);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->product);
|
2009-07-01 16:03:41 +04:00
|
|
|
$this->product->removeShipping();
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertForeignKeyIs(null);
|
|
|
|
}
|
|
|
|
|
2009-07-18 17:15:54 +04:00
|
|
|
public function _testEagerLoad()
|
2009-07-01 16:03:41 +04:00
|
|
|
{
|
2009-07-18 17:15:54 +04:00
|
|
|
$this->_createFixture();
|
2009-07-01 16:03:41 +04:00
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select p, s from Doctrine\Tests\Models\ECommerce\ECommerceProduct p left join p.shipping s');
|
2009-08-03 21:18:37 +04:00
|
|
|
$result = $query->getResult();
|
2009-07-01 16:03:41 +04:00
|
|
|
$product = $result[0];
|
|
|
|
|
|
|
|
$this->assertTrue($product->getShipping() instanceof ECommerceShipping);
|
|
|
|
$this->assertEquals(1, $product->getShipping()->getDays());
|
|
|
|
}
|
|
|
|
|
2009-07-20 16:05:19 +04:00
|
|
|
public function testLazyLoadsObjects() {
|
2009-07-18 17:15:54 +04:00
|
|
|
$this->_createFixture();
|
|
|
|
$metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
|
|
|
|
$metadata->getAssociationMapping('shipping')->fetchMode = AssociationMapping::FETCH_LAZY;
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
|
2009-08-03 21:18:37 +04:00
|
|
|
$result = $query->getResult();
|
2009-07-18 17:15:54 +04:00
|
|
|
$product = $result[0];
|
2009-07-01 16:03:41 +04:00
|
|
|
|
2009-07-18 17:15:54 +04:00
|
|
|
$this->assertTrue($product->getShipping() instanceof ECommerceShipping);
|
|
|
|
$this->assertEquals(1, $product->getShipping()->getDays());
|
|
|
|
}
|
|
|
|
|
2009-07-20 16:05:19 +04:00
|
|
|
public function testDoesNotLazyLoadObjectsIfConfigurationDoesNotAllowIt() {
|
|
|
|
$this->_createFixture();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
|
2009-10-15 18:39:43 +04:00
|
|
|
$query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
|
|
|
|
|
2009-08-03 21:18:37 +04:00
|
|
|
$result = $query->getResult();
|
2009-07-20 16:05:19 +04:00
|
|
|
$product = $result[0];
|
|
|
|
|
|
|
|
$this->assertNull($product->getShipping());
|
|
|
|
}
|
|
|
|
|
2009-07-18 17:15:54 +04:00
|
|
|
protected function _createFixture()
|
|
|
|
{
|
|
|
|
$product = new ECommerceProduct;
|
|
|
|
$product->setName('Php manual');
|
|
|
|
$shipping = new ECommerceShipping;
|
|
|
|
$shipping->setDays('1');
|
|
|
|
$product->setShipping($shipping);
|
|
|
|
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($product);
|
2009-07-18 17:15:54 +04:00
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
}
|
2009-07-01 16:03:41 +04:00
|
|
|
|
|
|
|
public function assertForeignKeyIs($value) {
|
2010-04-01 01:13:34 +04:00
|
|
|
$foreignKey = $this->_em->getConnection()->executeQuery(
|
|
|
|
'SELECT shipping_id FROM ecommerce_products WHERE id=?',
|
|
|
|
array($this->product->getId())
|
|
|
|
)->fetchColumn();
|
2009-07-01 16:03:41 +04:00
|
|
|
$this->assertEquals($value, $foreignKey);
|
|
|
|
}
|
|
|
|
}
|