ferdinandfly
ferdinandfly

Reputation: 371

Entities passed to the choice field must be managed

i search the answers from internet but i can not find a reason: i have a table company, a table companyType, so :

/**
 * Acme\UserBundle\Entity\Company
 *
 * @ORM\Table(name="company")
 * @ORM\Entity
 */
class Company
{
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;
/**
 * @var CompanyType
 *
 * @ORM\ManyToOne(targetEntity="CompanyType")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="company_type_id", referencedColumnName="id")
 * })
 */
private $companyType;
...

}

/**
* Acme\UserBundle\Entity\CompanyType
*
* @ORM\Table(name="company_type")
* @ORM\Entity
*/
class CompanyType
{
 /**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;
/**
 * @var string $name
 *
 * @ORM\Column(name="name", type="string", length=45, nullable=true)
 */
private $name;
....
public function __toString(){
    return $this->name;
}

}
and then , in the formtype class:

class CompanyForm extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
    $builder
        ->add('name')
        ->add('siren')
        ->add('siret')
        ->add('tvaCommun')
        ->add('apeCode')
    ;
    $builder->add('activity','collection', array('type'=> new ActivityForm()));      
    $builder->add('companyType','entity',array(
            'class' => 'AcmeUserBundle:CompanyType',
    ));
   }
...
}  

when i trying to use the form:

   {{ form_row(company.companyType)  }}  

in the view, i got the error message.

Upvotes: 3

Views: 12445

Answers (2)

ferdinandfly
ferdinandfly

Reputation: 371

I have find out the reason, cause i make a instance of companyType in company for the form. Which means:

$cType=new CompanyType();
$company=new Company();
$company->getCompanyTypes()->add($cType);
$cForm=$this->createFrom(new CompanyForm(),$company);

This is the reason why it throw this exception. I should not initialize any companyType for the form. cause i need to choose it.
Thanks for who try to help. Hope this can help somebody.

Upvotes: 6

AlterPHP
AlterPHP

Reputation: 12727

This error means that CompanyType objects passed to the entity field must be managed by the EntityManager, ie must be persisted to your database through the entity manager.

Are you sure that CompanyType entity is stored in the AcmeUserBundle ?

Upvotes: 0

Related Questions