[英]Silex FormServiceProvider could not load type “form” when using Symfony3 Components
[英]Symfony3 Could not load type error during form generation
我正在嘗試在 Symfony 3 中構建一個表單,但遇到了一些我認為應該很容易解決的問題,但我想我沒有找到正確的地方。
我有 2 個對象,實體和供應商。 1 個實體確實有 1 個與之關聯的供應商。 1 個供應商可以有許多與之相關的實體。 (一對多關聯)。
我正在嘗試為實體類構建表單,並使用下拉列表提供必須與之關聯的供應商。
這是實體表單類:
<?php
namespace VP\SupplierBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use VP\SupplierBundle\Entity\Supplier;
use VP\SupplierBundle\Repository\SupplierRepository;
class EntityType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name')
->add('supplier', Supplier::class, array(
'class' => 'VPSupplierBundle:Supplier',
'property' => 'name',
'query_builder' => function (\Doctrine\ORM\EntityRepository $er) {
return $er->findAllByIsDeleted(0);
},
'choice_label' => 'name'
))
->add('submitnew', SubmitType::class, array(
'label' => 'Add Entity',
'attr' => array(
'class' => 'btn btn-primary',
)));
}
這是我的SupplierRepository,它應該被調用:
<?php
namespace VP\SupplierBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* SupplierRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class SupplierRepository extends EntityRepository {
public function findAllByIsDeleted($isDeleted) {
$qb = $this->createQueryBuilder('s');
$qb
->where('s.isDeleted = :isDeleted')
->setParameter('isDeleted', $isDeleted);
return $qb;
}
}
盡管如此,我還是收到以下錯誤:
無法加載類型“VP\\SupplierBundle\\Entity\\Supplier”
知道它可能來自哪里嗎? 試圖查看官方 symfony 文檔和一些論壇主題,但到目前為止沒有運氣......
非常感謝你的幫助!
@Jeet 提供的工作解決方案
//
use Symfony\Bridge\Doctrine\Form\Type\EntityType as DoctrineEntityType;
//
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name')
->add('supplier', DoctrineEntityType::class, array(
'class' => 'VPSupplierBundle:Supplier',
'query_builder' => function (EntityRepository $er) {
return $er->findAllByIsDeleted(0);
},
'choice_label' => 'name'
))
->add('submitnew', SubmitType::class, array(
'label' => 'Add Entity',
'attr' => array(
'class' => 'btn btn-primary',
)));
}
謝謝!
你在這里做錯了:
->add('supplier', Supplier::class, array(
'class' => 'VPSupplierBundle:Supplier',
'property' => 'name',
'query_builder' => function (\Doctrine\ORM\EntityRepository $er) {
return $er->findAllByIsDeleted(0);
},
'choice_label' => 'name'
))
您正在加載表單類型未知的Supplier::class
。 我想您應該改為加載EntityType::class
表單類型。
檢查這里的文件。 您想顯示一個與實體關聯的選擇字段,因此您需要有EntityType::class
。 要識別實體類型,您已經通過class
選項提供了您的實體身份。 所以它說清楚了。
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.