Skip to content

Commit 8e7b892

Browse files
author
floriansemm
committed
read annotations from parent class, fixes #89
1 parent 94ccc52 commit 8e7b892

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

Doctrine/Annotation/AnnotationReader.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct()
3333
private function getPropertiesByType($entity, $type)
3434
{
3535
$reflectionClass = new \ReflectionClass($entity);
36-
$properties = $reflectionClass->getProperties();
36+
$properties = array_merge($reflectionClass->getProperties(), $this->getParentProperties($reflectionClass));
3737

3838
$fields = array();
3939
foreach ($properties as $property) {
@@ -53,6 +53,21 @@ private function getPropertiesByType($entity, $type)
5353
return $fields;
5454
}
5555

56+
/**
57+
* @param \ReflectionClass $reflectionClass
58+
*
59+
* @return \ReflectionProperty[]
60+
*/
61+
private function getParentProperties(\ReflectionClass $reflectionClass)
62+
{
63+
$parent = $reflectionClass->getParentClass();
64+
if ($parent == null) {
65+
return array();
66+
}
67+
68+
return $parent->getProperties();
69+
}
70+
5671
/**
5772
* @param object $entity
5873
*
@@ -65,9 +80,10 @@ public function getFields($entity)
6580

6681
/**
6782
* @param object $entity
68-
* @throws \InvalidArgumentException if the boost value is not numeric
6983
*
7084
* @return number
85+
*
86+
* @throws \InvalidArgumentException if the boost value is not numeric
7187
*/
7288
public function getEntityBoost($entity)
7389
{
@@ -108,6 +124,7 @@ public function getDocumentIndex($entity)
108124

109125
/**
110126
* @param object $entity
127+
*
111128
* @return Type
112129
*
113130
* @throws \RuntimeException
@@ -165,6 +182,7 @@ public function getFieldMapping($entity)
165182

166183
/**
167184
* @param object $entity
185+
*
168186
* @return boolean
169187
*/
170188
public function hasDocumentDeclaration($entity)
@@ -192,14 +210,20 @@ public function getSynchronizationCallback($entity)
192210

193211
/**
194212
* @param string $entity
195-
* @param string $annotation
213+
* @param string $annotationName
196214
*
197215
* @return string
198216
*/
199-
private function getClassAnnotation($entity, $annotation)
217+
private function getClassAnnotation($entity, $annotationName)
200218
{
201219
$reflectionClass = new \ReflectionClass($entity);
202220

203-
return $this->reader->getClassAnnotation($reflectionClass, $annotation);
221+
$annotation = $this->reader->getClassAnnotation($reflectionClass, $annotationName);
222+
223+
if ($annotation === null && $reflectionClass->getParentClass()) {
224+
$annotation = $this->reader->getClassAnnotation($reflectionClass->getParentClass(), $annotationName);
225+
}
226+
227+
return $annotation;
204228
}
205229
}

Tests/Doctrine/Annotation/AnnotationReaderTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,39 @@ public function getIndexFromIndexHandler()
200200

201201
$this->assertEquals('my_core', $index);
202202
}
203+
204+
/**
205+
* @test
206+
*/
207+
public function readAnnotationsFromBaseClass()
208+
{
209+
$reader = new AnnotationReader();
210+
$fields = $reader->getFields(new ChildEntity());
211+
212+
$this->assertEquals(2, count($fields));
213+
$this->assertTrue($reader->hasDocumentDeclaration(new ChildEntity()));
214+
}
215+
}
216+
217+
use FS\SolrBundle\Doctrine\Annotation as Solr;
218+
219+
/**
220+
*
221+
* @Solr\Document
222+
*/
223+
abstract class BaseEntity
224+
{
225+
/**
226+
*
227+
* @Solr\Field(type="integer")
228+
*/
229+
private $field;
203230
}
204231

232+
class ChildEntity extends BaseEntity
233+
{
234+
/**
235+
* @Solr\Field(type="integer")
236+
*/
237+
private $childField;
238+
}

0 commit comments

Comments
 (0)