简体   繁体   English

Symfony-三个相关实体的表格

[英]Symfony - Form for Three Related Entities

I have three Entities with relationships as follows: Can --> Jobcan <-- Job 我有三个实体,它们之间的关系如下:Can-> Jobcan <-Job

Jobcan has ManyToOne relationships with both Can and Job. Jobcan与Can和Job都有ManyToOne关系。

I build a JobType form as follows: 我建立一个JobType表单,如下所示:

    $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. 该表单将按我期望的方式呈现,其中数据库中每个Can都有一个可用的复选框。 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 但是,提交表单时,将引发以下异常:在关联Roelab \\ GusBundle \\ Entity \\ Job#jobcans上找到类型为Roelab \\ GusBundle \\ Entity \\ Can的实体,但是期望Roelab \\ GusBundle \\ Entity \\ Jobcan

Which is understandable, as the Job Entity is looking for a Jobcan: 这是可以理解的,因为作业实体正在寻找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. 最终,我想获取选定的Can实体的ID,并为每个选定的Can创建Jobcan对象/行。

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). 我尝试将表单字段设置为普通的旧选择,通过查询Can表的构建来构建数组,但由于无法转换int而遇到一些错误(对不起,没有确切的错误消息)。

Am sure this is probably quite simple, but new to Symfony and OOP, so apologies in advance. 确保这可能很简单,但是对于Symfony和OOP来说是新的,因此提前致歉。

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 如果您最近进行了一些重构(将类名从“ Can”更改为“ Jobcan”),请检查实体方法是否指向正确的类或检查实体定义(yml或注释)并再次生成实体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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