Commit d90e71d0 authored by romanb's avatar romanb

[2.0] First implementation of XmlDriver + tests. First draft of XSD document.

parent de7dfb4f
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.doctrine-project.org/orm/doctrine-mapping"
xmlns:orm="http://schemas.doctrine-project.org/orm/doctrine-mapping"
elementFormDefault="qualified">
<xs:annotation>
<xs:documentation><![CDATA[
This is the XML Schema for the object/relational
mapping file used by Doctrine.
]]></xs:documentation>
</xs:annotation>
<xs:element name="doctrine-mapping">
<xs:complexType>
<xs:sequence>
<xs:element name="entity" type="orm:entity" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="emptyType"/>
<xs:complexType name="cascade-type">
<xs:sequence>
<xs:element name="cascade-all" type="orm:emptyType" minOccurs="0"/>
<xs:element name="cascade-save" type="orm:emptyType" minOccurs="0"/>
<xs:element name="cascade-merge" type="orm:emptyType" minOccurs="0"/>
<xs:element name="cascade-delete" type="orm:emptyType" minOccurs="0"/>
<xs:element name="cascade-refresh" type="orm:emptyType" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="entity">
<xs:sequence>
<xs:element name="id" type="orm:id" />
<xs:element name="field" type="orm:field" minOccurs="0" />
<xs:element name="one-to-one" type="orm:one-to-one" minOccurs="0"/>
<xs:element name="one-to-many" type="orm:one-to-many" minOccurs="0" />
<xs:element name="many-to-many" type="orm:many-to-many" minOccurs="0" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="table" type="xs:NMTOKEN" />
<xs:attribute name="schema" type="xs:NMTOKEN" />
<xs:attribute name="inheritance-type" type="orm:inheritance-type"/>
</xs:complexType>
<xs:complexType name="mapped-superclass">
<xs:sequence>
<xs:element name="id" type="orm:id" minOccurs="0" />
<xs:element name="field" type="orm:field" minOccurs="0" />
<xs:element name="one-to-many" type="orm:one-to-many" minOccurs="0" />
<xs:element name="many-to-many" type="orm:many-to-many" minOccurs="0" />
</xs:sequence>
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
</xs:complexType>
<xs:simpleType name="inheritance-type">
<xs:restriction base="xs:string">
<xs:enumeration value="single-table"/>
<xs:enumeration value="joined"/>
<xs:enumeration value="table-per-class"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="field">
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
<xs:attribute name="type" type="xs:NMTOKEN" use="required" />
<xs:attribute name="column" type="xs:NMTOKEN" />
<xs:attribute name="length" type="xs:NMTOKEN" />
</xs:complexType>
<xs:complexType name="generator">
<xs:attribute name="strategy" type="xs:NMTOKEN" use="required" />
</xs:complexType>
<xs:complexType name="id">
<xs:sequence>
<xs:element name="generator" type="orm:generator" minOccurs="0" />
</xs:sequence>
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
<xs:attribute name="type" type="xs:NMTOKEN" use="required" />
<xs:attribute name="column" type="xs:NMTOKEN" />
</xs:complexType>
<xs:complexType name="inverse-join-columns">
<xs:sequence>
<xs:element name="join-column" type="orm:join-column" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="join-column">
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
<xs:attribute name="referencedColumnName" type="xs:NMTOKEN" use="required" />
</xs:complexType>
<xs:complexType name="join-columns">
<xs:sequence>
<xs:element name="join-column" type="orm:join-column" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="join-table">
<xs:sequence>
<xs:element name="join-columns" type="orm:join-columns" />
<xs:element name="inverse-join-columns" type="orm:join-columns" />
</xs:sequence>
<xs:attribute name="name" type="xs:NMTOKEN" use="required" />
</xs:complexType>
<xs:complexType name="many-to-many">
<xs:sequence>
<xs:element name="cascade" type="orm:cascade-type" minOccurs="0" />
<xs:element name="join-table" type="orm:join-table" />
</xs:sequence>
<xs:attribute name="targetEntity" type="xs:NMTOKEN" use="required" />
<xs:attribute name="field" type="xs:NMTOKEN" use="required" />
</xs:complexType>
<xs:complexType name="one-to-many">
<xs:sequence>
<xs:element name="cascade" type="orm:cascade-type" minOccurs="0" />
</xs:sequence>
<xs:attribute name="targetEntity" type="xs:NMTOKEN" use="required" />
<xs:attribute name="mappedBy" type="xs:NMTOKEN" />
<xs:attribute name="field" type="xs:NMTOKEN" use="required" />
</xs:complexType>
<xs:complexType name="one-to-one">
<xs:sequence>
<xs:element name="cascade" type="orm:cascade-type" minOccurs="0" />
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="join-column" type="orm:join-column"/>
<xs:element name="join-columns" type="orm:join-columns"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="targetEntity" type="xs:NMTOKEN" use="required" />
<xs:attribute name="mappedBy" type="xs:NMTOKEN" />
<xs:attribute name="field" type="xs:NMTOKEN" use="required" />
</xs:complexType>
</xs:schema>
\ No newline at end of file
...@@ -59,7 +59,7 @@ class ClassLoader ...@@ -59,7 +59,7 @@ class ClassLoader
} }
/** /**
* Set namespace separator * Sets the namespace separator to use.
* *
* @param string $separator * @param string $separator
* @return void * @return void
......
...@@ -66,12 +66,10 @@ class ClassMetadataFactory ...@@ -66,12 +66,10 @@ class ClassMetadataFactory
public function setCacheDriver($cacheDriver) public function setCacheDriver($cacheDriver)
{ {
$this->_cacheDriver = $cacheDriver; $this->_cacheDriver = $cacheDriver;
/*
foreach ($this->_driver->preload() as $className) { foreach ($this->_driver->preload() as $className) {
$cacheKey = "$className\$CLASSMETADATA"; $cacheKey = "$className\$CLASSMETADATA";
$this->_cacheDriver->save($cacheKey, $this->getMetadataFor($className), null); $this->_cacheDriver->save($cacheKey, $this->getMetadataFor($className), null);
} }
*/
} }
/** /**
......
...@@ -61,12 +61,12 @@ class AnnotationDriver ...@@ -61,12 +61,12 @@ class AnnotationDriver
)); ));
} }
// Evaluate DoctrineInheritanceType annotation // Evaluate InheritanceType annotation
if ($inheritanceTypeAnnot = $annotClass->getAnnotation('InheritanceType')) { if ($inheritanceTypeAnnot = $annotClass->getAnnotation('InheritanceType')) {
$metadata->setInheritanceType($inheritanceTypeAnnot->value); $metadata->setInheritanceType($inheritanceTypeAnnot->value);
} }
// Evaluate DoctrineDiscriminatorColumn annotation // Evaluate DiscriminatorColumn annotation
if ($discrColumnAnnot = $annotClass->getAnnotation('DiscriminatorColumn')) { if ($discrColumnAnnot = $annotClass->getAnnotation('DiscriminatorColumn')) {
$metadata->setDiscriminatorColumn(array( $metadata->setDiscriminatorColumn(array(
'name' => $discrColumnAnnot->name, 'name' => $discrColumnAnnot->name,
...@@ -75,7 +75,7 @@ class AnnotationDriver ...@@ -75,7 +75,7 @@ class AnnotationDriver
)); ));
} }
// Evaluate DoctrineDiscriminatorMap annotation // Evaluate DiscriminatorValue annotation
if ($discrValueAnnot = $annotClass->getAnnotation('DiscriminatorValue')) { if ($discrValueAnnot = $annotClass->getAnnotation('DiscriminatorValue')) {
$metadata->setDiscriminatorValue($discrValueAnnot->value); $metadata->setDiscriminatorValue($discrValueAnnot->value);
} }
......
This diff is collapsed.
...@@ -89,7 +89,7 @@ class OneToOneMapping extends AssociationMapping ...@@ -89,7 +89,7 @@ class OneToOneMapping extends AssociationMapping
if ($this->isOwningSide()) { if ($this->isOwningSide()) {
if ( ! isset($mapping['joinColumns'])) { if ( ! isset($mapping['joinColumns'])) {
throw MappingException::invalidMapping($this->_sourceFieldName); throw MappingException::invalidMapping($this->sourceFieldName);
} }
$this->joinColumns = $mapping['joinColumns']; $this->joinColumns = $mapping['joinColumns'];
foreach ($mapping['joinColumns'] as $joinColumn) { foreach ($mapping['joinColumns'] as $joinColumn) {
......
...@@ -20,6 +20,7 @@ class AllTests ...@@ -20,6 +20,7 @@ class AllTests
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Mapping'); $suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Mapping');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\XmlDriverTest');
//$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest'); //$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest');
return $suite; return $suite;
......
<?php
namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
require_once __DIR__ . '/xml/User.php';
require_once __DIR__ . '/../../TestInit.php';
class XmlDriverTest extends \Doctrine\Tests\OrmTestCase
{
public function testFilePerClassMapping()
{
$className = 'XmlMappingTest\User';
$xmlDriver = new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml', XmlDriver::FILE_PER_CLASS);
$class = new ClassMetadata($className);
$this->assertFalse($xmlDriver->isTransient($className));
$xmlDriver->loadMetadataForClass($className, $class);
$this->assertEquals('cms_users', $class->getTableName());
$this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $class->getInheritanceType());
$this->assertEquals(2, count($class->fieldMappings));
$this->assertTrue(isset($class->fieldMappings['id']));
$this->assertTrue(isset($class->fieldMappings['name']));
$this->assertEquals('string', $class->fieldMappings['name']['type']);
$this->assertEquals(array('id'), $class->identifier);
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $class->getIdGeneratorType());
$this->assertEquals(3, count($class->associationMappings));
$this->assertEquals(1, count($class->inverseMappings));
$this->assertTrue($class->associationMappings['address'] instanceof \Doctrine\ORM\Mapping\OneToOneMapping);
$this->assertTrue(isset($class->associationMappings['address']));
$this->assertTrue($class->associationMappings['address']->isOwningSide);
$this->assertTrue($class->associationMappings['phonenumbers'] instanceof \Doctrine\ORM\Mapping\OneToManyMapping);
$this->assertTrue(isset($class->associationMappings['phonenumbers']));
$this->assertFalse($class->associationMappings['phonenumbers']->isOwningSide);
$this->assertTrue($class->associationMappings['phonenumbers']->isInverseSide());
$this->assertTrue($class->associationMappings['phonenumbers']->isCascadeSave);
$this->assertTrue($class->associationMappings['groups'] instanceof \Doctrine\ORM\Mapping\ManyToManyMapping);
$this->assertTrue(isset($class->associationMappings['groups']));
$this->assertTrue($class->associationMappings['groups']->isOwningSide);
}
public function testPreloadMode()
{
$className = 'XmlMappingTest\User';
$xmlDriver = new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml');
$class = new ClassMetadata($className);
$classNames = $xmlDriver->preload();
$this->assertEquals($className, $classNames[0]);
$this->assertEquals(1, count($xmlDriver->getPreloadedXmlElements()));
$xmlDriver->loadMetadataForClass($className, $class);
$this->assertEquals(0, count($xmlDriver->getPreloadedXmlElements()));
}
}
\ No newline at end of file
<?php
namespace XmlMappingTest;
class User {
private $id;
private $name;
private $address;
private $phonenumbers;
private $groups;
// ... rest of code omitted, irrelevant for the mapping test
}
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://schemas.doctrine-project.org/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.doctrine-project.org/orm/doctrine-mapping
/Users/robo/dev/php/Doctrine/doctrine-mapping.xsd">
<entity name="XmlMappingTest\User" table="cms_users">
<id name="id" type="integer" column="id">
<generator strategy="auto"/>
</id>
<field name="name" column="name" type="string" length="50"/>
<one-to-one field="address" targetEntity="Address">
<join-column name="address_id" referencedColumnName="id"/>
</one-to-one>
<one-to-many field="phonenumbers" targetEntity="Phonenumber" mappedBy="user">
<cascade>
<cascade-save/>
</cascade>
</one-to-many>
<many-to-many field="groups" targetEntity="Group">
<join-table name="cms_users_groups">
<join-columns>
<join-column name="user_id" referencedColumnName="id"/>
</join-columns>
<inverse-join-columns>
<join-column name="group_id" referencedColumnName="id"/>
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
</doctrine-mapping>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment