I'm getting the following error when trying to edit a previously persisted Mail entity :
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__\Mail\Bundle\Entity\Departement. You can avoid this error by setting the "data_class" option to "Proxies__CG__\Mail\Bundle\Entity\Departement" or by adding a view transformer that transforms an instance of class Proxies__CG__\Mail\Bundle\Entity\Departement to scalar, array or an instance of \ArrayAccess.
But sometimes, another one shows up :
The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class Mail\UserBundle\Entity\MailUser. You can avoid this error by setting the "data_class" option to "Mail\UserBundle\Entity\MailUser" or by adding a view transformer that transforms an instance of class Mail\UserBundle\Entity\MailUser to scalar, array or an instance of \ArrayAccess.
Basically, I have 3 entities involved in the problem : a MailEntity, a MailUserEntity and a DepartementEntity.
Here is my Mail Entity :
<?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;
}
}
and yml version (the one I use), if prefered :
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: { }
And here is its Form Type :
<?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;
}
}
Mail\Bundle\Entity
and notMail\Bundle\MailBundle\Entity
– Geoffrey Brier