1
0
mirror of synced 2025-01-17 22:11:41 +03:00

added xml/yml drivers

This commit is contained in:
Fabio B. Silva 2012-02-02 00:57:39 -02:00
parent 30fdf8dd1b
commit 9e010cbd34
20 changed files with 566 additions and 221 deletions

View File

@ -477,6 +477,11 @@ class ClassMetadataInfo implements ClassMetadata
*/
public $associationMappings = array();
/**
* @var array
*/
public $overrideAssociationMappings = array();
/**
* READ-ONLY: Flag indicating whether the identifier/primary key of the class is composite.
*
@ -1802,6 +1807,49 @@ class ClassMetadataInfo implements ClassMetadata
$this->inheritanceType = $type;
}
/**
* @param string $fieldName
*/
public function setAssociationOverride($fieldName, $overrideMapping)
{
if (!isset($this->associationMappings[$fieldName])) {
throw MappingException::invalidOverrideFieldName($this->name, $fieldName);
}
$mapping = $this->associationMappings[$fieldName];
if (isset($overrideMapping['joinColumns'])) {
$mapping['joinColumns'] = $overrideMapping['joinColumns'];
}
if (isset($overrideMapping['joinTable'])) {
$mapping['joinTable'] = $overrideMapping['joinTable'];
}
$mapping['joinColumnFieldNames'] = null;
$mapping['joinTableColumns'] = null;
$mapping['sourceToTargetKeyColumns'] = null;
$mapping['relationToSourceKeyColumns'] = null;
$mapping['relationToTargetKeyColumns'] = null;
switch ($mapping['type']) {
case self::ONE_TO_ONE:
$mapping = $this->_validateAndCompleteOneToOneMapping($mapping);
break;
case self::ONE_TO_MANY:
$mapping = $this->_validateAndCompleteOneToManyMapping($mapping);
break;
case self::MANY_TO_ONE:
$mapping = $this->_validateAndCompleteOneToOneMapping($mapping);
break;
case self::MANY_TO_MANY:
$mapping = $this->_validateAndCompleteManyToManyMapping($mapping);
break;
}
$this->associationMappings[$fieldName] = $mapping;
}
/**
* Checks whether a mapped field is inherited from an entity superclass.
*

View File

@ -363,9 +363,7 @@ class AnnotationDriver implements Driver
// Check for JoinColummn/JoinColumns annotations
$joinColumns = array();
if (isset($associationOverrides[$property->name]['joinColumns'])) {
$joinColumns = $associationOverrides[$property->name]['joinColumns'];
} else if ($joinColumnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumn')) {
if ($joinColumnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumn')) {
$joinColumns[] = $this->joinColumnToArray($joinColumnAnnot);
} else if ($joinColumnsAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumns')) {
foreach ($joinColumnsAnnot->value as $joinColumn) {
@ -466,9 +464,7 @@ class AnnotationDriver implements Driver
} else if ($manyToManyAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToMany')) {
$joinTable = array();
if (isset($associationOverrides[$property->name]['joinTable'])) {
$joinTable = $associationOverrides[$property->name]['joinTable'];
} else if ($joinTableAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinTable')) {
if ($joinTableAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinTable')) {
$joinTable = array(
'name' => $joinTableAnnot->name,
'schema' => $joinTableAnnot->schema
@ -500,6 +496,47 @@ class AnnotationDriver implements Driver
}
}
// Evaluate AssociationOverrides annotation
if (isset($classAnnotations['Doctrine\ORM\Mapping\AssociationOverrides'])) {
$associationOverridesAnnot = $classAnnotations['Doctrine\ORM\Mapping\AssociationOverrides'];
foreach ($associationOverridesAnnot->value as $associationOverride) {
$override = array();
$fieldName = $associationOverride->name;
// Check for JoinColummn/JoinColumns annotations
if ($associationOverride->joinColumns) {
$joinColumns = array();
foreach ($associationOverride->joinColumns as $joinColumn) {
$joinColumns[] = $this->joinColumnToArray($joinColumn);
}
$override['joinColumns'] = $joinColumns;
}
// Check for JoinTable annotations
if ($associationOverride->joinTable) {
$joinTable = null;
$joinTableAnnot = $associationOverride->joinTable;
$joinTable = array(
'name' => $joinTableAnnot->name,
'schema' => $joinTableAnnot->schema
);
foreach ($joinTableAnnot->joinColumns as $joinColumn) {
$joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumn);
}
foreach ($joinTableAnnot->inverseJoinColumns as $joinColumn) {
$joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumn);
}
$override['joinTable'] = $joinTable;
}
$metadata->setAssociationOverride($fieldName, $override);
}
}
// Evaluate @HasLifecycleCallbacks annotation
if (isset($classAnnotations['Doctrine\ORM\Mapping\HasLifecycleCallbacks'])) {
foreach ($class->getMethods() as $method) {
@ -667,7 +704,7 @@ class AnnotationDriver implements Driver
'referencedColumnName' => $joinColumn->referencedColumnName,
);
}
/**
* Factory method for the Annotation Driver
*

View File

@ -353,10 +353,10 @@ class XmlDriver extends AbstractFileDriver
$joinColumns = array();
if (isset($oneToOneElement->{'join-column'})) {
$joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement->{'join-column'});
$joinColumns[] = $this->joinColumnToArray($oneToOneElement->{'join-column'});
} else if (isset($oneToOneElement->{'join-columns'})) {
foreach ($oneToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
$joinColumns[] = $this->joinColumnToArray($joinColumnElement);
}
}
@ -437,10 +437,10 @@ class XmlDriver extends AbstractFileDriver
$joinColumns = array();
if (isset($manyToOneElement->{'join-column'})) {
$joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement->{'join-column'});
$joinColumns[] = $this->joinColumnToArray($manyToOneElement->{'join-column'});
} else if (isset($manyToOneElement->{'join-columns'})) {
foreach ($manyToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
$joinColumns[] = $this->joinColumnToArray($joinColumnElement);
}
}
@ -487,11 +487,11 @@ class XmlDriver extends AbstractFileDriver
}
foreach ($joinTableElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
$joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
$joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
}
foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) {
$joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
$joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
}
$mapping['joinTable'] = $joinTable;
@ -519,6 +519,50 @@ class XmlDriver extends AbstractFileDriver
}
}
// Evaluate association-overrides
if (isset($xmlRoot->{'association-overrides'})) {
foreach ($xmlRoot->{'association-overrides'}->{'association-override'} as $associationOverrideElement) {
$fieldName = (string) $associationOverrideElement['name'];
$override = array();
// Check for join-columns
if (isset($associationOverrideElement->{'join-columns'})) {
$joinColumns = array();
foreach ($associationOverrideElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
$joinColumns[] = $this->joinColumnToArray($joinColumnElement);
}
$override['joinColumns'] = $joinColumns;
}
// Check for join-table
if ($associationOverrideElement->{'join-table'}) {
$joinTable = null;
$joinTableElement = $associationOverrideElement->{'join-table'};
$joinTable = array(
'name' => (string) $joinTableElement['name'],
'schema' => (string) $joinTableElement['schema']
);
if (isset($joinTableElement->{'join-columns'})) {
foreach ($joinTableElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
$joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
}
}
if (isset($joinTableElement->{'inverse-join-columns'})) {
foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) {
$joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
}
}
$override['joinTable'] = $joinTable;
}
$metadata->setAssociationOverride($fieldName, $override);
}
}
// Evaluate <lifecycle-callbacks...>
if (isset($xmlRoot->{'lifecycle-callbacks'})) {
foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
@ -563,7 +607,7 @@ class XmlDriver extends AbstractFileDriver
* @param $joinColumnElement The XML element.
* @return array The mapping array.
*/
private function _getJoinColumnMapping(SimpleXMLElement $joinColumnElement)
private function joinColumnToArray(SimpleXMLElement $joinColumnElement)
{
$joinColumn = array(
'name' => (string)$joinColumnElement['name'],

View File

@ -356,14 +356,14 @@ class YamlDriver extends AbstractFileDriver
$joinColumns = array();
if (isset($oneToOneElement['joinColumn'])) {
$joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement['joinColumn']);
$joinColumns[] = $this->joinColumnToArray($oneToOneElement['joinColumn']);
} else if (isset($oneToOneElement['joinColumns'])) {
foreach ($oneToOneElement['joinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
$joinColumns[] = $this->joinColumnToArray($joinColumnElement);
}
}
@ -438,14 +438,14 @@ class YamlDriver extends AbstractFileDriver
$joinColumns = array();
if (isset($manyToOneElement['joinColumn'])) {
$joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement['joinColumn']);
$joinColumns[] = $this->joinColumnToArray($manyToOneElement['joinColumn']);
} else if (isset($manyToOneElement['joinColumns'])) {
foreach ($manyToOneElement['joinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
$joinColumns[] = $this->joinColumnToArray($joinColumnElement);
}
}
@ -489,7 +489,7 @@ class YamlDriver extends AbstractFileDriver
$joinColumnElement['name'] = $name;
}
$joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
$joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
}
foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
@ -497,7 +497,7 @@ class YamlDriver extends AbstractFileDriver
$joinColumnElement['name'] = $name;
}
$joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
$joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
}
$mapping['joinTable'] = $joinTable;
@ -527,6 +527,59 @@ class YamlDriver extends AbstractFileDriver
}
}
// Evaluate association-overrides
if (isset($element['associationOverride'])) {
foreach ($element['associationOverride'] as $fieldName => $associationOverride) {
$override = array();
// Check for join-columns
if (isset($associationOverride['joinColumn'])) {
$joinColumns = array();
foreach ($associationOverride['joinColumn'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinColumns[] = $this->joinColumnToArray($joinColumnElement);
}
$override['joinColumns'] = $joinColumns;
}
// Check for join-table
if (isset($associationOverride['joinTable'])) {
$joinTableElement = $associationOverride['joinTable'];
$joinTable = array(
'name' => $joinTableElement['name']
);
if (isset($joinTableElement['schema'])) {
$joinTable['schema'] = $joinTableElement['schema'];
}
foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement);
}
foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
if (!isset($joinColumnElement['name'])) {
$joinColumnElement['name'] = $name;
}
$joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement);
}
$override['joinTable'] = $joinTable;
}
$metadata->setAssociationOverride($fieldName, $override);
}
}
// Evaluate lifeCycleCallbacks
if (isset($element['lifecycleCallbacks'])) {
foreach ($element['lifecycleCallbacks'] as $type => $methods) {
@ -544,7 +597,7 @@ class YamlDriver extends AbstractFileDriver
* @param $joinColumnElement The array join column element
* @return array The mapping array.
*/
private function _getJoinColumnMapping($joinColumnElement)
private function joinColumnToArray($joinColumnElement)
{
$joinColumn = array();
if (isset($joinColumnElement['referencedColumnName'])) {

View File

@ -83,6 +83,17 @@ class MappingException extends \Doctrine\ORM\ORMException
return new self("Invalid mapping file '$fileName' for class '$entityName'.");
}
/**
* Exception for invalid property name override.
*
* @param string $className The entity's name
* @param string $fieldName
*/
public static function invalidOverrideFieldName($className, $fieldName)
{
return new self("Invalid field override named '$fieldName' for class '$className'.");
}
public static function mappingNotFound($className, $fieldName)
{
return new self("No mapping found for field '$fieldName' on class '$className'.");

View File

@ -23,5 +23,25 @@ use Doctrine\Common\Collections\ArrayCollection;
*/
class DDC964Admin extends DDC964User
{
public static function loadMetadata($metadata)
{
$metadata->setAssociationOverride('address',array(
'joinColumns'=>array(array(
'name' => 'adminaddress_id',
'referencedColumnName' => 'id',
))
));
$metadata->setAssociationOverride('groups',array(
'joinTable' => array(
'name' => 'ddc964_users_admingroups',
'joinColumns' => array(array(
'name' => 'adminuser_id',
)),
'inverseJoinColumns' =>array (array (
'name' => 'admingroup_id',
))
)
));
}
}

View File

@ -9,6 +9,8 @@ use Doctrine\Common\Collections\ArrayCollection;
*/
class DDC964Guest extends DDC964User
{
public static function loadMetadata($metadata)
{
}
}

View File

@ -105,4 +105,45 @@ class DDC964User
{
$this->address = $address;
}
public static function loadMetadata($metadata)
{
$metadata->mapField(array(
'id' => true,
'fieldName' => 'id',
'type' => 'integer',
'columnName' => 'id',
));
$metadata->mapField(array(
'fieldName' => 'name',
'type' => 'string',
));
$metadata->mapManyToOne(array(
'fieldName' => 'address',
'targetEntity' => 'DDC964Address',
'cascade' => array('persist','merge'),
'joinColumn' => array('name'=>'address_id', 'referencedColumnMame'=>'id'),
));
$metadata->mapManyToMany(array(
'fieldName' => 'groups',
'targetEntity' => 'DDC964Group',
'inversedBy' => 'users',
'cascade' => array('persist','merge','detach'),
'joinTable' => array(
'name' => 'ddc964_users_groups',
'joinColumns' => array(array(
'name'=>'user_id',
'referencedColumnName'=>'id',
)),
'inverseJoinColumns'=>array(array(
'name'=>'group_id',
'referencedColumnName'=>'id',
))
)
));
$metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadataInfo::GENERATOR_TYPE_AUTO);
}
}

View File

@ -1,193 +0,0 @@
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\ORM\Query;
require_once __DIR__ . '/../../../TestInit.php';
use Doctrine\Tests\Models\DDC964\DDC964Address;
use Doctrine\Tests\Models\DDC964\DDC964Group;
use Doctrine\Tests\Models\DDC964\DDC964Admin;
use Doctrine\Tests\Models\DDC964\DDC964Guest;
use Doctrine\Tests\Models\DDC964\DDC964User;
/**
* @group DDC-964
*/
class DDC964Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
const NS = 'Doctrine\Tests\Models\DDC964';
public function testAssociationOverridesMapping()
{
$adminMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\DDC964\DDC964Admin');
$guestMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\DDC964\DDC964Guest');
// assert groups association mappings
$this->assertArrayHasKey('groups', $guestMetadata->associationMappings);
$this->assertArrayHasKey('groups', $adminMetadata->associationMappings);
$guestGroups = $guestMetadata->associationMappings['groups'];
$adminGroups = $adminMetadata->associationMappings['groups'];
$this->assertEquals('ddc964_users_groups', $guestGroups['joinTable']['name']);
$this->assertEquals('user_id', $guestGroups['joinTable']['joinColumns'][0]['name']);
$this->assertEquals('group_id', $guestGroups['joinTable']['inverseJoinColumns'][0]['name']);
$this->assertEquals(array('user_id'=>'id'), $guestGroups['relationToSourceKeyColumns']);
$this->assertEquals(array('group_id'=>'id'), $guestGroups['relationToTargetKeyColumns']);
$this->assertEquals(array('user_id','group_id'), $guestGroups['joinTableColumns']);
$this->assertEquals('ddc964_users_admingroups', $adminGroups['joinTable']['name']);
$this->assertEquals('adminuser_id', $adminGroups['joinTable']['joinColumns'][0]['name']);
$this->assertEquals('admingroup_id', $adminGroups['joinTable']['inverseJoinColumns'][0]['name']);
$this->assertEquals(array('adminuser_id'=>'id'), $adminGroups['relationToSourceKeyColumns']);
$this->assertEquals(array('admingroup_id'=>'id'), $adminGroups['relationToTargetKeyColumns']);
$this->assertEquals(array('adminuser_id','admingroup_id'), $adminGroups['joinTableColumns']);
// assert address association mappings
$this->assertArrayHasKey('address', $guestMetadata->associationMappings);
$this->assertArrayHasKey('address', $adminMetadata->associationMappings);
$guestAddress = $guestMetadata->associationMappings['address'];
$adminAddress = $adminMetadata->associationMappings['address'];
$this->assertEquals('address_id', $guestAddress['joinColumns'][0]['name']);
$this->assertEquals(array('address_id'=>'id'), $guestAddress['sourceToTargetKeyColumns']);
$this->assertEquals(array('address_id'=>'address_id'), $guestAddress['joinColumnFieldNames']);
$this->assertEquals(array('id'=>'address_id'), $guestAddress['targetToSourceKeyColumns']);
$this->assertEquals('adminaddress_id', $adminAddress['joinColumns'][0]['name']);
$this->assertEquals(array('adminaddress_id'=>'id'), $adminAddress['sourceToTargetKeyColumns']);
$this->assertEquals(array('adminaddress_id'=>'adminaddress_id'), $adminAddress['joinColumnFieldNames']);
$this->assertEquals(array('id'=>'adminaddress_id'), $adminAddress['targetToSourceKeyColumns']);
}
public function testShouldCreateAndRetrieveOverriddenAssociation()
{
list($admin1,$admin2, $guest1,$guest2) = $this->loadFixtures();
$this->_em->clear();
$this->assertNotNull($admin1->getId());
$this->assertNotNull($admin2->getId());
$this->assertNotNull($guest1->getId());
$this->assertNotNull($guest1->getId());
$adminCount = $this->_em
->createQuery('SELECT COUNT(a) FROM ' . self::NS . '\DDC964Admin a')
->getSingleScalarResult();
$guestCount = $this->_em
->createQuery('SELECT COUNT(g) FROM ' . self::NS . '\DDC964Guest g')
->getSingleScalarResult();
$this->assertEquals(2, $adminCount);
$this->assertEquals(2, $guestCount);
$admin1 = $this->_em->find(self::NS . '\DDC964Admin', $admin1->getId());
$admin2 = $this->_em->find(self::NS . '\DDC964Admin', $admin2->getId());
$guest1 = $this->_em->find(self::NS . '\DDC964Guest', $guest1->getId());
$guest2 = $this->_em->find(self::NS . '\DDC964Guest', $guest2->getId());
$this->assertUser($admin1, self::NS . '\DDC964Admin', '11111-111', 2);
$this->assertUser($admin2, self::NS . '\DDC964Admin', '22222-222', 2);
$this->assertUser($guest1, self::NS . '\DDC964Guest', '33333-333', 2);
$this->assertUser($guest2, self::NS . '\DDC964Guest', '44444-444', 1);
}
/**
* @param DDC964User $user
* @param string $addressZip
* @param integer $groups
*/
private function assertUser(DDC964User $user, $className, $addressZip, $groups)
{
$this->assertInstanceOf($className, $user);
$this->assertInstanceOf(self::NS . '\DDC964User', $user);
$this->assertInstanceOf(self::NS . '\DDC964Address', $user->getAddress());
$this->assertEquals($addressZip, $user->getAddress()->getZip());
$this->assertEquals($groups, $user->getGroups()->count());
}
private function createSchemaDDC964()
{
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(self::NS . '\DDC964Address'),
$this->_em->getClassMetadata(self::NS . '\DDC964Group'),
$this->_em->getClassMetadata(self::NS . '\DDC964Guest'),
$this->_em->getClassMetadata(self::NS . '\DDC964Admin'),
));
} catch (\Exception $exc) {
}
}
/**
* @return array
*/
private function loadFixtures()
{
$this->createSchemaDDC964();
$group1 = new DDC964Group('Foo Admin Group');
$group2 = new DDC964Group('Bar Admin Group');
$group3 = new DDC964Group('Foo Guest Group');
$group4 = new DDC964Group('Bar Guest Group');
$this->_em->persist($group1);
$this->_em->persist($group2);
$this->_em->persist($group3);
$this->_em->persist($group4);
$this->_em->flush();
$admin1 = new DDC964Admin('Admin 1');
$admin2 = new DDC964Admin('Admin 2');
$guest1 = new DDC964Guest('Guest 1');
$guest2 = new DDC964Guest('Guest 2');
$admin1->setAddress(new DDC964Address('11111-111', 'Some Country', 'Some City', 'Some Street'));
$admin2->setAddress(new DDC964Address('22222-222', 'Some Country', 'Some City', 'Some Street'));
$guest1->setAddress(new DDC964Address('33333-333', 'Some Country', 'Some City', 'Some Street'));
$guest2->setAddress(new DDC964Address('44444-444', 'Some Country', 'Some City', 'Some Street'));
$admin1->addGroup($group1);
$admin1->addGroup($group2);
$admin2->addGroup($group1);
$admin2->addGroup($group2);
$guest1->addGroup($group3);
$guest1->addGroup($group4);
$guest2->addGroup($group2);
$this->_em->persist($admin1);
$this->_em->persist($admin2);
$this->_em->persist($guest1);
$this->_em->persist($guest2);
$this->_em->flush();
return array($admin1,$admin2, $guest1,$guest2);
}
}

