123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- <?php
-
- namespace Doctrine\Tests\ORM\Functional;
-
- use Doctrine\ORM\Query;
-
- require_once __DIR__ . '/../../TestInit.php';
-
-
- class AdvancedAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
- {
- protected function setUp() {
- parent::setUp();
- try {
- $this->_schemaTool->createSchema(array(
- $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Phrase'),
- $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\PhraseType'),
- $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Definition'),
- $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Lemma'),
- $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Type')
- ));
- } catch (\Exception $e) {
-
- }
- }
-
- public function testIssue()
- {
-
- $phrase = new Phrase;
- $phrase->setPhrase('lalala');
-
- $type = new PhraseType;
- $type->setType('nonsense');
- $type->setAbbreviation('non');
-
- $def1 = new Definition;
- $def1->setDefinition('def1');
- $def2 = new Definition;
- $def2->setDefinition('def2');
-
- $phrase->setType($type);
- $phrase->addDefinition($def1);
- $phrase->addDefinition($def2);
-
- $this->_em->persist($phrase);
- $this->_em->persist($type);
-
- $this->_em->flush();
- $this->_em->clear();
-
-
-
- $phrase2 = $this->_em->find('Doctrine\Tests\ORM\Functional\Phrase', $phrase->getId());
- $this->assertTrue(is_numeric($phrase2->getType()->getId()));
-
- $this->_em->clear();
-
-
- $query = $this->_em->createQuery("SELECT p,t FROM Doctrine\Tests\ORM\Functional\Phrase p JOIN p.type t");
- $res = $query->getResult();
- $this->assertEquals(1, count($res));
- $this->assertTrue($res[0]->getType() instanceof PhraseType);
- $this->assertTrue($res[0]->getType()->getPhrases() instanceof \Doctrine\ORM\PersistentCollection);
- $this->assertFalse($res[0]->getType()->getPhrases()->isInitialized());
-
- $this->_em->clear();
-
-
- $query = $this->_em->createQuery("SELECT p,t,pp FROM Doctrine\Tests\ORM\Functional\Phrase p JOIN p.type t JOIN t.phrases pp");
- $res = $query->getResult();
- $this->assertEquals(1, count($res));
- $this->assertTrue($res[0]->getType() instanceof PhraseType);
- $this->assertTrue($res[0]->getType()->getPhrases() instanceof \Doctrine\ORM\PersistentCollection);
- $this->assertTrue($res[0]->getType()->getPhrases()->isInitialized());
-
- $this->_em->clear();
-
-
- $phrase3 = $this->_em->find('Doctrine\Tests\ORM\Functional\Phrase', $phrase->getId());
- $definitions = $phrase3->getDefinitions();
- $this->assertTrue($definitions instanceof \Doctrine\ORM\PersistentCollection);
- $this->assertTrue($definitions[0] instanceof Definition);
-
- $this->_em->clear();
-
-
- $query = $this->_em->createQuery("SELECT p FROM Doctrine\Tests\ORM\Functional\Phrase p");
- $res = $query->getResult();
- $definitions = $res[0]->getDefinitions();
-
- $this->assertEquals(1, count($res));
-
- $this->assertTrue($definitions[0] instanceof Definition);
- $this->assertEquals(2, $definitions->count());
- }
-
- public function testManyToMany()
- {
- $lemma = new Lemma;
- $lemma->setLemma('abu');
-
- $type = new Type();
- $type->setType('nonsense');
- $type->setAbbreviation('non');
-
- $lemma->addType($type);
-
- $this->_em->persist($lemma);
- $this->_em->persist($type);
- $this->_em->flush();
-
-
- $query = $this->_em->createQuery("SELECT l FROM Doctrine\Tests\ORM\Functional\Lemma l");
- $res = $query->getResult();
- $types = $res[0]->getTypes();
-
- $this->assertTrue($types[0] instanceof Type);
- }
- }
-
-
- class Lemma {
-
- const CLASS_NAME = __CLASS__;
-
-
-
- private $id;
-
-
-
- private $lemma;
-
-
-
- private $types;
-
- public function __construct() {
- $this->types = new \Doctrine\Common\Collections\ArrayCollection();
- }
-
-
-
-
- public function getId(){
- return $this->id;
- }
-
-
-
- public function setLemma($lemma){
- $this->lemma = $lemma;
- }
-
-
-
- public function getLemma(){
- return $this->lemma;
- }
-
-
-
- public function addType(Type $type){
- if (!$this->types->contains($type)) {
- $this->types[] = $type;
- $type->addLemma($this);
- }
- }
-
-
-
- public function removeType(Type $type)
- {
- $removed = $this->sources->removeElement($type);
- if ($removed !== null) {
- $removed->removeLemma($this);
- }
- }
-
-
-
- public function getTypes()
- {
- return $this->types;
- }
- }
-
-
- class Type {
-
- const CLASS_NAME = __CLASS__;
-
-
-
- private $id;
-
-
-
- private $type;
-
-
-
- private $abbreviation;
-
-
-
- private $lemmas;
-
- public function __construct(){
- $this->lemmas = new \Doctrine\Common\Collections\ArrayCollection();
- }
-
-
-
- public function getId(){
- return $this->id;
- }
-
-
-
- public function setType($type){
- $this->type = $type;
- }
-
-
-
- public function getType(){
- return $this->type;
- }
-
-
-
- public function setAbbreviation($abbreviation){
- $this->abbreviation = $abbreviation;
- }
-
-
-
- public function getAbbreviation(){
- return $this->abbreviation;
- }
-
-
-
- public function addLemma(Lemma $lemma)
- {
- if (!$this->lemmas->contains($lemma)) {
- $this->lemmas[] = $lemma;
- $lemma->addType($this);
- }
- }
-
-
-
- public function removeLEmma(Lemma $lemma)
- {
- $removed = $this->lemmas->removeElement($lemma);
- if ($removed !== null) {
- $removed->removeType($this);
- }
- }
-
-
-
- public function getCategories()
- {
- return $this->categories;
- }
-
- }
-
-
-
- class Phrase {
-
- const CLASS_NAME = __CLASS__;
-
-
-
- private $id;
-
-
-
- private $phrase;
-
-
-
- private $type;
-
-
-
- private $definitions;
-
- public function __construct() {
- $this->definitions = new \Doctrine\Common\Collections\ArrayCollection;
- }
-
-
-
- public function addDefinition(Definition $definition){
- $this->definitions[] = $definition;
- $definition->setPhrase($this);
- }
-
-
-
- public function getId(){
- return $this->id;
- }
-
-
-
- public function setPhrase($phrase){
- $this->phrase = $phrase;
- }
-
-
-
- public function getPhrase(){
- return $this->phrase;
- }
-
-
-
- public function setType(PhraseType $type){
- $this->type = $type;
- }
-
-
-
- public function getType(){
- return $this->type;
- }
-
-
-
- public function getDefinitions(){
- return $this->definitions;
- }
- }
-
-
- class PhraseType {
-
- const CLASS_NAME = __CLASS__;
-
-
-
- private $id;
-
-
-
- private $type;
-
-
-
- private $abbreviation;
-
-
-
- private $phrases;
-
- public function __construct() {
- $this->phrases = new \Doctrine\Common\Collections\ArrayCollection;
- }
-
-
-
- public function getId(){
- return $this->id;
- }
-
-
-
- public function setType($type){
- $this->type = $type;
- }
-
-
-
- public function getType(){
- return $this->type;
- }
-
-
-
- public function setAbbreviation($abbreviation){
- $this->abbreviation = $abbreviation;
- }
-
-
-
- public function getAbbreviation(){
- return $this->abbreviation;
- }
-
-
-
- public function setPhrases($phrases){
- $this->phrases = $phrases;
- }
-
-
-
- public function getPhrases(){
- return $this->phrases;
- }
-
- }
-
-
- class Definition {
-
- const CLASS_NAME = __CLASS__;
-
-
-
- private $id;
-
-
-
- private $phrase;
-
-
-
- private $definition;
-
-
-
- public function getId(){
- return $this->id;
- }
-
-
-
- public function setPhrase(Phrase $phrase){
- $this->phrase = $phrase;
- }
-
-
-
- public function getPhrase(){
- return $this->phrase;
- }
-
- public function removePhrase() {
- if ($this->phrase !== null) {
-
- $phrase = $this->phrase;
- $this->phrase = null;
- $phrase->removeDefinition($this);
- }
- }
-
-
-
- public function setDefinition($definition){
- $this->definition = $definition;
- }
-
-
-
- public function getDefinition(){
- return $this->definition;
- }
- }
|