在表单扩展 Symfony2 中获取实体元数据

Get entity metadata in form extension Symfony2

我尝试获取实体元数据,当我这样做时:

$entityMetadata = $this->validator->getMetadataFactory()->getMetadataFor('Acme\DemoBundle\Entity\Post');

一切正常,但当我这样做时:

$entityMetadata = $this->validator->getMetadataFactory()->getMetadataFor($formData);

$formData 等于 string 'Acme\DemoBundle\Entity\Post' (length=27)

我收到错误:

Cannot create metadata for non-objects. Got: NULL

我做错了什么?有人可以帮我吗?谢谢!

根据 getMetadataFor 签名 ( http://www.doctrine-project.org/api/orm/2.3/class-Doctrine.ORM.Mapping.ClassMetadataFactory.html ) 你必须传递一个字符串。也许你应该尝试这样的事情:

$entityMetadata = $this->validator->getMetadataFactory()->getMetadataFor(get_class($formData));