尝试编辑以前保留的邮件实体时出现以下错误:
表单的视图数据应该是标量,数组或\ ArrayAccess的实例,但是类Proxies__CG __ \ Mail \ Bundle \ Entity \ Departement的实例。 您可以通过将“data_class”选项设置为“Proxies _CG __ \ Mail \ Bundle \ Entity \ Departement”或添加一个视图转换器来将类Proxies__CG __ \ Mail \ Bundle \ Entity \ Departement的实例转换为标量,数组或\ ArrayAccess的实例。
但有时,另一个出现:
表单的视图数据应该是标量,数组或\ ArrayAccess的实例,但是类Mail \ UserBundle \ Entity \ MailUser的实例。您可以通过将“data_class”选项设置为“Mail \ UserBundle \ Entity \ MailUser”或通过添加视图转换器来将此问题转移到“标记”,“数组”或“实例”的实例,该视图转换器将类Mail \ UserBundle \ Entity \ MailUser的实例转换为ArrayAccess接口。
基本上,我有3个实体参与了这个问题:MailEntity,MailUserEntity和DepartementEntity。
这是我的邮件实体:
<?php
namespace Mail\Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Mail
*/
class Mail {
/**
* @var integer
*/
private $id;
/**
* @var \Mail\Bundle\Entity\Departement
*/
private $departement;
/**
* @var \Mail\UserBundle\Entity\MailUser
*/
private $mailUser;
/**
* @var \Mail\UserBundle\Entity\MailUser
*/
private $modifUser;
// Some more variables & Getters / Setters
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Constructor
*/
public function __construct() {
}
/**
* Set departement
*
* @param \Mail\Bundle\Entity\Departement $departement
* @return Mail
*/
public function setDepartement(
\Mail\Bundle\Entity\Departement $departement = null) {
$this->departement = $departement;
return $this;
}
/**
* Get departement
*
* @return \Mail\Bundle\Entity\Departement
*/
public function getDepartement() {
return $this->departement;
}
/**
* Set mailUser
*
* @param \Mail\UserBundle\Entity\MailUser $mailUser
* @return Mail
*/
public function setMailUser(
\Mail\UserBundle\Entity\MailUser $mailUser = null) {
$this->mailUser = $mailUser;
return $this;
}
/**
* Get mailUser
*
* @return \Mail\UserBundle\Entity\MailUser
*/
public function getMailUser() {
return $this->mailUser;
}
/**
* Set modifUser
*
* @param \Mail\UserBundle\Entity\MailUser $modifUser
* @return Mail
*/
public function setModifUser(
\Mail\UserBundle\Entity\MailUser $modifUser = null) {
$this->modifUser = $modifUser;
return $this;
}
/**
* Get modifUser
*
* @return \Mail\UserBundle\Entity\MailUser
*/
public function getModifUser() {
return $this->modifUser;
}
}
和yml版本(我使用的版本),如果首选:
Mail\Bundle\Entity\Mail:
type: entity
table: null
repositoryClass: Mail\Bundle\Repository\MailRepository
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
manyToOne:
departement:
targetEntity: Mail\Bundle\Entity\Departement
joinColumns:
departement_id:
referencedColumnName: id
orphanRemoval: false
mailUser:
targetEntity: Mail\UserBundle\Entity\MailUser
joinColumns:
mail_user_id:
referencedColumnName: id
orphanRemoval: false
modifUser:
targetEntity: Mail\UserBundle\Entity\MailUser
joinColumns:
modif_user_id:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
这是表格类型:
<?php
namespace Mail\Bundle\Form;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Security\Core\SecurityContext;
class MailType extends AbstractType
{
protected $userId;
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$userId = $this->userId;
$builder
// Some other fields ...
->add('departement', 'hidden', array(
'label' => 'Departement',
'required' => false,
'disabled' => true))
->add('mailUser', 'entity', array(
'class' => 'MailUserBundle:MailUser',
'query_builder' => function(EntityRepository $ermu) {
return $ermu->createQueryBuilder('MU')
->orderBy('MU.lastName', 'ASC');
},
'multiple' => false,
'required' => true,
'label' => 'Sender'))
->add('modifUser', 'hidden', array(
'label' => 'Editor'))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Mail\Bundle\Entity\Mail'
));
}
/**
* @return string
*/
public function getName()
{
return 'mail_bundle_courriertype';
}
public function __construct($userId)
{
$this->userId = $userId;
}
}
答案 0 :(得分:1)
为什么要在表单中添加部门作为隐藏字段?尝试删除它,因为它不是必需的。您已经在实体中获得了它,并且可以在您的控制器中设置/操作它。你的表格中没有必要(也许你来自sf1.4?)....