2009-07-01 17:11:45 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
|
|
|
|
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
|
2009-07-28 15:43:42 +04:00
|
|
|
use Doctrine\ORM\Mapping\AssociationMapping;
|
2009-07-01 17:11:45 +04:00
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a bidirectional one-to-one association mapping (without inheritance).
|
|
|
|
*/
|
|
|
|
class OneToManyBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|
|
|
{
|
|
|
|
private $product;
|
|
|
|
private $firstFeature;
|
2009-07-01 17:50:26 +04:00
|
|
|
private $secondFeature;
|
2009-07-01 17:11:45 +04:00
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->useModelSet('ecommerce');
|
|
|
|
parent::setUp();
|
|
|
|
$this->product = new ECommerceProduct();
|
|
|
|
$this->product->setName('Doctrine Cookbook');
|
|
|
|
$this->firstFeature = new ECommerceFeature();
|
|
|
|
$this->firstFeature->setDescription('Model writing tutorial');
|
|
|
|
$this->secondFeature = new ECommerceFeature();
|
|
|
|
$this->secondFeature->setDescription('Annotations examples');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavesAOneToManyAssociationWithCascadeSaveSet() {
|
|
|
|
$this->product->addFeature($this->firstFeature);
|
|
|
|
$this->product->addFeature($this->secondFeature);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->product);
|
2009-07-18 15:41:37 +04:00
|
|
|
$this->_em->flush();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-07-01 17:11:45 +04:00
|
|
|
$this->assertFeatureForeignKeyIs($this->product->getId(), $this->firstFeature);
|
|
|
|
$this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature);
|
|
|
|
}
|
|
|
|
|
2009-07-02 11:47:57 +04:00
|
|
|
public function testSavesAnEmptyCollection()
|
|
|
|
{
|
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-02 11:47:57 +04:00
|
|
|
|
|
|
|
$this->assertEquals(0, count($this->product->getFeatures()));
|
|
|
|
}
|
|
|
|
|
2009-07-01 17:11:45 +04:00
|
|
|
public function testDoesNotSaveAnInverseSideSet() {
|
|
|
|
$this->product->brokenAddFeature($this->firstFeature);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->product);
|
2009-07-18 15:41:37 +04:00
|
|
|
$this->_em->flush();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-07-01 17:11:45 +04:00
|
|
|
$this->assertFeatureForeignKeyIs(null, $this->firstFeature);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemovesOneToOneAssociation()
|
|
|
|
{
|
|
|
|
$this->product->addFeature($this->firstFeature);
|
|
|
|
$this->product->addFeature($this->secondFeature);
|
2009-07-19 20:54:53 +04:00
|
|
|
$this->_em->persist($this->product);
|
2009-07-01 17:11:45 +04:00
|
|
|
|
|
|
|
$this->product->removeFeature($this->firstFeature);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertFeatureForeignKeyIs(null, $this->firstFeature);
|
|
|
|
$this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEagerLoadsOneToManyAssociation()
|
|
|
|
{
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_createFixture();
|
2009-07-01 17:11:45 +04:00
|
|
|
$query = $this->_em->createQuery('select p, f from Doctrine\Tests\Models\ECommerce\ECommerceProduct p join p.features f');
|
2009-08-03 21:18:37 +04:00
|
|
|
$result = $query->getResult();
|
2009-07-01 17:11:45 +04:00
|
|
|
$product = $result[0];
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-07-01 17:11:45 +04:00
|
|
|
$features = $product->getFeatures();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $features[0]);
|
|
|
|
$this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $features[0]->getProduct());
|
2009-07-01 17:11:45 +04:00
|
|
|
$this->assertSame($product, $features[0]->getProduct());
|
|
|
|
$this->assertEquals('Model writing tutorial', $features[0]->getDescription());
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $features[1]);
|
2009-07-01 17:11:45 +04:00
|
|
|
$this->assertSame($product, $features[1]->getProduct());
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $features[1]->getProduct());
|
2009-07-01 17:11:45 +04:00
|
|
|
$this->assertEquals('Annotations examples', $features[1]->getDescription());
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
public function testLazyLoadsObjectsOnTheOwningSide()
|
|
|
|
{
|
|
|
|
$this->_createFixture();
|
|
|
|
|
|
|
|
$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-28 15:43:42 +04:00
|
|
|
$product = $result[0];
|
|
|
|
$features = $product->getFeatures();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-01-15 16:33:42 +03:00
|
|
|
$this->assertFalse($features->isInitialized());
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $features[0]);
|
2010-01-15 16:33:42 +03:00
|
|
|
$this->assertTrue($features->isInitialized());
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->assertSame($product, $features[0]->getProduct());
|
|
|
|
$this->assertEquals('Model writing tutorial', $features[0]->getDescription());
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $features[1]);
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->assertSame($product, $features[1]->getProduct());
|
|
|
|
$this->assertEquals('Annotations examples', $features[1]->getDescription());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLazyLoadsObjectsOnTheInverseSide()
|
|
|
|
{
|
|
|
|
$this->_createFixture();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select f from Doctrine\Tests\Models\ECommerce\ECommerceFeature f');
|
2009-08-03 21:18:37 +04:00
|
|
|
$features = $query->getResult();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
$product = $features[0]->getProduct();
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $product);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $product);
|
2010-01-15 16:33:42 +03:00
|
|
|
$this->assertFalse($product->__isInitialized__);
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->assertSame('Doctrine Cookbook', $product->getName());
|
2010-01-15 16:33:42 +03:00
|
|
|
$this->assertTrue($product->__isInitialized__);
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-01-15 16:33:42 +03:00
|
|
|
public function testLazyLoadsObjectsOnTheInverseSide2()
|
|
|
|
{
|
2010-04-01 00:47:35 +04:00
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
|
2010-01-15 16:33:42 +03:00
|
|
|
$this->_createFixture();
|
|
|
|
|
|
|
|
$query = $this->_em->createQuery('select f,p from Doctrine\Tests\Models\ECommerce\ECommerceFeature f join f.product p');
|
|
|
|
$features = $query->getResult();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-01-15 16:33:42 +03:00
|
|
|
$product = $features[0]->getProduct();
|
2011-07-26 17:22:57 +04:00
|
|
|
$this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $product);
|
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $product);
|
2010-01-15 16:33:42 +03:00
|
|
|
$this->assertSame('Doctrine Cookbook', $product->getName());
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-01-15 16:33:42 +03:00
|
|
|
$this->assertFalse($product->getFeatures()->isInitialized());
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-01-15 16:33:42 +03:00
|
|
|
// This would trigger lazy-load
|
|
|
|
//$this->assertEquals(2, $product->getFeatures()->count());
|
|
|
|
//$this->assertTrue($product->getFeatures()->contains($features[0]));
|
|
|
|
//$this->assertTrue($product->getFeatures()->contains($features[1]));
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-04-01 00:47:35 +04:00
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(null);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-10-15 18:39:43 +04:00
|
|
|
public function testJoinFromOwningSide()
|
|
|
|
{
|
|
|
|
$query = $this->_em->createQuery('select f,p from Doctrine\Tests\Models\ECommerce\ECommerceFeature f join f.product p');
|
|
|
|
$features = $query->getResult();
|
|
|
|
$this->assertEquals(0, count($features));
|
|
|
|
}
|
2009-07-28 15:43:42 +04:00
|
|
|
|
2012-06-19 02:01:03 +04:00
|
|
|
/**
|
|
|
|
* @group DDC-1637
|
|
|
|
*/
|
|
|
|
public function testMatching()
|
|
|
|
{
|
|
|
|
$this->_createFixture();
|
|
|
|
|
|
|
|
$product = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $this->product->getId());
|
|
|
|
$features = $product->getFeatures();
|
|
|
|
|
|
|
|
$results = $features->matching(new \Doctrine\Common\Collections\Criteria(
|
|
|
|
$features->expr()->eq('description', 'Model writing tutorial')
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results);
|
|
|
|
$this->assertEquals(1, count($results));
|
|
|
|
|
|
|
|
$results = $features->matching(new \Doctrine\Common\Collections\Criteria());
|
|
|
|
|
|
|
|
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results);
|
|
|
|
$this->assertEquals(2, count($results));
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
private function _createFixture()
|
|
|
|
{
|
|
|
|
$this->product->addFeature($this->firstFeature);
|
|
|
|
$this->product->addFeature($this->secondFeature);
|
|
|
|
$this->_em->persist($this->product);
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
}
|
2009-07-01 17:11:45 +04:00
|
|
|
|
|
|
|
public function assertFeatureForeignKeyIs($value, ECommerceFeature $feature) {
|
2010-04-01 01:13:34 +04:00
|
|
|
$foreignKey = $this->_em->getConnection()->executeQuery(
|
|
|
|
'SELECT product_id FROM ecommerce_features WHERE id=?',
|
|
|
|
array($feature->getId())
|
|
|
|
)->fetchColumn();
|
2009-07-01 17:11:45 +04:00
|
|
|
$this->assertEquals($value, $foreignKey);
|
|
|
|
}
|
|
|
|
}
|