简体   繁体   中英

Symfony - Form for Three Related Entities

I have three Entities with relationships as follows: Can --> Jobcan <-- Job

Jobcan has ManyToOne relationships with both Can and Job.

I build a JobType form as follows:

    $builder
        ->add('name','text')
        ->add('schedule','text')
        ->add('jobcans', 'entity',array(
            'class' => 'RoelabGusBundle:Can',
            'property' => 'name',
            'multiple' => 'true',
            'expanded' => true,))
        ->add('save','submit')
    ;

The form renders as I expect where a checkbox is available for each Can in the database. However, when the form is submitted the following exception is thrown: Found entity of type Roelab\\GusBundle\\Entity\\Can on association Roelab\\GusBundle\\Entity\\Job#jobcans, but expecting Roelab\\GusBundle\\Entity\\Jobcan

Which is understandable, as the Job Entity is looking for a Jobcan:

    class Job
    {
        protected $jobcans;

        public function __construct()
        {
            $this->jobcans = new ArrayCollection();
        }

Ultimately, I want to grab the id of the selected Can entities and create Jobcan objects/rows for each selected Can.

I've tried setting the form field up as plain old choice, populated by building an array from querying the Can table, but got some error about not being able to convert an int (sorry, don't have exact error message).

Am sure this is probably quite simple, but new to Symfony and OOP, so apologies in advance.

Probably the problem is in the type hinting i think. If you have done some refactoring recently(changed the class name from "Can" to "Jobcan"), check that entity methods is pointing to the correct class or check the entity definition (yml or annotation) and generate entities again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM