我在尝试更新实体时遇到以下异常:
The form's view data is expected to be of type scalar, array or an instance of 'ArrayAccess, but is an instance of class Proxies__CG__'Acme'DemoBundle'Entity'TicketCategory. You can avoid this error by setting the "data_class" option to "Proxies__CG__'Acme'DemoBundle'Entity'TicketCategory" or by adding a view transformer that transforms an instance of class Proxies__CG__'Acme'DemoBundle'Entity'TicketCategory to scalar, array or an instance of 'ArrayAccess.
创建时,不会出现问题,关系正常。但是,在更新时,会出现这种奇怪的异常。我的实体设置如下:
class Ticket
{
// ...
/**
* @var TicketCategory
*
* @ORM'ManyToOne(targetEntity="TicketCategory")
* @ORM'JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
// ...
}
class TicketCategory
{
/**
* @var integer $id
*
* @ORM'Column(name="id", type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $title
*
* @ORM'Column(name="title", type="string", length=255)
* @Assert'NotBlank()
*/
private $title;
// ...
}
形式
class TicketType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text', array(
'error_bubbling' => true,
)
)
->add('category', 'text', array(
'error_bubbling' => true,
)
)
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme'DemoBundle'Entity'Ticket',
'csrf_protection' => false,
));
}
public function getName()
{
return '';
}
}
有什么想法吗,伙计们?
问题是:
$builder
->add('category', 'text', array(
'error_bubbling' => true,
)
)
;
字段 category
声明为"text"类型,因此您只能向其传递标量(字符串、布尔值等)。也就是说,您只能指定标量属性(Ticket
类)。
在类category
Ticket
它是一个实体,因此会发生错误。
在不知道你想完成什么的情况下,我猜你想让用户为工单选择一个类别,所以我会做:
$builder
->add('category', 'entity', array(
'label' => 'Assign a category',
'class' => 'Acme'HelloBundle'Entity'TicketCategory',
'property' => 'title',
'multiple' => false
)
)
;
详细了解实体字段类型。
编辑:不知道你是否省略了它,但Ticket
没有名为"title"的属性。