View File

@ -376,11 +376,8 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
*/
public function testDefaultFieldType()
{
$em = $this->_getTestEntityManager();
$factory = $this->createClassMetadataFactory($em);
$class = $factory->getMetadataFor('Doctrine\Tests\Models\DDC1476\DDC1476EntityWithDefaultFieldType');
$factory = $this->createClassMetadataFactory();
$class = $factory->getMetadataFor('Doctrine\Tests\Models\DDC1476\DDC1476EntityWithDefaultFieldType');
$this->assertArrayHasKey('id', $class->fieldMappings);
@ -417,7 +414,6 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
*/
public function testIdentifierColumnDefinition()
{
$class = $this->createClassMetadata(__NAMESPACE__ . '\DDC1170Entity');
@ -491,6 +487,7 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
}
/**
<<<<<<< HEAD
* @group DDC-1663
*/
public function testNamedNativeQuery()
@ -613,6 +610,89 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals(array('name'=>'name','column'=>'name'), $mapping['entities'][0]['fields'][1]);
$this->assertEquals($personMetadata->name, $mapping['entities'][0]['entityClass']);
}
/*
* @group DDC-964
*/
public function testAssociationOverridesMapping()
{
$factory = $this->createClassMetadataFactory();
$adminMetadata = $factory->getMetadataFor('Doctrine\Tests\Models\DDC964\DDC964Admin');
$guestMetadata = $factory->getMetadataFor('Doctrine\Tests\Models\DDC964\DDC964Guest');
// assert groups association mappings
$this->assertArrayHasKey('groups', $guestMetadata->associationMappings);
$this->assertArrayHasKey('groups', $adminMetadata->associationMappings);
$guestGroups = $guestMetadata->associationMappings['groups'];
$adminGroups = $adminMetadata->associationMappings['groups'];
// assert not override attributes
$this->assertEquals($guestGroups['fieldName'], $adminGroups['fieldName']);
$this->assertEquals($guestGroups['type'], $adminGroups['type']);
$this->assertEquals($guestGroups['mappedBy'], $adminGroups['mappedBy']);
$this->assertEquals($guestGroups['inversedBy'], $adminGroups['inversedBy']);
$this->assertEquals($guestGroups['isOwningSide'], $adminGroups['isOwningSide']);
$this->assertEquals($guestGroups['fetch'], $adminGroups['fetch']);
$this->assertEquals($guestGroups['isCascadeRemove'], $adminGroups['isCascadeRemove']);
$this->assertEquals($guestGroups['isCascadePersist'], $adminGroups['isCascadePersist']);
$this->assertEquals($guestGroups['isCascadeRefresh'], $adminGroups['isCascadeRefresh']);
$this->assertEquals($guestGroups['isCascadeMerge'], $adminGroups['isCascadeMerge']);
$this->assertEquals($guestGroups['isCascadeDetach'], $adminGroups['isCascadeDetach']);
// assert not override attributes
$this->assertEquals('ddc964_users_groups', $guestGroups['joinTable']['name']);
$this->assertEquals('user_id', $guestGroups['joinTable']['joinColumns'][0]['name']);
$this->assertEquals('group_id', $guestGroups['joinTable']['inverseJoinColumns'][0]['name']);
$this->assertEquals(array('user_id'=>'id'), $guestGroups['relationToSourceKeyColumns']);
$this->assertEquals(array('group_id'=>'id'), $guestGroups['relationToTargetKeyColumns']);
$this->assertEquals(array('user_id','group_id'), $guestGroups['joinTableColumns']);
$this->assertEquals('ddc964_users_admingroups', $adminGroups['joinTable']['name']);
$this->assertEquals('adminuser_id', $adminGroups['joinTable']['joinColumns'][0]['name']);
$this->assertEquals('admingroup_id', $adminGroups['joinTable']['inverseJoinColumns'][0]['name']);
$this->assertEquals(array('adminuser_id'=>'id'), $adminGroups['relationToSourceKeyColumns']);
$this->assertEquals(array('admingroup_id'=>'id'), $adminGroups['relationToTargetKeyColumns']);
$this->assertEquals(array('adminuser_id','admingroup_id'), $adminGroups['joinTableColumns']);
// assert address association mappings
$this->assertArrayHasKey('address', $guestMetadata->associationMappings);
$this->assertArrayHasKey('address', $adminMetadata->associationMappings);
$guestAddress = $guestMetadata->associationMappings['address'];
$adminAddress = $adminMetadata->associationMappings['address'];
// assert not override attributes
$this->assertEquals($guestAddress['fieldName'], $adminAddress['fieldName']);
$this->assertEquals($guestAddress['type'], $adminAddress['type']);
$this->assertEquals($guestAddress['mappedBy'], $adminAddress['mappedBy']);
$this->assertEquals($guestAddress['inversedBy'], $adminAddress['inversedBy']);
$this->assertEquals($guestAddress['isOwningSide'], $adminAddress['isOwningSide']);
$this->assertEquals($guestAddress['fetch'], $adminAddress['fetch']);
$this->assertEquals($guestAddress['isCascadeRemove'], $adminAddress['isCascadeRemove']);
$this->assertEquals($guestAddress['isCascadePersist'], $adminAddress['isCascadePersist']);
$this->assertEquals($guestAddress['isCascadeRefresh'], $adminAddress['isCascadeRefresh']);
$this->assertEquals($guestAddress['isCascadeMerge'], $adminAddress['isCascadeMerge']);
$this->assertEquals($guestAddress['isCascadeDetach'], $adminAddress['isCascadeDetach']);
// assert override
$this->assertEquals('address_id', $guestAddress['joinColumns'][0]['name']);
$this->assertEquals(array('address_id'=>'id'), $guestAddress['sourceToTargetKeyColumns']);
$this->assertEquals(array('address_id'=>'address_id'), $guestAddress['joinColumnFieldNames']);
$this->assertEquals(array('id'=>'address_id'), $guestAddress['targetToSourceKeyColumns']);
$this->assertEquals('adminaddress_id', $adminAddress['joinColumns'][0]['name']);
$this->assertEquals(array('adminaddress_id'=>'id'), $adminAddress['sourceToTargetKeyColumns']);
$this->assertEquals(array('adminaddress_id'=>'adminaddress_id'), $adminAddress['joinColumnFieldNames']);
$this->assertEquals(array('id'=>'adminaddress_id'), $adminAddress['targetToSourceKeyColumns']);
}
}
/**

View File

@ -932,6 +932,20 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
$this->setExpectedException("Doctrine\ORM\Mapping\MappingException", "Invalid cascade option(s) specified: 'invalid'. Only 'remove', 'persist', 'refresh', 'merge' and 'detach' are allowed.");
$cm->mapManyToOne(array('fieldName' => 'address', 'targetEntity' => 'UnknownClass', 'cascade' => array('invalid')));
}
/**
* @group DDC-964
* @expectedException Doctrine\ORM\Mapping\MappingException
* @expectedExceptionMessage Invalid field override named 'invalidPropertyName' for class 'Doctrine\Tests\Models\DDC964\DDC964Admin
*/
public function testInvalidPropertyOverrideNameException()
{
$cm = new ClassMetadata('Doctrine\Tests\Models\DDC964\DDC964Admin');
$cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
$cm->mapManyToOne(array('fieldName' => 'address', 'targetEntity' => 'DDC964Address'));
$cm->setAssociationOverride('invalidPropertyName', array());
}
}

