使用的Symfony 2.1.3-dev的和學說2.3使用實體字段類型
我試圖建立一個形式爲一個用戶過濾返回的一組數據提供多個選項(Entity\EngineCodes
) 。該表格由1個文本輸入字段(id
)和3個選擇字段(module
,type
,status
)組成。我正嘗試使用Symfony2 entity
form_type爲EngineCodes
實體的3個選擇字段生成值。
因爲我想要使用任何3個選擇字段的組合來篩選表。基於2.1文檔,我決定創建FormType(EngineCodesFilterType
),並將三個表單字段設置爲entity
類型,並使用query_builder
語句爲每個字段返回一組唯一值。
不幸的是,我收到下面的錯誤,我不完全確定它爲什麼返回一個數組而不是一個對象。
The form's view data is expected to be an instance of class
Vendor\IndexBundle\Entity\EngineCodes, but is a(n) array.
You can avoid this error by setting the "data_class" option
to null or by adding a view transformer that transforms a(n)
array to an instance of Vendor\IndexBundle\Entity\EngineCodes.
如果我設置data_class
到null
,我收到此錯誤:
A "__toString()" method was not found on the objects of type
"Vendor\IndexBundle\Entity\EngineCodes" passed to the choice
field. To read a custom getter instead, set the option
"property" to the desired property path.
由於我還在學習這些Symfony2的功能,我的目標是,以配合施工和格式條款的2.1文檔越多越好。
這裏是控制器內的功能:
public function displayAction() {
// ...
$entity = $this->getDoctrine()->getEntityManager()
->getRepository('VendorIndexBundle:EngineCodes')
->findAll();
// ...
$form = $this->createForm(new EngineCodesFilterType(), $entity);
// ...
return $this->render(
'VendorIndexBundle::layout.html.twig',
array(
'entity' => $entity,
'form' => $form->createView(),));
這裏是表單類型:
class EngineCodesFilterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'id',
'integer',
array(
'label' => 'Code ID',));
$builder->add(
'status',
'entity',
array(
'label' => 'Code Status',
'class' => 'VendorIndexBundle:EngineCodes',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('u')
->select('u.status')
->add('groupBy', 'u.status');
},
'multiple' => true,));
$builder->add(
'type',
'entity',
array(
'label' => 'Code Type',
'class' => 'VendorIndexBundle:EngineCodes',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('u')
->select('u.type')
->add('groupBy' ,'u.type');
},
'multiple' => true,));
$builder->add(
'module',
'entity',
array(
'label' => 'Code Module',
'class' => 'VendorIndexBundle:EngineCodes',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('u')
->select('u.module')
->add('groupBy', 'u.module');
},
'multiple' => true,));
}
public function getName()
{
return 'EngineCodesFilter';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
array(
'data_class' => 'Vendor\IndexBundle\Entity\EngineCodes',
/*'data_class' => null,*/
'validation_groups' => 'filter',));
}
}
這裏是Vendor\Entity\EngineCodes
類:
/**
* Vendor\IndexBundle\Entity\EngineCodes
*
* @ORM\Table(name="engine_codes")
* @ORM\Entity(repositoryClass="Vendor\IndexBundle\Entity\EngineCodesRepository")
* @UniqueEntity(fields="id", message="ID already in use! Enter a unique ID for the code.")
*/
class EngineCodes
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false, unique=true)
* @ORM\Id
* @Assert\NotBlank(message="ID cannot be blank!")
* @Assert\Regex(pattern="/^\d+$/", match=true, message="ID must be an integer!")
* @Assert\MinLength(limit=8, message="ID must be 8 numbers in length!")
* @Assert\MaxLength(limit=8, message="ID must be 8 numbers in length!")
*/
private $id;
/**
* @var string $token
*
* @ORM\Column(name="token", type="string", length=255, nullable=false, unique=true)
*/
private $token;
/**
* @var boolean $status
*
* @ORM\Column(name="status", type="integer", nullable=false)
* @Assert\NotBlank(message="Status cannot be blank!")
*/
private $status;
/**
* @var string $module
*
* @ORM\Column(name="module", type="string", length=255, nullable=false)
* @Assert\NotBlank(message="Module cannot be blank!")
*/
private $module;
/**
* @var string $submodule
*
* @ORM\Column(name="submodule", type="string", length=255, nullable=false)
* @Assert\NotBlank(message="Submodule cannot be blank!")
*/
private $submodule;
/**
* @var string $type
*
* @ORM\Column(name="type", type="string", length=255, nullable=false)
* @Assert\NotBlank(message="Type cannot be blank!")
*/
private $type;
/**
* @var string $description
*
* @ORM\Column(name="description", type="text", nullable=false)
* @Assert\NotBlank(message="Description cannot be blank!")
*/
private $description;
/**
* @var string $title
*
* @ORM\Column(name="title", type="string", length=255, nullable=false)
* @Assert\NotBlank(message="Title cannot be blank!")
*/
private $title;
/**
* @var string $definition
*
* @ORM\Column(name="definition", type="text", nullable=true)
*/
private $definition;
/**
* @var string $color
*
* @ORM\Column(name="color", type="string", length=10, nullable=true)
*/
private $color;
/**
* @var \DateTime $createTimestamp
*
* @ORM\Column(name="create_timestamp", type="datetime", nullable=false)
*/
private $createTimestamp;
/**
* @var Accounts
*
* @ORM\ManyToOne(targetEntity="Accounts")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="create_account_fk", referencedColumnName="id")
* })
*/
private $createAccountFk;
// getters and setters ...
/**
* Set createAccountFk
*
* @param Vendor\IndexBundle\Entity\Accounts $createAccountFk
* @return EngineCodes
*/
public function setCreateAccountFk(\Vendor\IndexBundle\Entity\Accounts $createAccountFk = null)
{
$this->createAccountFk = $createAccountFk;
return $this;
}
/**
* @ORM\PrePersist
*/
public function setCreateTimestampValue()
{
$this->createTimestamp = new \DateTime();
}
}
+1用於查找數組錯誤。 – gremo
Ahhhh,不應用我的數組實體的形式是完全合理的。感謝您的觀察。 –
現在看來我的query_builder語句不是返回數組/對象,而是整數和字符串。必須在早晨重新用新鮮的眼睛重新審視。 –