diff --git a/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php b/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php index c4bde7bb7..5dd363698 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php @@ -26,7 +26,8 @@ use Doctrine\Common\Cache\ArrayCache, Doctrine\DBAL\Schema\AbstractSchemaManager, Doctrine\ORM\Mapping\ClassMetadataInfo, Doctrine\ORM\Mapping\MappingException, - Doctrine\Common\Util\Inflector; + Doctrine\Common\Util\Inflector, + Doctrine\ORM\Mapping\Driver\AbstractFileDriver; /** * The PhpDriver includes php files which just populate ClassMetadataInfo @@ -41,126 +42,30 @@ use Doctrine\Common\Cache\ArrayCache, * @author Jonathan H. Wage * @author Roman Borschel */ -class PhpDriver implements Driver +class PhpDriver extends AbstractFileDriver { - /** - * @var array The array of class names found and the path to the file - */ - private $_classPaths = array(); - /** - * The paths where to look for mapping files. - * - * @var array - */ - protected $_paths = array(); - - /** - * The file extension of mapping documents. - * - * @var string + * {@inheritdoc} */ protected $_fileExtension = '.php'; - - /** - * Initializes a new PhpDriver that looks in the given path(s) for mapping - * documents and operates in the specified operating mode. - * - * @param string|array $paths One or multiple paths where mapping documents can be found. - */ - public function __construct($paths) - { - $this->addPaths((array) $paths); - } - /** - * Append lookup paths to metadata driver. - * - * @param array $paths - */ - public function addPaths(array $paths) - { - $this->_paths = array_unique(array_merge($this->_paths, $paths)); - } - - /** - * Retrieve the defined metadata lookup paths. - * - * @return array - */ - public function getPaths() - { - return $this->_paths; - } - - /** - * Get the file extension used to look for mapping files under - * - * @return void - */ - public function getFileExtension() - { - return $this->_fileExtension; - } - - /** - * Set the file extension used to look for mapping files under - * - * @param string $fileExtension The file extension to set - * @return void - */ - public function setFileExtension($fileExtension) - { - $this->_fileExtension = $fileExtension; - } + protected $_metadata; /** * {@inheritdoc} */ public function loadMetadataForClass($className, ClassMetadataInfo $metadata) { - $path = $this->_classPaths[$className]; - - require_once $path; + $this->_metadata = $metadata; + $this->_loadMappingFile($this->_findMappingFile($className)); } /** * {@inheritdoc} */ - public function isTransient($className) + protected function _loadMappingFile($file) { - return true; - } - - /** - * {@inheritDoc} - */ - public function getAllClassNames() - { - $classes = array(); - - if ($this->_paths) { - foreach ((array) $this->_paths as $path) { - if ( ! is_dir($path)) { - throw MappingException::phpDriverRequiresConfiguredDirectoryPath(); - } - - $iterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($path), - \RecursiveIteratorIterator::LEAVES_ONLY - ); - - foreach ($iterator as $file) { - if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) { - continue; - } - - $classes[] = $fileName; - $this->_classPaths[$fileName] = $file->getPathName(); - } - } - } - - return $classes; + $metadata = $this->_metadata; + require_once $file; } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php index 8dff41be3..acd8e80b6 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php @@ -80,6 +80,14 @@ class PhpExporter extends AbstractExporter $lines[] = '$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_' . $this->_getChangeTrackingPolicyString($metadata->changeTrackingPolicy) . ');'; } + if ($metadata->lifecycleCallbacks) { + foreach ($metadata->lifecycleCallbacks as $event => $callbacks) { + foreach ($callbacks as $callback) { + $lines[] = "\$metadata->addLifecycleCallback('$callback', '$event');"; + } + } + } + foreach ($metadata->fieldMappings as $fieldMapping) { $lines[] = '$metadata->mapField(' . $this->_varExport($fieldMapping) . ');'; } @@ -89,16 +97,16 @@ class PhpExporter extends AbstractExporter } foreach ($metadata->associationMappings as $associationMapping) { + $cascade = array('remove', 'persist', 'refresh', 'merge', 'detach'); + foreach ($cascade as $key => $value) { + if ( ! $associationMapping->{'isCascade'.ucfirst($value)}) { + unset($cascade[$key]); + } + } $associationMappingArray = array( 'fieldName' => $associationMapping->sourceFieldName, 'targetEntity' => $associationMapping->targetEntityName, - 'cascade' => array( - 'remove' => $associationMapping->isCascadeRemove, - 'persist' => $associationMapping->isCascadePersist, - 'refresh' => $associationMapping->isCascadeRefresh, - 'merge' => $associationMapping->isCascadeMerge, - 'detach' => $associationMapping->isCascadeDetach, - ), + 'cascade' => $cascade, ); if ($associationMapping instanceof \Doctrine\ORM\Mapping\OneToOneMapping) { @@ -115,6 +123,7 @@ class PhpExporter extends AbstractExporter $oneToManyMappingArray = array( 'mappedBy' => $associationMapping->mappedBy, 'orphanRemoval' => $associationMapping->orphanRemoval, + 'orderBy' => $associationMapping->orderBy ); $associationMappingArray = array_merge($associationMappingArray, $oneToManyMappingArray); @@ -123,10 +132,12 @@ class PhpExporter extends AbstractExporter $manyToManyMappingArray = array( 'mappedBy' => $associationMapping->mappedBy, 'joinTable' => $associationMapping->joinTable, + 'orderBy' => $associationMapping->orderBy ); $associationMappingArray = array_merge($associationMappingArray, $manyToManyMappingArray); } + $lines[] = '$metadata->' . $method . '(' . $this->_varExport($associationMappingArray) . ');'; } diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index a0cef775d..dd3e03483 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -157,7 +157,8 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase * @depends testManyToManyAssociationWithCascadeAll * @param ClassMetadata $class */ - public function testLifecycleCallbacksSupportMultipleMethodNames($class) { + public function testLifecycleCallbacksSupportMultipleMethodNames($class) + { $this->assertEquals(count($class->lifecycleCallbacks['prePersist']), 2); $this->assertEquals($class->lifecycleCallbacks['prePersist'][1], 'doOtherStuffOnPrePersistToo'); diff --git a/tests/Doctrine/Tests/ORM/Mapping/AllTests.php b/tests/Doctrine/Tests/ORM/Mapping/AllTests.php index b667656eb..db9ceb0e2 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AllTests.php @@ -23,6 +23,7 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\XmlMappingDriverTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\YamlMappingDriverTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\AnnotationDriverTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\PhpMappingDriverTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataLoadEventTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Mapping\BasicInheritanceMappingTest'); diff --git a/tests/Doctrine/Tests/ORM/Mapping/PhpMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/PhpMappingDriverTest.php new file mode 100644 index 000000000..943c7580c --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/PhpMappingDriverTest.php @@ -0,0 +1,31 @@ +addMappingSource(__DIR__ . DIRECTORY_SEPARATOR . 'yaml', 'yaml'); + + $exporter = $cme->getExporter('php', $path); + $exporter->setMetadatas($cme->getMetadatasForMappingSources()); + $exporter->export(); + */ + + return new PhpDriver($path); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php new file mode 100644 index 000000000..9623e991f --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php @@ -0,0 +1,102 @@ +setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE); +$metadata->setPrimaryTable(array( + 'name' => 'cms_users', + )); +$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT); +$metadata->addLifecycleCallback('doStuffOnPrePersist', 'prePersist'); +$metadata->addLifecycleCallback('doOtherStuffOnPrePersistToo', 'prePersist'); +$metadata->addLifecycleCallback('doStuffOnPostPersist', 'postPersist'); +$metadata->mapField(array( + 'id' => true, + 'fieldName' => 'id', + 'type' => 'integer', + 'columnName' => 'id', + )); +$metadata->mapField(array( + 'fieldName' => 'name', + 'type' => 'string', + 'length' => 50, + 'unique' => true, + 'nullable' => true, + 'columnName' => 'name', + )); +$metadata->mapField(array( + 'fieldName' => 'email', + 'type' => 'string', + 'columnName' => 'user_email', + 'columnDefinition' => 'CHAR(32) NOT NULL', + )); +$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); +$metadata->mapOneToOne(array( + 'fieldName' => 'address', + 'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Address', + 'cascade' => + array( + 0 => 'remove', + ), + 'mappedBy' => NULL, + 'joinColumns' => + array( + 0 => + array( + 'name' => 'address_id', + 'referencedColumnName' => 'id', + ), + ), + 'orphanRemoval' => false, + )); +$metadata->mapOneToMany(array( + 'fieldName' => 'phonenumbers', + 'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Phonenumber', + 'cascade' => + array( + 1 => 'persist', + ), + 'mappedBy' => 'user', + 'orphanRemoval' => false, + 'orderBy' => + array( + 'number' => 'ASC', + ), + )); +$metadata->mapManyToMany(array( + 'fieldName' => 'groups', + 'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Group', + 'cascade' => + array( + 0 => 'remove', + 1 => 'persist', + 2 => 'refresh', + 3 => 'merge', + 4 => 'detach', + ), + 'mappedBy' => NULL, + 'joinTable' => + array( + 'name' => 'cms_users_groups', + 'joinColumns' => + array( + 0 => + array( + 'name' => 'user_id', + 'referencedColumnName' => 'id', + 'unique' => false, + 'nullable' => false, + ), + ), + 'inverseJoinColumns' => + array( + 0 => + array( + 'name' => 'group_id', + 'referencedColumnName' => 'id', + 'columnDefinition' => 'INT NULL', + ), + ), + ), + 'orderBy' => NULL, + )); \ No newline at end of file