Objects passed to the selection field must be managed

I am looking for answers from the Internet, but I can not find the reason: I have a dining company, companyType table, 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 try to use the form:

   {{ form_row(company.companyType)  }}  

In the view, I received an error message.

+3
source share
2 answers

I will find out the reason because I am making a companyType instance in the company for the form. It means:

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

That is why this is an exception. I do not have to initialize any corporate type for the form. because I have to choose it.
Thanks for trying to help. Hope this helps someone.

+6
source

, CompanyType, , EntityManager, .

, CompanyType AcmeUserBundle?

0

All Articles