2009-01-05 23:18:56 +03:00
|
|
|
<?php
|
2009-02-04 19:35:36 +03:00
|
|
|
/*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2009-02-04 19:35:36 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM\Mapping\Driver;
|
|
|
|
|
2012-12-03 13:36:08 +04:00
|
|
|
use Doctrine\Common\Annotations\AnnotationReader;
|
|
|
|
use Doctrine\ORM\Mapping\MappingException;
|
|
|
|
use Doctrine\ORM\Mapping\JoinColumn;
|
|
|
|
use Doctrine\ORM\Mapping\Column;
|
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver;
|
2012-07-30 02:10:33 +04:00
|
|
|
use Doctrine\ORM\Events;
|
2009-01-05 23:18:56 +03:00
|
|
|
|
|
|
|
/**
|
2009-07-08 19:25:41 +04:00
|
|
|
* The AnnotationDriver reads the mapping metadata from docblock annotations.
|
2009-01-05 23:18:56 +03:00
|
|
|
*
|
2010-05-08 19:01:20 +04:00
|
|
|
* @since 2.0
|
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan H. Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-01-05 23:18:56 +03:00
|
|
|
*/
|
2012-01-17 21:09:49 +04:00
|
|
|
class AnnotationDriver extends AbstractAnnotationDriver
|
2009-01-22 22:38:10 +03:00
|
|
|
{
|
2010-03-05 19:35:00 +03:00
|
|
|
/**
|
2012-06-21 17:31:36 +04:00
|
|
|
* {@inheritDoc}
|
2010-01-28 22:38:16 +03:00
|
|
|
*/
|
2012-01-17 19:05:20 +04:00
|
|
|
protected $entityAnnotationClasses = array(
|
|
|
|
'Doctrine\ORM\Mapping\Entity' => 1,
|
|
|
|
'Doctrine\ORM\Mapping\MappedSuperclass' => 2,
|
|
|
|
);
|
2009-10-08 02:14:13 +04:00
|
|
|
|
2009-07-15 20:31:59 +04:00
|
|
|
/**
|
2012-06-21 17:31:36 +04:00
|
|
|
* {@inheritDoc}
|
2009-01-05 23:18:56 +03:00
|
|
|
*/
|
2012-01-12 05:09:26 +04:00
|
|
|
public function loadMetadataForClass($className, ClassMetadata $metadata)
|
2009-01-05 23:18:56 +03:00
|
|
|
{
|
2012-06-21 17:31:36 +04:00
|
|
|
/* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */
|
2009-07-08 19:25:41 +04:00
|
|
|
$class = $metadata->getReflectionClass();
|
2012-04-14 09:10:44 +04:00
|
|
|
if ( ! $class) {
|
2012-01-09 11:02:53 +04:00
|
|
|
// this happens when running annotation driver in combination with
|
|
|
|
// static reflection services. This is not the nicest fix
|
2012-01-09 11:04:21 +04:00
|
|
|
$class = new \ReflectionClass($metadata->name);
|
2012-01-09 11:02:53 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-01-17 19:05:20 +04:00
|
|
|
$classAnnotations = $this->reader->getClassAnnotations($class);
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2012-07-29 13:56:41 +04:00
|
|
|
if ($classAnnotations) {
|
|
|
|
foreach ($classAnnotations as $key => $annot) {
|
|
|
|
if ( ! is_numeric($key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-05-23 00:10:13 +04:00
|
|
|
$classAnnotations[get_class($annot)] = $annot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-30 19:16:02 +04:00
|
|
|
// Evaluate Entity annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\Entity'])) {
|
|
|
|
$entityAnnot = $classAnnotations['Doctrine\ORM\Mapping\Entity'];
|
2011-09-08 20:17:53 +04:00
|
|
|
if ($entityAnnot->repositoryClass !== null) {
|
2011-09-08 00:27:05 +04:00
|
|
|
$metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
|
|
|
|
}
|
2011-03-29 22:17:44 +04:00
|
|
|
if ($entityAnnot->readOnly) {
|
|
|
|
$metadata->markReadOnly();
|
|
|
|
}
|
2009-07-30 19:16:02 +04:00
|
|
|
} else if (isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'])) {
|
2011-09-08 00:27:05 +04:00
|
|
|
$mappedSuperclassAnnot = $classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'];
|
|
|
|
$metadata->setCustomRepositoryClass($mappedSuperclassAnnot->repositoryClass);
|
2009-07-30 19:16:02 +04:00
|
|
|
$metadata->isMappedSuperclass = true;
|
|
|
|
} else {
|
2010-02-03 00:17:00 +03:00
|
|
|
throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
|
|
|
|
2010-04-30 19:30:27 +04:00
|
|
|
// Evaluate Table annotation
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\Table'])) {
|
|
|
|
$tableAnnot = $classAnnotations['Doctrine\ORM\Mapping\Table'];
|
2009-08-21 22:13:22 +04:00
|
|
|
$primaryTable = array(
|
2009-02-04 19:35:36 +03:00
|
|
|
'name' => $tableAnnot->name,
|
2009-05-29 14:23:13 +04:00
|
|
|
'schema' => $tableAnnot->schema
|
2009-08-21 22:13:22 +04:00
|
|
|
);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if ($tableAnnot->indexes !== null) {
|
|
|
|
foreach ($tableAnnot->indexes as $indexAnnot) {
|
2012-03-03 22:05:11 +04:00
|
|
|
$index = array('columns' => $indexAnnot->columns);
|
|
|
|
|
|
|
|
if ( ! empty($indexAnnot->name)) {
|
|
|
|
$primaryTable['indexes'][$indexAnnot->name] = $index;
|
|
|
|
} else {
|
|
|
|
$primaryTable['indexes'][] = $index;
|
|
|
|
}
|
2009-08-21 22:13:22 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if ($tableAnnot->uniqueConstraints !== null) {
|
2012-03-03 22:05:11 +04:00
|
|
|
foreach ($tableAnnot->uniqueConstraints as $uniqueConstraintAnnot) {
|
|
|
|
$uniqueConstraint = array('columns' => $uniqueConstraintAnnot->columns);
|
|
|
|
|
|
|
|
if ( ! empty($uniqueConstraintAnnot->name)) {
|
|
|
|
$primaryTable['uniqueConstraints'][$uniqueConstraintAnnot->name] = $uniqueConstraint;
|
|
|
|
} else {
|
|
|
|
$primaryTable['uniqueConstraints'][] = $uniqueConstraint;
|
|
|
|
}
|
2009-08-21 22:13:22 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-01-26 17:36:56 +04:00
|
|
|
if ($tableAnnot->options !== null) {
|
|
|
|
$primaryTable['options'] = $tableAnnot->options;
|
|
|
|
}
|
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
$metadata->setPrimaryTable($primaryTable);
|
2009-01-06 21:30:51 +03:00
|
|
|
}
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2012-02-26 23:51:39 +04:00
|
|
|
// Evaluate NamedNativeQueries annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\NamedNativeQueries'])) {
|
|
|
|
$namedNativeQueriesAnnot = $classAnnotations['Doctrine\ORM\Mapping\NamedNativeQueries'];
|
|
|
|
|
|
|
|
foreach ($namedNativeQueriesAnnot->value as $namedNativeQuery) {
|
|
|
|
$metadata->addNamedNativeQuery(array(
|
|
|
|
'name' => $namedNativeQuery->name,
|
|
|
|
'query' => $namedNativeQuery->query,
|
|
|
|
'resultClass' => $namedNativeQuery->resultClass,
|
|
|
|
'resultSetMapping' => $namedNativeQuery->resultSetMapping,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate SqlResultSetMappings annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\SqlResultSetMappings'])) {
|
|
|
|
$sqlResultSetMappingsAnnot = $classAnnotations['Doctrine\ORM\Mapping\SqlResultSetMappings'];
|
|
|
|
|
|
|
|
foreach ($sqlResultSetMappingsAnnot->value as $resultSetMapping) {
|
|
|
|
$entities = array();
|
2012-03-10 06:03:25 +04:00
|
|
|
$columns = array();
|
2012-02-26 23:51:39 +04:00
|
|
|
foreach ($resultSetMapping->entities as $entityResultAnnot) {
|
|
|
|
$entityResult = array(
|
|
|
|
'fields' => array(),
|
|
|
|
'entityClass' => $entityResultAnnot->entityClass,
|
|
|
|
'discriminatorColumn' => $entityResultAnnot->discriminatorColumn,
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($entityResultAnnot->fields as $fieldResultAnnot) {
|
|
|
|
$entityResult['fields'][] = array(
|
|
|
|
'name' => $fieldResultAnnot->name,
|
|
|
|
'column' => $fieldResultAnnot->column
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$entities[] = $entityResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($resultSetMapping->columns as $columnResultAnnot) {
|
|
|
|
$columns[] = array(
|
2012-02-27 04:10:19 +04:00
|
|
|
'name' => $columnResultAnnot->name,
|
2012-02-26 23:51:39 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$metadata->addSqlResultSetMapping(array(
|
|
|
|
'name' => $resultSetMapping->name,
|
|
|
|
'entities' => $entities,
|
|
|
|
'columns' => $columns
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 00:45:09 +03:00
|
|
|
// Evaluate NamedQueries annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\NamedQueries'])) {
|
2012-02-27 02:27:42 +04:00
|
|
|
$namedQueriesAnnot = $classAnnotations['Doctrine\ORM\Mapping\NamedQueries'];
|
2011-03-07 00:45:09 +03:00
|
|
|
|
2012-04-14 09:10:44 +04:00
|
|
|
if ( ! is_array($namedQueriesAnnot->value)) {
|
2011-12-17 15:35:22 +04:00
|
|
|
throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations.");
|
|
|
|
}
|
|
|
|
|
2012-02-27 02:27:42 +04:00
|
|
|
foreach ($namedQueriesAnnot->value as $namedQuery) {
|
2012-04-14 09:10:44 +04:00
|
|
|
if ( ! ($namedQuery instanceof \Doctrine\ORM\Mapping\NamedQuery)) {
|
2011-12-17 15:35:22 +04:00
|
|
|
throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations.");
|
|
|
|
}
|
2011-03-07 00:45:09 +03:00
|
|
|
$metadata->addNamedQuery(array(
|
|
|
|
'name' => $namedQuery->name,
|
|
|
|
'query' => $namedQuery->query
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 20:14:11 +04:00
|
|
|
// Evaluate InheritanceType annotation
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\InheritanceType'])) {
|
|
|
|
$inheritanceTypeAnnot = $classAnnotations['Doctrine\ORM\Mapping\InheritanceType'];
|
2010-04-10 02:00:36 +04:00
|
|
|
$metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value));
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2010-08-08 14:29:14 +04:00
|
|
|
if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) {
|
|
|
|
// Evaluate DiscriminatorColumn annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'])) {
|
|
|
|
$discrColumnAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'];
|
|
|
|
$metadata->setDiscriminatorColumn(array(
|
|
|
|
'name' => $discrColumnAnnot->name,
|
|
|
|
'type' => $discrColumnAnnot->type,
|
2012-02-14 05:22:49 +04:00
|
|
|
'length' => $discrColumnAnnot->length,
|
|
|
|
'columnDefinition' => $discrColumnAnnot->columnDefinition
|
2010-08-08 14:29:14 +04:00
|
|
|
));
|
2010-09-24 01:10:31 +04:00
|
|
|
} else {
|
|
|
|
$metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255));
|
2010-08-08 14:29:14 +04:00
|
|
|
}
|
2009-05-05 21:20:55 +04:00
|
|
|
|
2010-08-08 14:29:14 +04:00
|
|
|
// Evaluate DiscriminatorMap annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorMap'])) {
|
|
|
|
$discrMapAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorMap'];
|
|
|
|
$metadata->setDiscriminatorMap($discrMapAnnot->value);
|
|
|
|
}
|
|
|
|
}
|
2009-04-12 23:02:12 +04:00
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
|
2010-08-08 14:29:14 +04:00
|
|
|
|
2009-05-05 21:20:55 +04:00
|
|
|
// Evaluate DoctrineChangeTrackingPolicy annotation
|
2009-07-24 15:33:38 +04:00
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'])) {
|
|
|
|
$changeTrackingAnnot = $classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'];
|
2010-04-10 02:00:36 +04:00
|
|
|
$metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_' . $changeTrackingAnnot->value));
|
2009-05-05 21:20:55 +04:00
|
|
|
}
|
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
// Evaluate annotations on properties/fields
|
2012-06-21 17:31:36 +04:00
|
|
|
/* @var $property \ReflectionProperty */
|
2009-07-08 19:25:41 +04:00
|
|
|
foreach ($class->getProperties() as $property) {
|
2009-10-07 22:53:30 +04:00
|
|
|
if ($metadata->isMappedSuperclass && ! $property->isPrivate()
|
|
|
|
||
|
|
|
|
$metadata->isInheritedField($property->name)
|
|
|
|
||
|
|
|
|
$metadata->isInheritedAssociation($property->name)) {
|
2009-04-09 22:12:48 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping = array();
|
|
|
|
$mapping['fieldName'] = $property->getName();
|
2009-02-04 19:35:36 +03:00
|
|
|
|
2009-06-30 20:00:28 +04:00
|
|
|
// Check for JoinColummn/JoinColumns annotations
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinColumns = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-01-17 19:05:20 +04:00
|
|
|
if ($joinColumnAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumn')) {
|
2011-12-14 22:28:01 +04:00
|
|
|
$joinColumns[] = $this->joinColumnToArray($joinColumnAnnot);
|
2012-01-17 19:05:20 +04:00
|
|
|
} else if ($joinColumnsAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumns')) {
|
2009-07-08 19:25:41 +04:00
|
|
|
foreach ($joinColumnsAnnot->value as $joinColumn) {
|
2011-12-14 22:28:01 +04:00
|
|
|
$joinColumns[] = $this->joinColumnToArray($joinColumn);
|
2009-07-08 19:25:41 +04:00
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
|
|
|
|
2009-06-30 20:00:28 +04:00
|
|
|
// Field can only be annotated with one of:
|
|
|
|
// @Column, @OneToOne, @OneToMany, @ManyToOne, @ManyToMany
|
2012-01-17 19:05:20 +04:00
|
|
|
if ($columnAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Column')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
if ($columnAnnot->type == null) {
|
2010-02-03 00:17:00 +03:00
|
|
|
throw MappingException::propertyTypeIsRequired($className, $property->getName());
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-04-14 09:10:44 +04:00
|
|
|
$mapping = $this->columnToArray($property->getName(), $columnAnnot);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-01-17 19:05:20 +04:00
|
|
|
if ($idAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['id'] = true;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-01-17 19:05:20 +04:00
|
|
|
if ($generatedValueAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\GeneratedValue')) {
|
2009-06-07 21:20:37 +04:00
|
|
|
$metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy));
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-01-17 19:05:20 +04:00
|
|
|
if ($this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Version')) {
|
2009-08-24 21:06:12 +04:00
|
|
|
$metadata->setVersionMapping($mapping);
|
2009-07-17 22:13:03 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->mapField($mapping);
|
2009-03-30 23:43:05 +04:00
|
|
|
|
|
|
|
// Check for SequenceGenerator/TableGenerator definition
|
2012-01-17 19:05:20 +04:00
|
|
|
if ($seqGeneratorAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\SequenceGenerator')) {
|
2009-03-30 23:43:05 +04:00
|
|
|
$metadata->setSequenceGeneratorDefinition(array(
|
|
|
|
'sequenceName' => $seqGeneratorAnnot->sequenceName,
|
|
|
|
'allocationSize' => $seqGeneratorAnnot->allocationSize,
|
|
|
|
'initialValue' => $seqGeneratorAnnot->initialValue
|
|
|
|
));
|
2012-01-17 19:05:20 +04:00
|
|
|
} else if ($this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\TableGenerator')) {
|
2010-02-03 00:17:00 +03:00
|
|
|
throw MappingException::tableIdGeneratorNotImplemented($className);
|
2012-01-17 19:05:20 +04:00
|
|
|
} else if ($customGeneratorAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\CustomIdGenerator')) {
|
2011-11-26 14:32:44 +04:00
|
|
|
$metadata->setCustomGeneratorDefinition(array(
|
2012-01-08 16:20:35 +04:00
|
|
|
'class' => $customGeneratorAnnot->class
|
2011-11-26 14:32:44 +04:00
|
|
|
));
|
2009-03-30 23:43:05 +04:00
|
|
|
}
|
2012-01-17 19:05:20 +04:00
|
|
|
} else if ($oneToOneAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToOne')) {
|
|
|
|
if ($idAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
|
2010-08-07 21:33:54 +04:00
|
|
|
$mapping['id'] = true;
|
|
|
|
}
|
|
|
|
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['targetEntity'] = $oneToOneAnnot->targetEntity;
|
2009-02-04 19:35:36 +03:00
|
|
|
$mapping['joinColumns'] = $joinColumns;
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['mappedBy'] = $oneToOneAnnot->mappedBy;
|
2010-03-15 20:19:00 +03:00
|
|
|
$mapping['inversedBy'] = $oneToOneAnnot->inversedBy;
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['cascade'] = $oneToOneAnnot->cascade;
|
2009-07-23 13:52:16 +04:00
|
|
|
$mapping['orphanRemoval'] = $oneToOneAnnot->orphanRemoval;
|
2011-11-08 13:01:22 +04:00
|
|
|
$mapping['fetch'] = $this->getFetchMode($className, $oneToOneAnnot->fetch);
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->mapOneToOne($mapping);
|
2012-01-17 19:05:20 +04:00
|
|
|
} else if ($oneToManyAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToMany')) {
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['mappedBy'] = $oneToManyAnnot->mappedBy;
|
|
|
|
$mapping['targetEntity'] = $oneToManyAnnot->targetEntity;
|
2009-01-09 19:25:06 +03:00
|
|
|
$mapping['cascade'] = $oneToManyAnnot->cascade;
|
2011-02-05 11:31:40 +03:00
|
|
|
$mapping['indexBy'] = $oneToManyAnnot->indexBy;
|
2009-07-23 13:52:16 +04:00
|
|
|
$mapping['orphanRemoval'] = $oneToManyAnnot->orphanRemoval;
|
2011-11-08 13:01:22 +04:00
|
|
|
$mapping['fetch'] = $this->getFetchMode($className, $oneToManyAnnot->fetch);
|
2010-02-14 22:38:22 +03:00
|
|
|
|
2012-01-17 19:05:20 +04:00
|
|
|
if ($orderByAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OrderBy')) {
|
2010-02-14 22:38:22 +03:00
|
|
|
$mapping['orderBy'] = $orderByAnnot->value;
|
|
|
|
}
|
|
|
|
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->mapOneToMany($mapping);
|
2012-01-17 19:05:20 +04:00
|
|
|
} else if ($manyToOneAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToOne')) {
|
|
|
|
if ($idAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
|
2010-08-07 21:33:54 +04:00
|
|
|
$mapping['id'] = true;
|
|
|
|
}
|
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
$mapping['joinColumns'] = $joinColumns;
|
2009-02-07 20:02:13 +03:00
|
|
|
$mapping['cascade'] = $manyToOneAnnot->cascade;
|
2010-03-15 20:19:00 +03:00
|
|
|
$mapping['inversedBy'] = $manyToOneAnnot->inversedBy;
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['targetEntity'] = $manyToOneAnnot->targetEntity;
|
2011-11-08 13:01:22 +04:00
|
|
|
$mapping['fetch'] = $this->getFetchMode($className, $manyToOneAnnot->fetch);
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->mapManyToOne($mapping);
|
2012-01-17 19:05:20 +04:00
|
|
|
} else if ($manyToManyAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToMany')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinTable = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-01-17 19:05:20 +04:00
|
|
|
if ($joinTableAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinTable')) {
|
2009-02-04 19:35:36 +03:00
|
|
|
$joinTable = array(
|
|
|
|
'name' => $joinTableAnnot->name,
|
2009-07-15 20:31:59 +04:00
|
|
|
'schema' => $joinTableAnnot->schema
|
2009-02-04 19:35:36 +03:00
|
|
|
);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-08 19:25:41 +04:00
|
|
|
foreach ($joinTableAnnot->joinColumns as $joinColumn) {
|
2011-12-14 22:28:01 +04:00
|
|
|
$joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumn);
|
2009-07-08 19:25:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($joinTableAnnot->inverseJoinColumns as $joinColumn) {
|
2011-12-14 22:28:01 +04:00
|
|
|
$joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumn);
|
2009-07-08 19:25:41 +04:00
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
$mapping['joinTable'] = $joinTable;
|
2009-01-05 23:18:56 +03:00
|
|
|
$mapping['targetEntity'] = $manyToManyAnnot->targetEntity;
|
|
|
|
$mapping['mappedBy'] = $manyToManyAnnot->mappedBy;
|
2010-03-15 20:19:00 +03:00
|
|
|
$mapping['inversedBy'] = $manyToManyAnnot->inversedBy;
|
2009-02-07 20:02:13 +03:00
|
|
|
$mapping['cascade'] = $manyToManyAnnot->cascade;
|
2011-02-05 11:31:40 +03:00
|
|
|
$mapping['indexBy'] = $manyToManyAnnot->indexBy;
|
2012-02-20 13:33:16 +04:00
|
|
|
$mapping['orphanRemoval'] = $manyToManyAnnot->orphanRemoval;
|
2011-11-08 13:01:22 +04:00
|
|
|
$mapping['fetch'] = $this->getFetchMode($className, $manyToManyAnnot->fetch);
|
2010-02-14 22:38:22 +03:00
|
|
|
|
2012-01-17 19:05:20 +04:00
|
|
|
if ($orderByAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OrderBy')) {
|
2010-02-14 22:38:22 +03:00
|
|
|
$mapping['orderBy'] = $orderByAnnot->value;
|
|
|
|
}
|
|
|
|
|
2009-01-05 23:18:56 +03:00
|
|
|
$metadata->mapManyToMany($mapping);
|
2009-10-09 18:27:35 +04:00
|
|
|
}
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-02-02 06:57:39 +04:00
|
|
|
// 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) {
|
|
|
|
$joinTableAnnot = $associationOverride->joinTable;
|
2012-07-30 03:47:31 +04:00
|
|
|
$joinTable = array(
|
2012-02-02 06:57:39 +04:00
|
|
|
'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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-14 09:10:44 +04:00
|
|
|
// Evaluate AttributeOverrides annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\AttributeOverrides'])) {
|
|
|
|
$attributeOverridesAnnot = $classAnnotations['Doctrine\ORM\Mapping\AttributeOverrides'];
|
|
|
|
foreach ($attributeOverridesAnnot->value as $attributeOverrideAnnot) {
|
|
|
|
$attributeOverride = $this->columnToArray($attributeOverrideAnnot->name, $attributeOverrideAnnot->column);
|
|
|
|
$metadata->setAttributeOverride($attributeOverrideAnnot->name, $attributeOverride);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 02:10:33 +04:00
|
|
|
// Evaluate EntityListeners annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\EntityListeners'])) {
|
|
|
|
$entityListenersAnnot = $classAnnotations['Doctrine\ORM\Mapping\EntityListeners'];
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-08-01 05:20:21 +04:00
|
|
|
foreach ($entityListenersAnnot->value as $item) {
|
|
|
|
$listenerClassName = $metadata->fullyQualifiedClassName($item);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-08-01 05:20:21 +04:00
|
|
|
if ( ! class_exists($listenerClassName)) {
|
|
|
|
throw MappingException::entityListenerClassNotFound($listenerClassName, $className);
|
2012-07-30 02:10:33 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-08-01 05:20:21 +04:00
|
|
|
$listenerClass = new \ReflectionClass($listenerClassName);
|
2012-07-30 04:04:59 +04:00
|
|
|
/* @var $method \ReflectionMethod */
|
2012-08-01 05:20:21 +04:00
|
|
|
foreach ($listenerClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
|
2012-07-30 04:04:59 +04:00
|
|
|
// find method callbacks.
|
2012-07-30 02:10:33 +04:00
|
|
|
foreach ($this->getMethodCallbacks($method) as $value) {
|
2012-08-01 05:20:21 +04:00
|
|
|
$metadata->addEntityListener($value[1], $listenerClassName, $value[0]);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2012-07-30 02:10:33 +04:00
|
|
|
}
|
|
|
|
}
|
2012-08-08 05:34:32 +04:00
|
|
|
|
2012-08-12 21:53:00 +04:00
|
|
|
// Evaluate as lifecycle callback if the listener class is not given.
|
2012-08-08 05:34:32 +04:00
|
|
|
if(empty($entityListenersAnnot->value)) {
|
|
|
|
/* @var $method \Doctrine\ORM\Mapping\LifecycleCallback */
|
|
|
|
foreach ($entityListenersAnnot->callbacks as $callback) {
|
|
|
|
$metadata->addLifecycleCallback($callback->method, $callback->event);
|
|
|
|
}
|
|
|
|
}
|
2012-07-30 02:10:33 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2012-07-30 02:10:33 +04:00
|
|
|
// Evaluate @HasLifecycleCallbacks annotation
|
|
|
|
if (isset($classAnnotations['Doctrine\ORM\Mapping\HasLifecycleCallbacks'])) {
|
|
|
|
/* @var $method \ReflectionMethod */
|
|
|
|
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
|
|
|
|
// filter for the declaring class only, callbacks from parents will already be registered.
|
|
|
|
if ($method->getDeclaringClass()->name !== $class->name) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-23 19:39:16 +04:00
|
|
|
|
2012-07-30 02:10:33 +04:00
|
|
|
foreach ($this->getMethodCallbacks($method) as $value) {
|
2012-07-30 03:47:31 +04:00
|
|
|
$metadata->addLifecycleCallback($value[0], $value[1]);
|
2009-07-18 22:06:30 +04:00
|
|
|
}
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-05 20:34:44 +03:00
|
|
|
|
2011-11-08 13:01:22 +04:00
|
|
|
/**
|
|
|
|
* Attempts to resolve the fetch mode.
|
|
|
|
*
|
2012-12-03 13:36:08 +04:00
|
|
|
* @param string $className The class name.
|
|
|
|
* @param string $fetchMode The fetch mode.
|
|
|
|
*
|
|
|
|
* @return integer The fetch mode as defined in ClassMetadata.
|
|
|
|
*
|
|
|
|
* @throws MappingException If the fetch mode is not valid.
|
2011-11-08 13:01:22 +04:00
|
|
|
*/
|
|
|
|
private function getFetchMode($className, $fetchMode)
|
|
|
|
{
|
2012-04-14 09:10:44 +04:00
|
|
|
if( ! defined('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode)) {
|
2011-11-08 13:01:22 +04:00
|
|
|
throw MappingException::invalidFetchMode($className, $fetchMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode);
|
|
|
|
}
|
2011-12-14 22:28:01 +04:00
|
|
|
|
|
|
|
/**
|
2012-07-30 02:10:33 +04:00
|
|
|
* Parses the given method.
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2012-07-30 02:10:33 +04:00
|
|
|
* @param \ReflectionMethod $method
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getMethodCallbacks(\ReflectionMethod $method)
|
|
|
|
{
|
|
|
|
$callbacks = array();
|
|
|
|
$annotations = $this->reader->getMethodAnnotations($method);
|
|
|
|
|
|
|
|
foreach ($annotations as $annot) {
|
|
|
|
if ($annot instanceof \Doctrine\ORM\Mapping\PrePersist) {
|
|
|
|
$callbacks[] = array($method->name, Events::prePersist);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($annot instanceof \Doctrine\ORM\Mapping\PostPersist) {
|
|
|
|
$callbacks[] = array($method->name, Events::postPersist);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($annot instanceof \Doctrine\ORM\Mapping\PreUpdate) {
|
|
|
|
$callbacks[] = array($method->name, Events::preUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($annot instanceof \Doctrine\ORM\Mapping\PostUpdate) {
|
|
|
|
$callbacks[] = array($method->name, Events::postUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($annot instanceof \Doctrine\ORM\Mapping\PreRemove) {
|
|
|
|
$callbacks[] = array($method->name, Events::preRemove);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($annot instanceof \Doctrine\ORM\Mapping\PostRemove) {
|
|
|
|
$callbacks[] = array($method->name, Events::postRemove);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($annot instanceof \Doctrine\ORM\Mapping\PostLoad) {
|
|
|
|
$callbacks[] = array($method->name, Events::postLoad);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($annot instanceof \Doctrine\ORM\Mapping\PreFlush) {
|
|
|
|
$callbacks[] = array($method->name, Events::preFlush);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $callbacks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the given JoinColumn as array
|
2011-12-14 22:28:01 +04:00
|
|
|
*
|
2012-12-03 13:36:08 +04:00
|
|
|
* @return array
|
2011-12-14 22:28:01 +04:00
|
|
|
*/
|
|
|
|
private function joinColumnToArray(JoinColumn $joinColumn)
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'name' => $joinColumn->name,
|
|
|
|
'unique' => $joinColumn->unique,
|
|
|
|
'nullable' => $joinColumn->nullable,
|
|
|
|
'onDelete' => $joinColumn->onDelete,
|
|
|
|
'columnDefinition' => $joinColumn->columnDefinition,
|
|
|
|
'referencedColumnName' => $joinColumn->referencedColumnName,
|
|
|
|
);
|
|
|
|
}
|
2012-02-02 06:57:39 +04:00
|
|
|
|
2012-04-14 09:10:44 +04:00
|
|
|
/**
|
|
|
|
* Parse the given Column as array
|
|
|
|
*
|
2012-12-03 13:36:08 +04:00
|
|
|
* @param string $fieldName
|
|
|
|
* @param Column $column
|
|
|
|
*
|
|
|
|
* @return array
|
2012-04-14 09:10:44 +04:00
|
|
|
*/
|
|
|
|
private function columnToArray($fieldName, Column $column)
|
|
|
|
{
|
|
|
|
$mapping = array(
|
|
|
|
'fieldName' => $fieldName,
|
|
|
|
'type' => $column->type,
|
|
|
|
'scale' => $column->scale,
|
|
|
|
'length' => $column->length,
|
|
|
|
'unique' => $column->unique,
|
|
|
|
'nullable' => $column->nullable,
|
|
|
|
'precision' => $column->precision
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($column->options) {
|
|
|
|
$mapping['options'] = $column->options;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($column->name)) {
|
|
|
|
$mapping['columnName'] = $column->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($column->columnDefinition)) {
|
|
|
|
$mapping['columnDefinition'] = $column->columnDefinition;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $mapping;
|
|
|
|
}
|
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Factory method for the Annotation Driver.
|
|
|
|
*
|
|
|
|
* @param array|string $paths
|
|
|
|
* @param AnnotationReader|null $reader
|
2011-05-23 00:10:13 +04:00
|
|
|
*
|
2010-04-10 19:35:18 +04:00
|
|
|
* @return AnnotationDriver
|
|
|
|
*/
|
|
|
|
static public function create($paths = array(), AnnotationReader $reader = null)
|
|
|
|
{
|
|
|
|
if ($reader == null) {
|
|
|
|
$reader = new AnnotationReader();
|
|
|
|
}
|
2012-06-21 17:31:36 +04:00
|
|
|
|
2010-04-10 19:35:18 +04:00
|
|
|
return new self($reader, $paths);
|
|
|
|
}
|
2010-03-19 02:04:21 +03:00
|
|
|
}
|