2013-02-19 22:04:13 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
2013-03-27 00:45:15 +04:00
|
|
|
/**
|
|
|
|
* @group DDC-93
|
|
|
|
*/
|
2013-02-19 22:04:13 +04:00
|
|
|
class ValueObjectsTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|
|
|
{
|
|
|
|
public function setUp()
|
|
|
|
{
|
2013-03-27 00:45:15 +04:00
|
|
|
parent::setUp();
|
|
|
|
|
2013-03-28 00:45:16 +04:00
|
|
|
try {
|
|
|
|
$this->_schemaTool->createSchema(array(
|
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC93Person'),
|
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC93Address'),
|
2013-11-02 16:23:56 +04:00
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC93Vehicle'),
|
|
|
|
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC93Car'),
|
2013-03-28 00:45:16 +04:00
|
|
|
));
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
}
|
2013-03-27 00:45:15 +04:00
|
|
|
}
|
|
|
|
|
2013-03-27 10:44:47 +04:00
|
|
|
public function testCRUD()
|
2013-03-27 00:45:15 +04:00
|
|
|
{
|
|
|
|
$person = new DDC93Person();
|
|
|
|
$person->name = "Tara";
|
|
|
|
$person->address = new DDC93Address();
|
|
|
|
$person->address->street = "United States of Tara Street";
|
|
|
|
$person->address->zip = "12345";
|
|
|
|
$person->address->city = "funkytown";
|
|
|
|
|
2013-03-27 10:44:47 +04:00
|
|
|
// 1. check saving value objects works
|
2013-03-27 00:45:15 +04:00
|
|
|
$this->_em->persist($person);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2013-03-27 10:44:47 +04:00
|
|
|
// 2. check loading value objects works
|
2013-03-27 00:45:15 +04:00
|
|
|
$person = $this->_em->find(DDC93Person::CLASSNAME, $person->id);
|
2013-03-27 03:10:30 +04:00
|
|
|
|
2013-03-27 00:45:15 +04:00
|
|
|
$this->assertInstanceOf(DDC93Address::CLASSNAME, $person->address);
|
2013-03-27 03:10:30 +04:00
|
|
|
$this->assertEquals('United States of Tara Street', $person->address->street);
|
|
|
|
$this->assertEquals('12345', $person->address->zip);
|
|
|
|
$this->assertEquals('funkytown', $person->address->city);
|
2013-03-27 10:44:47 +04:00
|
|
|
|
|
|
|
// 3. check changing value objects works
|
|
|
|
$person->address->street = "Street";
|
|
|
|
$person->address->zip = "54321";
|
|
|
|
$person->address->city = "another town";
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$person = $this->_em->find(DDC93Person::CLASSNAME, $person->id);
|
|
|
|
|
|
|
|
$this->assertEquals('Street', $person->address->street);
|
|
|
|
$this->assertEquals('54321', $person->address->zip);
|
|
|
|
$this->assertEquals('another town', $person->address->city);
|
|
|
|
|
|
|
|
// 4. check deleting works
|
|
|
|
$personId = $person->id;;
|
|
|
|
$this->_em->remove($person);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertNull($this->_em->find(DDC93Person::CLASSNAME, $personId));
|
2013-02-19 22:04:13 +04:00
|
|
|
}
|
2013-03-28 00:45:16 +04:00
|
|
|
|
|
|
|
public function testLoadDql()
|
|
|
|
{
|
|
|
|
for ($i = 0; $i < 3; $i++) {
|
|
|
|
$person = new DDC93Person();
|
|
|
|
$person->name = "Donkey Kong$i";
|
|
|
|
$person->address = new DDC93Address();
|
|
|
|
$person->address->street = "Tree";
|
|
|
|
$person->address->zip = "12345";
|
|
|
|
$person->address->city = "funkytown";
|
|
|
|
|
|
|
|
$this->_em->persist($person);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$dql = "SELECT p FROM " . __NAMESPACE__ . "\DDC93Person p";
|
|
|
|
$persons = $this->_em->createQuery($dql)->getResult();
|
|
|
|
|
|
|
|
$this->assertCount(3, $persons);
|
|
|
|
foreach ($persons as $person) {
|
|
|
|
$this->assertInstanceOf(DDC93Address::CLASSNAME, $person->address);
|
|
|
|
$this->assertEquals('Tree', $person->address->street);
|
|
|
|
$this->assertEquals('12345', $person->address->zip);
|
|
|
|
$this->assertEquals('funkytown', $person->address->city);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dql = "SELECT p FROM " . __NAMESPACE__ . "\DDC93Person p";
|
|
|
|
$persons = $this->_em->createQuery($dql)->getArrayResult();
|
|
|
|
|
|
|
|
foreach ($persons as $person) {
|
|
|
|
$this->assertEquals('Tree', $person['address.street']);
|
|
|
|
$this->assertEquals('12345', $person['address.zip']);
|
|
|
|
$this->assertEquals('funkytown', $person['address.city']);
|
|
|
|
}
|
|
|
|
}
|
2013-11-01 23:38:19 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group dql
|
|
|
|
*/
|
|
|
|
public function testDqlOnEmbeddedObjectsField()
|
|
|
|
{
|
2014-01-04 21:32:06 +04:00
|
|
|
if ($this->isSecondLevelCacheEnabled) {
|
|
|
|
$this->markTestSkipped('SLC does not work with UPDATE/DELETE queries through EM.');
|
|
|
|
}
|
|
|
|
|
2013-11-01 23:38:19 +04:00
|
|
|
$person = new DDC93Person('Johannes', new DDC93Address('Moo', '12345', 'Karlsruhe'));
|
|
|
|
$this->_em->persist($person);
|
|
|
|
$this->_em->flush($person);
|
|
|
|
|
2013-11-01 23:46:08 +04:00
|
|
|
// SELECT
|
|
|
|
$selectDql = "SELECT p FROM " . __NAMESPACE__ ."\\DDC93Person p WHERE p.address.city = :city";
|
|
|
|
$loadedPerson = $this->_em->createQuery($selectDql)
|
2013-11-01 23:38:19 +04:00
|
|
|
->setParameter('city', 'Karlsruhe')
|
|
|
|
->getSingleResult();
|
|
|
|
$this->assertEquals($person, $loadedPerson);
|
|
|
|
|
2013-11-01 23:46:08 +04:00
|
|
|
$this->assertNull($this->_em->createQuery($selectDql)->setParameter('city', 'asdf')->getOneOrNullResult());
|
|
|
|
|
|
|
|
// UPDATE
|
|
|
|
$updateDql = "UPDATE " . __NAMESPACE__ . "\\DDC93Person p SET p.address.street = :street WHERE p.address.city = :city";
|
|
|
|
$this->_em->createQuery($updateDql)
|
|
|
|
->setParameter('street', 'Boo')
|
|
|
|
->setParameter('city', 'Karlsruhe')
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->_em->refresh($person);
|
|
|
|
$this->assertEquals('Boo', $person->address->street);
|
|
|
|
|
|
|
|
// DELETE
|
|
|
|
$this->_em->createQuery("DELETE " . __NAMESPACE__ . "\\DDC93Person p WHERE p.address.city = :city")
|
|
|
|
->setParameter('city', 'Karlsruhe')
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
$this->assertNull($this->_em->find(__NAMESPACE__.'\\DDC93Person', $person->id));
|
2013-11-01 23:38:19 +04:00
|
|
|
}
|
2013-11-01 23:54:45 +04:00
|
|
|
|
|
|
|
public function testDqlWithNonExistentEmbeddableField()
|
|
|
|
{
|
2013-11-02 00:47:56 +04:00
|
|
|
$this->setExpectedException('Doctrine\ORM\Query\QueryException', 'no field or association named address.asdfasdf');
|
|
|
|
|
2013-11-01 23:54:45 +04:00
|
|
|
$this->_em->createQuery("SELECT p FROM " . __NAMESPACE__ . "\\DDC93Person p WHERE p.address.asdfasdf IS NULL")
|
|
|
|
->execute();
|
|
|
|
}
|
2013-11-02 16:23:56 +04:00
|
|
|
|
|
|
|
public function testEmbeddableWithInheritance()
|
|
|
|
{
|
|
|
|
$car = new DDC93Car(new DDC93Address('Foo', '12345', 'Asdf'));
|
|
|
|
$this->_em->persist($car);
|
|
|
|
$this->_em->flush($car);
|
|
|
|
|
|
|
|
$reloadedCar = $this->_em->find(__NAMESPACE__.'\\DDC93Car', $car->id);
|
|
|
|
$this->assertEquals($car, $reloadedCar);
|
|
|
|
}
|
2013-02-19 22:04:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
*/
|
|
|
|
class DDC93Person
|
|
|
|
{
|
2013-03-27 00:45:15 +04:00
|
|
|
const CLASSNAME = __CLASS__;
|
|
|
|
|
2013-02-19 22:04:13 +04:00
|
|
|
/** @Id @GeneratedValue @Column(type="integer") */
|
|
|
|
public $id;
|
|
|
|
|
|
|
|
/** @Column(type="string") */
|
|
|
|
public $name;
|
|
|
|
|
2013-03-27 00:45:15 +04:00
|
|
|
/** @Embedded(class="DDC93Address") */
|
2013-02-19 22:04:13 +04:00
|
|
|
public $address;
|
2013-11-01 23:38:19 +04:00
|
|
|
|
2013-11-02 16:27:17 +04:00
|
|
|
/** @Embedded(class = "DDC93Timestamps") */
|
|
|
|
public $timestamps;
|
|
|
|
|
2013-11-01 23:38:19 +04:00
|
|
|
public function __construct($name = null, DDC93Address $address = null)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
$this->address = $address;
|
2013-11-02 16:27:17 +04:00
|
|
|
$this->timestamps = new DDC93Timestamps(new \DateTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Embeddable
|
|
|
|
*/
|
|
|
|
class DDC93Timestamps
|
|
|
|
{
|
|
|
|
/** @Column(type = "datetime") */
|
|
|
|
public $createdAt;
|
|
|
|
|
|
|
|
public function __construct(\DateTime $createdAt)
|
|
|
|
{
|
|
|
|
$this->createdAt = $createdAt;
|
2013-11-01 23:38:19 +04:00
|
|
|
}
|
2013-02-19 22:04:13 +04:00
|
|
|
}
|
2013-03-27 00:45:15 +04:00
|
|
|
|
2013-11-02 16:23:56 +04:00
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
*
|
|
|
|
* @InheritanceType("SINGLE_TABLE")
|
|
|
|
* @DiscriminatorColumn(name = "t", type = "string", length = 10)
|
|
|
|
* @DiscriminatorMap({
|
|
|
|
* "v" = "Doctrine\Tests\ORM\Functional\DDC93Car",
|
|
|
|
* })
|
|
|
|
*/
|
|
|
|
abstract class DDC93Vehicle
|
|
|
|
{
|
|
|
|
/** @Id @GeneratedValue(strategy = "AUTO") @Column(type = "integer") */
|
|
|
|
public $id;
|
|
|
|
|
|
|
|
/** @Embedded(class = "DDC93Address") */
|
|
|
|
public $address;
|
|
|
|
|
|
|
|
public function __construct(DDC93Address $address)
|
|
|
|
{
|
|
|
|
$this->address = $address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
*/
|
|
|
|
class DDC93Car extends DDC93Vehicle
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-27 00:45:15 +04:00
|
|
|
/**
|
|
|
|
* @Embeddable
|
|
|
|
*/
|
|
|
|
class DDC93Address
|
|
|
|
{
|
|
|
|
const CLASSNAME = __CLASS__;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(type="string")
|
|
|
|
*/
|
|
|
|
public $street;
|
|
|
|
/**
|
|
|
|
* @Column(type="string")
|
|
|
|
*/
|
|
|
|
public $zip;
|
|
|
|
/**
|
|
|
|
* @Column(type="string")
|
|
|
|
*/
|
|
|
|
public $city;
|
2013-11-01 23:38:19 +04:00
|
|
|
|
|
|
|
public function __construct($street = null, $zip = null, $city = null)
|
|
|
|
{
|
|
|
|
$this->street = $street;
|
|
|
|
$this->zip = $zip;
|
|
|
|
$this->city = $city;
|
|
|
|
}
|
2013-03-27 00:45:15 +04:00
|
|
|
}
|
|
|
|
|