forked from doctrine/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2.0] DDC-289 / DDC-264 - Added a Model and TestCase for OneToMany Un…
…idirectional Assocations using a JoinTable and tested its defining feature (uniqueness).
- Loading branch information
beberlei
committed
Jan 31, 2010
1 parent
3bec768
commit 8d607b1
Showing
6 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\Models\Routing; | ||
|
||
/** | ||
* @Entity | ||
*/ | ||
class RoutingLeg | ||
{ | ||
/** | ||
* @Id | ||
* @generatedValue(strategy="AUTO") | ||
* @column(type="integer") | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ManyToOne(targetEntity="Doctrine\Tests\Models\Routing\RoutingLocation") | ||
* @JoinColumn(name="from_id", referencedColumnName="id") | ||
*/ | ||
public $fromLocation; | ||
|
||
/** | ||
* @ManyToOne(targetEntity="Doctrine\Tests\Models\Routing\RoutingLocation") | ||
* @JoinColumn(name="to_id", referencedColumnName="id") | ||
*/ | ||
public $toLocation; | ||
|
||
/** | ||
* @Column(type="datetime") | ||
*/ | ||
public $departureDate; | ||
|
||
/** | ||
* @Column(type="datetime") | ||
*/ | ||
public $arrivalDate; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\Models\Routing; | ||
|
||
/** | ||
* @Entity | ||
*/ | ||
class RoutingLocation | ||
{ | ||
/** | ||
* @Id | ||
* @generatedValue(strategy="AUTO") | ||
* @Column(type="integer") | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @Column(type="string") | ||
*/ | ||
public $name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\Models\Routing; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
|
||
/** | ||
* @Entity | ||
*/ | ||
class RoutingRoute | ||
{ | ||
/** | ||
* @Id | ||
* @generatedValue(strategy="AUTO") | ||
* @column(type="integer") | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ManyToMany(targetEntity="Doctrine\Tests\Models\Routing\RoutingLeg", cascade={"all"}) | ||
* @JoinTable(name="RoutingRouteLegs", | ||
* joinColumns={@JoinColumn(name="route_id", referencedColumnName="id")}, | ||
* inverseJoinColumns={@JoinColumn(name="leg_id", referencedColumnName="id", unique=true)} | ||
* ) | ||
*/ | ||
public $legs; | ||
|
||
public function __construct() | ||
{ | ||
$this->legs = new ArrayCollection(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
tests/Doctrine/Tests/ORM/Functional/OneToManyUnidirectionalAssociationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\ORM\Functional; | ||
|
||
use Doctrine\Tests\Models\Routing\RoutingRoute; | ||
use Doctrine\Tests\Models\Routing\RoutingLocation; | ||
use Doctrine\Tests\Models\Routing\RoutingLeg; | ||
|
||
require_once __DIR__ . '/../../TestInit.php'; | ||
|
||
/** | ||
* Tests a bidirectional one-to-one association mapping (without inheritance). | ||
*/ | ||
class OneToManyUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase | ||
{ | ||
protected $locations = array(); | ||
|
||
public function setUp() | ||
{ | ||
$this->useModelSet('routing'); | ||
parent::setUp(); | ||
|
||
$locations = array("Berlin", "Bonn", "Brasilia", "Atlanta"); | ||
|
||
foreach ($locations AS $locationName) { | ||
$location = new RoutingLocation(); | ||
$location->name = $locationName; | ||
$this->_em->persist($location); | ||
$this->locations[$locationName] = $location; | ||
} | ||
$this->_em->flush(); | ||
} | ||
|
||
public function testPersistOwning_InverseCascade() | ||
{ | ||
$leg = new RoutingLeg(); | ||
$leg->fromLocation = $this->locations['Berlin']; | ||
$leg->toLocation = $this->locations['Bonn']; | ||
$leg->departureDate = new \DateTime("now"); | ||
$leg->arrivalDate = new \DateTime("now +5 hours"); | ||
|
||
$route = new RoutingRoute(); | ||
$route->legs[] = $leg; | ||
|
||
$this->_em->persist($route); | ||
$this->_em->flush(); | ||
$this->_em->clear(); | ||
|
||
$routes = $this->_em->createQuery( | ||
"SELECT r, l, f, t FROM Doctrine\Tests\Models\Routing\RoutingRoute r ". | ||
"JOIN r.legs l JOIN l.fromLocation f JOIN l.toLocation t" | ||
)->getSingleResult(); | ||
|
||
$this->assertEquals(1, count($routes->legs)); | ||
$this->assertEquals("Berlin", $routes->legs[0]->fromLocation->name); | ||
$this->assertEquals("Bonn", $routes->legs[0]->toLocation->name); | ||
} | ||
|
||
public function testLegsAreUniqueToRoutes() | ||
{ | ||
$leg = new RoutingLeg(); | ||
$leg->fromLocation = $this->locations['Berlin']; | ||
$leg->toLocation = $this->locations['Bonn']; | ||
$leg->departureDate = new \DateTime("now"); | ||
$leg->arrivalDate = new \DateTime("now +5 hours"); | ||
|
||
$routeA = new RoutingRoute(); | ||
$routeA->legs[] = $leg; | ||
|
||
$routeB = new RoutingRoute(); | ||
$routeB->legs[] = $leg; | ||
|
||
$this->_em->persist($routeA); | ||
$this->_em->persist($routeB); | ||
|
||
$this->setExpectedException('Exception'); // depends on the underyling Database Driver | ||
$this->_em->flush(); // Exception | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters