Symfony - 如何在[EntityName] Type.php中访问[EntityName] .orm.yml中的值?

时间:2017-02-06 09:00:24

标签: php validation yaml symfony

假设我有一个实体class A{ private int _calls; private Stopwatch _sw; public A(){ _calls = 0; _sw = new StopWatch(); _sw.Start(); } public void MethodToMeasure(){ //Do stuff _calls++; if(sw.ElapsedMilliseconds > 1000){ _sw.Stop(); //Save or print _calls here before it's zeroed _calls = 0; _sw.Restart(); } } }

Car

适用于学说的Class Car { $private name; //... } 文件:

Car.orm.yml

在相应的...\Bundle\CarBundle\Entity\Car: ... fields: name: type: string length: 100 ... 类中,我想访问CarType文件的length值:

Car.orm.yml

如何访问 class CarType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', TextType::class, array( 'attr' => array('maxlength' => 100),// Access length here! )) //... } } 课程内length的{​​{1}}值?

1 个答案:

答案 0 :(得分:1)

这可以通过实体元数据访问。

  1. http://symfony.com/doc/current/form/form_dependencies.html#define-your-form-as-a-service

  2. 将实体管理器添加为依赖项。

  3. 通过完整的班级名称获取课程元数据:

    $classMetadata = $em->getClassMetadata('Bundle\CarBundle\Entity\Car')
    
  4. 获取字段定义:

    $classMetadata->getFieldMapping('name')['length']