2016-04-28 6 views
4

Symfony 2フレームワークを使用して、私はアノテーション付きのデータベースからのエンティティを持っています。セットのプロパティでパラメータの型を取得

私の特定のケースでは、私はエンティティを扱っていますが、私が使っているものはわかりません。

のは、私が

$class = get_class($entity); 
$reflect = new \ReflectionClass($class) 
$properties = $reflect->getProperties(); 

foreach($properties as $property) { 
    $entity->{'set' .ucfirst(strtolower($property))}($some_value); 
    .... 
} 

エンティティオブジェクト$実体を持っていると言うが、それは期待するパラメータの種類、各プロパティのために知っておくべき方法は何ですか?注釈を使用していますか?

+0

[教義エンティティプロパティの種類を取得する方法](http://stackoverflow.com/questions/27293233/how-to-get-the-type-of-a-doctrine-entity -property) – Miro

答えて

1

理論的にはこれを行うことができますが、パフォーマンスにどのように影響するかはわかりません。 これにDoctrine AnnotationReaderを使用できます。

ここでは、できることのコード例を示します。

use Doctrine\Common\Annotations\AnnotationReader; 

$class = get_class($entity); 
$reflect = new \ReflectionClass($class) 
$properties = $reflect->getProperties(); 
$annotationReader = new AnnotationReader(); 

foreach($properties as $property) { 
    $reflectionProperty = new ReflectionProperty($class, $property); 
    $propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty); 
    var_dump($propertAnnotations); 
    .... 
} 

はこのほかに、あなたが取得及び/または特定のオブジェクトの値を設定するためにsymfonyのProppertyAccesorに見たいと思うかもしれません。 http://symfony.com/doc/current/components/property_access/introduction.html

関連する問題