2011-03-12 16:01:51 +03:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
use Doctrine\ORM\UnitOfWork ;
require_once __DIR__ . '/../../TestInit.php' ;
/**
* @ group DDC - 952
*/
class OneToOneEagerLoadingTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp ()
{
parent :: setUp ();
$schemaTool = new \Doctrine\ORM\Tools\SchemaTool ( $this -> _em );
try {
$schemaTool -> createSchema ( array (
$this -> _em -> getClassMetadata ( 'Doctrine\Tests\ORM\Functional\Train' ),
$this -> _em -> getClassMetadata ( 'Doctrine\Tests\ORM\Functional\TrainDriver' ),
2011-11-03 12:20:41 +04:00
$this -> _em -> getClassMetadata ( 'Doctrine\Tests\ORM\Functional\TrainOwner' ),
2011-03-12 16:01:51 +03:00
$this -> _em -> getClassMetadata ( 'Doctrine\Tests\ORM\Functional\Waggon' ),
));
} catch ( \Exception $e ) {}
}
public function testEagerLoadOneToOneOwningSide ()
{
2011-11-03 12:20:41 +04:00
$train = new Train ( new TrainOwner ( " Alexander " ));
2011-03-12 16:01:51 +03:00
$driver = new TrainDriver ( " Benjamin " );
$waggon = new Waggon ();
$train -> setDriver ( $driver );
$train -> addWaggon ( $waggon );
$this -> _em -> persist ( $train ); // cascades
$this -> _em -> flush ();
$this -> _em -> clear ();
$sqlCount = count ( $this -> _sqlLoggerStack -> queries );
$train = $this -> _em -> find ( get_class ( $train ), $train -> id );
$this -> assertNotInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $train -> driver );
$this -> assertEquals ( " Benjamin " , $train -> driver -> name );
$this -> assertEquals ( $sqlCount + 1 , count ( $this -> _sqlLoggerStack -> queries ));
}
public function testEagerLoadOneToOneNullOwningSide ()
{
2011-11-03 12:20:41 +04:00
$train = new Train ( new TrainOwner ( " Alexander " ));
2011-03-12 16:01:51 +03:00
$this -> _em -> persist ( $train ); // cascades
$this -> _em -> flush ();
$this -> _em -> clear ();
$sqlCount = count ( $this -> _sqlLoggerStack -> queries );
$train = $this -> _em -> find ( get_class ( $train ), $train -> id );
$this -> assertNotInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $train -> driver );
$this -> assertNull ( $train -> driver );
$this -> assertEquals ( $sqlCount + 1 , count ( $this -> _sqlLoggerStack -> queries ));
}
public function testEagerLoadOneToOneInverseSide ()
{
2011-11-03 12:20:41 +04:00
$owner = new TrainOwner ( " Alexander " );
$train = new Train ( $owner );
2011-03-12 16:01:51 +03:00
$this -> _em -> persist ( $train ); // cascades
$this -> _em -> flush ();
$this -> _em -> clear ();
$sqlCount = count ( $this -> _sqlLoggerStack -> queries );
2011-11-03 12:20:41 +04:00
$driver = $this -> _em -> find ( get_class ( $owner ), $owner -> id );
$this -> assertNotInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $owner -> train );
$this -> assertNotNull ( $owner -> train );
2011-03-12 16:01:51 +03:00
$this -> assertEquals ( $sqlCount + 1 , count ( $this -> _sqlLoggerStack -> queries ));
}
public function testEagerLoadOneToOneNullInverseSide ()
{
2011-03-12 21:11:37 +03:00
$driver = new TrainDriver ( " Dagny Taggert " );
2011-03-12 16:01:51 +03:00
$this -> _em -> persist ( $driver );
$this -> _em -> flush ();
$this -> _em -> clear ();
$this -> assertNull ( $driver -> train );
$sqlCount = count ( $this -> _sqlLoggerStack -> queries );
$driver = $this -> _em -> find ( get_class ( $driver ), $driver -> id );
$this -> assertNotInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $driver -> train );
$this -> assertNull ( $driver -> train );
$this -> assertEquals ( $sqlCount + 1 , count ( $this -> _sqlLoggerStack -> queries ));
}
2011-03-12 21:11:37 +03:00
public function testEagerLoadManyToOne ()
{
2011-11-03 12:20:41 +04:00
$train = new Train ( new TrainOwner ( " Alexander " ));
2011-03-12 21:11:37 +03:00
$waggon = new Waggon ();
$train -> addWaggon ( $waggon );
$this -> _em -> persist ( $train ); // cascades
$this -> _em -> flush ();
$this -> _em -> clear ();
$waggon = $this -> _em -> find ( get_class ( $waggon ), $waggon -> id );
$this -> assertNotInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $waggon -> train );
$this -> assertNotNull ( $waggon -> train );
}
2011-11-01 00:53:46 +04:00
2011-11-03 12:20:41 +04:00
public function testEagerLoadWithNullableColumnsGeneratesLeftJoinOnBothSides ()
2011-11-01 00:53:46 +04:00
{
2011-11-03 12:20:41 +04:00
$train = new Train ( new TrainOwner ( " Alexander " ));
$driver = new TrainDriver ( " Benjamin " );
$train -> setDriver ( $driver );
2011-11-01 00:53:46 +04:00
$this -> _em -> persist ( $train );
$this -> _em -> flush ();
$this -> _em -> clear ();
$train = $this -> _em -> find ( get_class ( $train ), $train -> id );
$this -> assertEquals (
2011-11-03 12:20:41 +04:00
" SELECT t0.id AS id1, t0.driver_id AS driver_id2, t3.id AS id4, t3.name AS name5, t0.owner_id AS owner_id6, t7.id AS id8, t7.name AS name9 FROM Train t0 LEFT JOIN TrainDriver t3 ON t0.driver_id = t3.id INNER JOIN TrainOwner t7 ON t0.owner_id = t7.id WHERE t0.id = ? " ,
$this -> _sqlLoggerStack -> queries [ $this -> _sqlLoggerStack -> currentQuery ][ 'sql' ]
);
$this -> _em -> clear ();
$driver = $this -> _em -> find ( get_class ( $driver ), $driver -> id );
$this -> assertEquals (
" SELECT t0.id AS id1, t0.name AS name2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM TrainOwner t0 LEFT JOIN Train t3 ON t3.owner_id = t0.id WHERE t0.id IN (?) " ,
2011-11-01 01:08:40 +04:00
$this -> _sqlLoggerStack -> queries [ $this -> _sqlLoggerStack -> currentQuery ][ 'sql' ]
2011-11-01 00:53:46 +04:00
);
}
2011-11-03 12:20:41 +04:00
public function testEagerLoadWithNonNullableColumnsGeneratesInnerJoinOnOwningSide ()
2011-11-01 00:53:46 +04:00
{
$waggon = new Waggon ();
2011-12-19 00:33:38 +04:00
// It should have a train
$train = new Train ( new TrainOwner ( " Alexander " ));
$train -> addWaggon ( $waggon );
$this -> _em -> persist ( $train );
2011-11-01 00:53:46 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
$waggon = $this -> _em -> find ( get_class ( $waggon ), $waggon -> id );
2011-12-19 00:33:38 +04:00
// The last query is the eager loading of the owner of the train
2011-11-01 00:53:46 +04:00
$this -> assertEquals (
2011-12-19 00:33:38 +04:00
" SELECT t0.id AS id1, t0.name AS name2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM TrainOwner t0 LEFT JOIN Train t3 ON t3.owner_id = t0.id WHERE t0.id IN (?) " ,
2011-11-03 12:20:41 +04:00
$this -> _sqlLoggerStack -> queries [ $this -> _sqlLoggerStack -> currentQuery ][ 'sql' ]
);
2011-12-19 00:33:38 +04:00
// The one before is the fetching of the waggon and train
$this -> assertEquals (
" SELECT t0.id AS id1, t0.train_id AS train_id2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM Waggon t0 INNER JOIN Train t3 ON t0.train_id = t3.id WHERE t0.id = ? " ,
$this -> _sqlLoggerStack -> queries [ $this -> _sqlLoggerStack -> currentQuery - 1 ][ 'sql' ]
);
2011-11-03 12:20:41 +04:00
}
public function testEagerLoadWithNonNullableColumnsGeneratesLeftJoinOnNonOwningSide ()
{
$owner = new TrainOwner ( 'Alexander' );
$train = new Train ( $owner );
$this -> _em -> persist ( $train );
$this -> _em -> flush ();
$this -> _em -> clear ();
$waggon = $this -> _em -> find ( get_class ( $owner ), $owner -> id );
$this -> assertEquals (
" SELECT t0.id AS id1, t0.name AS name2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM TrainOwner t0 LEFT JOIN Train t3 ON t3.owner_id = t0.id WHERE t0.id = ? " ,
2011-11-01 01:08:40 +04:00
$this -> _sqlLoggerStack -> queries [ $this -> _sqlLoggerStack -> currentQuery ][ 'sql' ]
2011-11-01 00:53:46 +04:00
);
}
2011-03-12 16:01:51 +03:00
}
/**
* @ Entity
*/
class Train
{
/**
* @ id @ column ( type = " integer " ) @ generatedValue
* @ var int
*/
public $id ;
/**
* Owning side
* @ OneToOne ( targetEntity = " TrainDriver " , inversedBy = " train " , fetch = " EAGER " , cascade = { " persist " })
2011-11-01 00:36:55 +04:00
* @ JoinColumn ( nullable = true )
2011-03-12 16:01:51 +03:00
*/
public $driver ;
2011-11-03 12:20:41 +04:00
/**
* Owning side
* @ OneToOne ( targetEntity = " TrainOwner " , inversedBy = " train " , fetch = " EAGER " , cascade = { " persist " })
2011-12-19 00:33:38 +04:00
* @ JoinColumn ( nullable = false )
2011-11-03 12:20:41 +04:00
*/
public $owner ;
2011-03-12 16:01:51 +03:00
/**
* @ oneToMany ( targetEntity = " Waggon " , mappedBy = " train " , cascade = { " persist " })
*/
public $waggons ;
2011-11-03 12:20:41 +04:00
public function __construct ( TrainOwner $owner )
2011-03-12 16:01:51 +03:00
{
$this -> waggons = new \Doctrine\Common\Collections\ArrayCollection ();
2011-11-03 12:20:41 +04:00
$this -> setOwner ( $owner );
2011-03-12 16:01:51 +03:00
}
public function setDriver ( TrainDriver $driver )
{
$this -> driver = $driver ;
$driver -> setTrain ( $this );
}
2011-11-03 12:20:41 +04:00
public function setOwner ( TrainOwner $owner )
{
$this -> owner = $owner ;
$owner -> setTrain ( $this );
}
2011-03-12 16:01:51 +03:00
public function addWaggon ( Waggon $w )
{
$w -> setTrain ( $this );
$this -> waggons [] = $w ;
}
}
/**
* @ Entity
*/
class TrainDriver
{
/** @Id @Column(type="integer") @GeneratedValue */
public $id ;
/** @column(type="string") */
public $name ;
/**
* Inverse side
* @ OneToOne ( targetEntity = " Train " , mappedBy = " driver " , fetch = " EAGER " )
*/
public $train ;
public function __construct ( $name )
{
$this -> name = $name ;
}
public function setTrain ( Train $t )
{
$this -> train = $t ;
}
}
2011-11-03 12:20:41 +04:00
/**
* @ Entity
*/
class TrainOwner
{
/** @Id @Column(type="integer") @GeneratedValue */
public $id ;
/** @column(type="string") */
public $name ;
/**
* Inverse side
* @ OneToOne ( targetEntity = " Train " , mappedBy = " owner " , fetch = " EAGER " )
*/
public $train ;
public function __construct ( $name )
{
$this -> name = $name ;
}
public function setTrain ( Train $t )
{
$this -> train = $t ;
}
}
2011-03-12 16:01:51 +03:00
/**
* @ Entity
*/
class Waggon
{
/** @id @generatedValue @column(type="integer") */
public $id ;
2011-12-19 00:33:38 +04:00
/**
* @ ManyToOne ( targetEntity = " Train " , inversedBy = " waggons " , fetch = " EAGER " )
* @ JoinColumn ( nullable = false )
*/
2011-03-12 16:01:51 +03:00
public $train ;
public function setTrain ( $train )
{
$this -> train = $train ;
}
2011-11-01 00:36:55 +04:00
}