Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entities passed to the choice field must be managed

Tags:

symfony

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.

like image 425
ferdinandfly Avatar asked Dec 13 '25 15:12

ferdinandfly


1 Answers

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.

like image 82
ferdinandfly Avatar answered Dec 16 '25 23:12

ferdinandfly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!