View File

@ -0,0 +1,21 @@
<?php
$metadata->setAssociationOverride('address',array(
'joinColumns'=>array(array(
'name' => 'adminaddress_id',
'referencedColumnName' => 'id',
))
));
$metadata->setAssociationOverride('groups',array(
'joinTable' => array (
'name' => 'ddc964_users_admingroups',
'joinColumns' => array(array(
'name' => 'adminuser_id',
)),
'inverseJoinColumns' =>array (array (
'name' => 'admingroup_id',
))
)
));

View File

@ -0,0 +1,41 @@
<?php
use Doctrine\ORM\Mapping\ClassMetadataInfo;
$metadata->mapField(array(
'id' => true,
'fieldName' => 'id',
'type' => 'integer',
'columnName' => 'id',
));
$metadata->mapField(array(
'fieldName' => 'name',
'type' => 'string',
));
$metadata->mapManyToOne(array(
'fieldName' => 'address',
'targetEntity' => 'DDC964Address',
'cascade' => array('persist','merge'),
'joinColumn' => array('name'=>'address_id', 'referencedColumnMame'=>'id'),
));
$metadata->mapManyToMany(array(
'fieldName' => 'groups',
'targetEntity' => 'DDC964Group',
'inversedBy' => 'users',
'cascade' => array('persist','merge','detach'),
'joinTable' => array(
'name' => 'ddc964_users_groups',
'joinColumns' => array(array(
'name'=>'user_id',
'referencedColumnName'=>'id',
)),
'inverseJoinColumns'=>array(array(
'name'=>'group_id',
'referencedColumnName'=>'id',
))
)
));
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\Models\DDC964\DDC964Admin">
<association-overrides>
<association-override name="groups">
<join-table name="ddc964_users_admingroups">
<join-columns>
<join-column name="adminuser_id"/>
</join-columns>
<inverse-join-columns>
<join-column name="admingroup_id"/>
</inverse-join-columns>
</join-table>
</association-override>
<association-override name="address">
<join-columns>
<join-column name="adminaddress_id" referenced-column-name="id"/>
</join-columns>
</association-override>
</association-overrides>
</entity>
</doctrine-mapping>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\Models\DDC964\DDC964Guest">
</entity>
</doctrine-mapping>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<mapped-superclass name="Doctrine\Tests\Models\DDC964\DDC964User">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="name" column="name" type="string"/>
<many-to-one field="address" target-entity="DDC964Address">
<cascade>
<cascade-persist/>
<cascade-merge/>
</cascade>
<join-column name="address_id" referenced-column-name="id"/>
</many-to-one>
<many-to-many field="groups" target-entity="DDC964Group" inversed-by="users">
<cascade>
<cascade-persist/>
<cascade-merge/>
<cascade-detach/>
</cascade>
<join-table name="ddc964_users_groups">
<join-columns>
<join-column name="user_id" referenced-column-name="id" />
</join-columns>
<inverse-join-columns>
<join-column name="group_id" referenced-column-name="id" />
</inverse-join-columns>
</join-table>
</many-to-many>
</mapped-superclass>
</doctrine-mapping>

View File

@ -0,0 +1,17 @@
Doctrine\Tests\Models\DDC964\DDC964Admin:
type: entity
associationOverride:
address:
joinColumn:
adminaddress_id:
name: adminaddress_id
referencedColumnName: id
groups:
joinTable:
name: ddc964_users_admingroups
joinColumns:
adminuser_id:
referencedColumnName: id
inverseJoinColumns:
admingroup_id:
referencedColumnName: id

View File

@ -0,0 +1,2 @@
Doctrine\Tests\Models\DDC964\DDC964Guest:
type: entity

View File

@ -0,0 +1,30 @@
Doctrine\Tests\Models\DDC964\DDC964User:
type: mappedSuperclass
id:
id:
type: integer
unsigned: true
generator:
strategy: AUTO
fields:
name:
type: string
manyToOne:
address:
targetEntity: DDC964Address
joinColumn:
name: address_id
referencedColumnName: id
cascade: [ persist, merge ]
manyToMany:
groups:
targetEntity: DDC964Group
joinTable:
name: ddc964_users_groups
joinColumns:
user_id:
referencedColumnName: id
inverseJoinColumns:
group_id:
referencedColumnName: id
cascade: [ persist, merge, detach ]