SingleTableInheritanceTest.php 10.1 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\ORM\Functional;

5 6
use Doctrine\ORM\Query;

7 8 9 10 11 12 13 14 15 16 17
require_once __DIR__ . '/../../TestInit.php';

/**
 * Functional tests for the Single Table Inheritance mapping strategy.
 *
 * @author robo
 */
class SingleTableInheritanceTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
    protected function setUp() {
        parent::setUp();
18 19 20 21
        try {
            $this->_schemaTool->createSchema(array(
                $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\ParentEntity'),
                $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\ChildEntity'),
22 23
                $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\RelatedEntity'),
                $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\ParentRelatedEntity')
24 25
            ));
        } catch (\Exception $e) {
26
            // Swallow all exceptions. We do not test the schema tool here.
27
        }
28 29
    }

30
    public function testCRUD()
31
    {        
32 33 34
        $parent = new ParentEntity;
        $parent->setData('foobar');

romanb's avatar
romanb committed
35
        $this->_em->persist($parent);
36

37 38 39 40
        $child = new ChildEntity;
        $child->setData('thedata');
        $child->setNumber(1234);

romanb's avatar
romanb committed
41
        $this->_em->persist($child);
42 43 44 45 46

        $relatedEntity = new RelatedEntity;
        $relatedEntity->setName('theRelatedOne');
        $relatedEntity->setOwner($child);

romanb's avatar
romanb committed
47
        $this->_em->persist($relatedEntity);
48

49 50 51
        $this->_em->flush();
        $this->_em->clear();

romanb's avatar
romanb committed
52
        $query = $this->_em->createQuery("select e from Doctrine\Tests\ORM\Functional\ParentEntity e order by e.data asc");
53
        $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
54
        $entities = $query->getResult();
romanb's avatar
romanb committed
55
        
56
        $this->assertEquals(2, count($entities));
57 58
        $this->assertTrue(is_numeric($entities[0]->getId()));
        $this->assertTrue(is_numeric($entities[1]->getId()));
romanb's avatar
romanb committed
59
        $this->assertTrue($entities[0] instanceof ParentEntity);
60 61 62
        $this->assertTrue($entities[1] instanceof ChildEntity);
        $this->assertNull($entities[0]->getParentRelated());
        $this->assertNull($entities[1]->getParentRelated());
63 64 65
        $this->assertEquals('foobar', $entities[0]->getData());
        $this->assertEquals('thedata', $entities[1]->getData());
        $this->assertEquals(1234, $entities[1]->getNumber());
66 67 68 69

        $this->_em->clear();

        $query = $this->_em->createQuery("select e from Doctrine\Tests\ORM\Functional\ChildEntity e");
70
        $entities = $query->getResult();
71 72 73 74 75
        $this->assertEquals(1, count($entities));
        $this->assertTrue($entities[0] instanceof ChildEntity);
        $this->assertTrue(is_numeric($entities[0]->getId()));
        $this->assertEquals('thedata', $entities[0]->getData());
        $this->assertEquals(1234, $entities[0]->getNumber());
76
        $this->assertNull($entities[0]->getParentRelated());
77 78 79 80 81

        $this->_em->clear();

        $query = $this->_em->createQuery("select r,o from Doctrine\Tests\ORM\Functional\RelatedEntity r join r.owner o");

82
        $entities = $query->getResult();
83 84 85 86 87 88 89
        $this->assertEquals(1, count($entities));
        $this->assertTrue($entities[0] instanceof RelatedEntity);
        $this->assertTrue(is_numeric($entities[0]->getId()));
        $this->assertEquals('theRelatedOne', $entities[0]->getName());
        $this->assertTrue($entities[0]->getOwner() instanceof ChildEntity);
        $this->assertEquals('thedata', $entities[0]->getOwner()->getData());
        $this->assertSame($entities[0], $entities[0]->getOwner()->getRelatedEntity());
90
        $this->assertNull($entities[0]->getOwner()->getParentRelated());
91 92 93 94 95

        $query = $this->_em->createQuery("update Doctrine\Tests\ORM\Functional\ChildEntity e set e.data = 'newdata'");

        $affected = $query->execute();
        $this->assertEquals(1, $affected);
96
        
97 98 99
        $query = $this->_em->createQuery("delete Doctrine\Tests\ORM\Functional\ParentEntity e");

        $affected = $query->execute();
romanb's avatar
romanb committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        $this->assertEquals(2, $affected);
        
        $this->_em->clear();
        
        // DQL query with WHERE clause
        $child = new ChildEntity;
        $child->setData('thedata');
        $child->setNumber(1234);

        $this->_em->persist($child);
        $this->_em->flush();
        $this->_em->clear();
        
        $query = $this->_em->createQuery('select e from Doctrine\Tests\ORM\Functional\ParentEntity e where e.id=?1');
        $query->setParameter(1, $child->getId());
        
        $child2 = $query->getSingleResult();
        $this->assertTrue($child2 instanceof ChildEntity);
        $this->assertEquals('thedata', $child2->getData());
        $this->assertEquals(1234, $child2->getNumber());
        $this->assertEquals($child->getId(), $child2->getId());
        $this->assertFalse($child === $child2);
122
        $this->assertNull($child2->getParentRelated());
123
    }
romanb's avatar
romanb committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    
    public function testGetScalarResult()
    {
        $child = new ChildEntity;
        $child->setData('thedata');
        $child->setNumber(1234);

        $this->_em->persist($child);
        $this->_em->flush();
        
        $query = $this->_em->createQuery('select e from Doctrine\Tests\ORM\Functional\ParentEntity e where e.id=?1');
        $query->setParameter(1, $child->getId());
        
        $result = $query->getScalarResult();
        
        $this->assertEquals(1, count($result));
        $this->assertEquals($child->getId(), $result[0]['e_id']);
        $this->assertEquals('thedata', $result[0]['e_data']);
        $this->assertEquals(1234, $result[0]['e_number']);
        $this->assertNull($result[0]['e_related_entity_id']);
        $this->assertEquals('child', $result[0]['e_discr']);
    }
146
    
147
    public function testPolymorphicFindAndQuery()
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    {
        $child = new ChildEntity;
        $child->setData('thedata');
        $child->setNumber(1234);

        $this->_em->persist($child);
        $this->_em->flush();
        
        $this->_em->clear();
        
        $child2 = $this->_em->find('Doctrine\Tests\ORM\Functional\ParentEntity', $child->getId());
        
        $this->assertTrue($child2 instanceof ChildEntity);
        $this->assertEquals('thedata', $child2->getData());
        $this->assertSame(1234, $child2->getNumber());
163 164 165 166 167 168 169
        
        $parentRelated = new ParentRelatedEntity;
        $parentRelated->setData('related to parent!');
        $child2->setParentRelated($parentRelated);
        $parentRelated->setParent($child2);
        $this->_em->persist($parentRelated);
        
170
        //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
171 172 173 174 175 176 177 178 179 180 181 182 183
        
        $this->_em->flush();
        $this->_em->clear();
        
        $query = $this->_em->createQuery("select p, r from Doctrine\Tests\ORM\Functional\ParentEntity p join p.parentRelated r");
        $result = $query->getResult();
        
        $this->assertEquals(1, count($result));
        $this->assertTrue($result[0] instanceof ChildEntity);
        $related = $result[0]->getParentRelated();
        $this->assertFalse($related instanceof \Doctrine\ORM\Proxy\Proxy);
        $this->assertTrue($related instanceof ParentRelatedEntity);
        $this->assertEquals('related to parent!', $related->getData());
184
    }
185
    
186 187 188
}

/**
189
 * @Entity
190
 * @InheritanceType("SINGLE_TABLE")
191
 * @DiscriminatorColumn(name="discr", type="string")
192
 * @DiscriminatorMap({"parent"="ParentEntity", "child"="ChildEntity"})
193 194 195
 */
class ParentEntity {
    /**
196 197
     * @Id
     * @Column(type="integer")
198
     * @GeneratedValue(strategy="AUTO")
199 200 201 202
     */
    private $id;

    /**
203
     * @Column(name="DATA", type="string")
204 205
     */
    private $data;
206 207 208
    
    /** @OneToOne(targetEntity="ParentRelatedEntity", mappedBy="parent") */
    private $parentRelated;
209 210 211 212 213 214 215 216 217 218 219 220

    public function getId() {
        return $this->id;
    }

    public function getData() {
        return $this->data;
    }

    public function setData($data) {
        $this->data = $data;
    }
221 222 223 224 225 226 227 228
    
    public function getParentRelated() {
        return $this->parentRelated;
    }
    
    public function setParentRelated($parentRelated) {
        $this->parentRelated = $parentRelated;
    }
229 230 231
}

/**
232
 * @Entity
233 234 235
 */
class ChildEntity extends ParentEntity {
    /**
236
     * @Column(name="`number`", type="integer", nullable=true)
237 238
     */
    private $number;
239
    /**
240 241
     * @OneToOne(targetEntity="RelatedEntity")
     * @JoinColumn(name="related_entity_id", referencedColumnName="id")
242 243
     */
    private $relatedEntity;
244 245 246 247 248 249 250 251

    public function getNumber() {
        return $this->number;
    }

    public function setNumber($number) {
        $this->number = $number;
    }
252 253 254 255 256 257 258 259 260

    public function getRelatedEntity() {
        return $this->relatedEntity;
    }

    public function setRelatedEntity($relatedEntity) {
        $this->relatedEntity = $relatedEntity;
        $relatedEntity->setOwner($this);
    }
261 262
}

263
/**
264
 * @Entity
265 266 267
 */
class RelatedEntity {
    /**
268 269
     * @Id
     * @Column(type="integer")
270
     * @GeneratedValue(strategy="AUTO")
271 272 273
     */
    private $id;
    /**
274
     * @Column(type="string", length=50)
275 276 277
     */
    private $name;
    /**
278
     * @OneToOne(targetEntity="ChildEntity", mappedBy="relatedEntity")
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
     */
    private $owner;

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getOwner() {
        return $this->owner;
    }

    public function setOwner($owner) {
        $this->owner = $owner;
        if ($owner->getRelatedEntity() !== $this) {
            $owner->setRelatedEntity($this);
        }
    }
}
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

/** @Entity */
class ParentRelatedEntity {
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;
    public function getId() {return $this->id;}
    /** @Column(type="string") */
    private $data;
    public function getData() {return $this->data;}
    public function setData($data) {$this->data = $data;}
    /**
     * @OneToOne(targetEntity="ParentEntity")
     * @JoinColumn(name="parent_id", referencedColumnName="id")
     */
    private $parent;
    public function getParent() {return $this->parent;}
    public function setParent($parent) {$this->parent = $parent;}
}