Symfony在Bridge Entity Form中同时分配多个值 [英] Symfony Assign multiple values at once in Bridge Entity Form

查看:103
本文介绍了Symfony在Bridge Entity Form中同时分配多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



学院之间的一对多 - > InstituteCourses 课程之间的一对多 - > InstituteCourses



我已经避免了在许多课程和课程之间做许多事情,因为我在实体实体中有更多的领域。



现在它工作得很好,

  class Institutes {
/ **
* @ ORM\OneToMany(targetEntity =InstitutesCourses,mappedBy =institute cascade = {all})
** /
protected $ instituteCourses;
}
class课程{
/ **
*
* @ ORM\OneToMany(targetEntity =InstitutesCourses,mappedBy =course,cascade = { all},orphanRemoval = true)
* /
protected $ instituteCourses;
}

class InstitutesCourses {
/ **
* @ ORM\ManyToOne(targetEntity =Institutes,inversedBy =instituteCourses)
* @ ORM\JoinColumn(name =institute_id,referencedColumnName =id)
* /
protected $ institute;

/ **
* @ ORM\ManyToOne(targetEntity =Courses,inversedBy =instituteCourses)
* @ ORM\JoinColumn(name =course_id ,referencedColumnName =id)
* /
protected $ course;
/ **
* @var boolean $ isPrimary
* @ ORM\Column(name =is_primary,type =boolean,nullable = true)
*
protected $ isPrimary;
/ **
* @var boolean $ verified
* @ ORM\Column(name =courses_verified,type =boolean,nullable = true)
*
protected $ verified;
/ **
* @var boolean $ active
* @ ORM\Column(name =courses_active,type =boolean,nullable = true)
*
protected $ active;
/ **
*
* @ ORM\OneToMany(targetEntity =AcademicSessions,mappedBy =instituteCourse)
* /
protected $ academicSession;
}

,表单为

  $ builder-> add('institute','entity',array(
'class'=>'PNC\InstitutesBundle\Entity\Institutes ',
'property'=>'name',
'label'=>'学院'


- > add('course ','entity',array(
'class'=>'PNC\CoursesBundle\Entity\Courses',
'property'=>'courseTitle',
/ /'multiple'=> true,
'attr'=>数组(
'class'=>'form-control'

))

现在,如果我想分配一个机构多个课程,我必须多次为所有新的动作,我想要的单次提交多个课程位于一所学院。当我在表单中的课程字段中设置multiple = true时。它给出错误。



更新 -

  class Institutes {

/ **
* @ ORM\OneToMany(targetEntity =NC\InstitutesBundle\Entity\InstitutesCourses,mappedBy =institute,cascade = {all})
* * /
protected $ inst;
保护$课程;


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

//重要
public function getCourses()
{
$ courses = new ArrayCollection();

foreach($ this-> inst as $ ins)
{
$ courses [] = $ ins-> getCourses();
}
返回$课程;
}

//重要
public function setCourses($ courses)
{


foreach($ courses as $ c)
{
$ po = new InstitutesCourses();
$ po-> setCourse($ c);
$ po-> setInstitute($ this);
$ this-> addInst($ po);
}

}

//重要
public function getCourse()
{
return $ this;
}
//重要
public function addInst($ InstitutesCourses)
{
$ this-> inst [] = $ InstitutesCourses;
}
//重要
public function removeIns($ InstitutesCourses)
{
return $ this-> inst-> removeElement($ InstitutesCourses);
}

}

 课程课程{
/ **
*
* @ ORM\OneToMany(targetEntity =NC\InstitutesBundle\Entity \InstitutesCourses,mappedBy =course,cascade = {all},orphanRemoval = true)
* /
protected $ instituteCourses;

/ **
* @return mixed
* /
public function getInstituteCourses()
{
return $ this-> instituteCourses ;
}

/ **
* @param mixed $ instituteCourses
* /
public function setInstituteCourses($ instituteCourses)
{
$ this-> instituteCourses = $ instituteCourses;
}
}

和桥梁实体。

  class InstitutesCourses {

/ **
* @ ORM\ManyToOne(targetEntity =PNC\InstitutesBundle \Entity\Institutes,inversedBy =instituteCourses)
* @ ORM\JoinColumn(name =institute_id,referencedColumnName =id)
* /
protected $ institute ;
/ **
* @ ORM\ManyToOne(targetEntity =PNC\CoursesBundle\Entity\Courses,inversedBy =instituteCourses)
* @ ORM\JoinColumn name =course_id,referencedColumnName =id)
* /
protected $ course;
/ **
* @return mixed
* /
public function getInstitute()
{
return $ this-> institute;
}

/ **
* @param mixed $ Institute
* /
public function setInstitute($ institute)
{
$ this-> institute = $ institute;
}

/ **
* @return mixed
* /
public function getCourse()
{
return $这 - >当然;
}
/ **
* @param mixed $ course
* /
public function setCourse($ course)
{
$ this - > course = $ course;
}

}

我做了一种表单类型,

   - > add('courses','entity',array(
'class'=> 'PNCCoursesBundle:Courses',
'property'=>'courseTitle',
//'expanded'=> true,
'multiple'=> true,
'attr'=> array(
'class'=>'form-control',
),
'label_attr'=>数组(
'class'= "'control-label'
),

转到创建新文件完美保存记录,当我更新相同的记录时。


尝试调用名为你的意思是调用
,例如getCourse或setCourse?



解决方案

你尝试混合两种不同的方法
Fir st:在研究所和课程之间建立许多关系,为学院创建表格实体,课程是收集。在这种方法中,原则在每个课程的InstitutesCourses表中创建记录。
第二:您为InstitutesCourses Entity创建表单。一个形式 - 一个实体。在这种情况下,您必须使用解决方案之一。其中一个:
1.在表单类型中,将'课程'设为不映射字段('mapped'= false)
2.在控制器中提交表单获取课程数组

  $ courses = $ form-> get('courses') - > getData(); 




  1. 用户手动创建InstitutesCourses的课程:



    foreach($ courses as $ course){
    $ InstitutesCourse = new InstitutesCourses();
    $ InstitutesCourse-> setCourse($ course)
    - > setInstitutes($ institute);
    $ em - > $ persist($ InstitutesCourse);
    }
    $ em-> flush();



I have below relationship between my entities.

One to many between Institute -> InstituteCourses. One to many between Courses -> InstituteCourses.

I have avoid making many to many between instiutes and courses because i have alot more fields in instiutecourses entity.

For now it is working well,

    class Institutes {
        /**
        * @ORM\OneToMany(targetEntity="InstitutesCourses", mappedBy="institute", cascade={"all"})
        **/
        protected $instituteCourses;
    }
    class Courses {
        /**
         *
         * @ORM\OneToMany(targetEntity="InstitutesCourses", mappedBy="course", cascade={"all"}, orphanRemoval=true)
         */
        protected $instituteCourses;
    }

    class InstitutesCourses {
        /**
         * @ORM\ManyToOne(targetEntity="Institutes", inversedBy="instituteCourses")
         * @ORM\JoinColumn(name="institute_id", referencedColumnName="id")
         */
        protected $institute;

        /**
         * @ORM\ManyToOne(targetEntity="Courses", inversedBy="instituteCourses")
         * @ORM\JoinColumn(name="course_id", referencedColumnName="id")
         */
        protected $course;
  /**
     * @var boolean $isPrimary
     * @ORM\Column(name="is_primary", type="boolean", nullable=true)
     */
    protected $isPrimary;
   /**
     * @var boolean $verified
     * @ORM\Column(name="courses_verified", type="boolean", nullable=true)
     */
    protected $verified;
    /**
     * @var boolean $active
     * @ORM\Column(name="courses_active", type="boolean", nullable=true)
     */
    protected $active;
    /**
     *
     * @ORM\OneToMany(targetEntity="AcademicSessions", mappedBy="instituteCourse")
     */
    protected $academicSession;
    }

and the form is

 $builder->add('institute','entity', array(
                    'class'=>'PNC\InstitutesBundle\Entity\Institutes',
                    'property'=>'name',
                    'label' => 'Institute'
                )
            )
            ->add('course', 'entity', array(
                'class' => 'PNC\CoursesBundle\Entity\Courses',
                'property' => 'courseTitle',
                //'multiple' => true,
                'attr' => array(
                    'class' => 'form-control'
                )
            ))

Now if i want to assign one institute mulitple courses i have to all new Action several time, what i wanted to in single submit multiple courses allocated to one institute. when i set multiple = true in courses field in form. it gives error.

UPDATE -

class Institutes {

  /**
     * @ORM\OneToMany(targetEntity="NC\InstitutesBundle\Entity\InstitutesCourses", mappedBy="institute", cascade={"all"})
     * */
    protected $inst;
    protected $courses;


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

    // Important
    public function getCourses()
    {
        $courses = new ArrayCollection();

        foreach($this->inst as $ins)
        {
            $courses[] = $ins->getCourses();
        }
        return $courses;
    }

    // Important
    public function setCourses($courses)
    {


        foreach($courses as $c)
        {
            $po = new InstitutesCourses();
            $po->setCourse($c);
            $po->setInstitute($this);
            $this->addInst($po);
        }

    }

    // Important
    public function getCourse()
    {
        return $this;
    }
    // Important
    public function addInst($InstitutesCourses)
    {
        $this->inst[] = $InstitutesCourses;
    }
    // Important
    public function removeIns($InstitutesCourses)
    {
        return $this->inst->removeElement($InstitutesCourses);
    }

} And

class Courses {
    /**
     *
     * @ORM\OneToMany(targetEntity="NC\InstitutesBundle\Entity\InstitutesCourses", mappedBy="course", cascade={"all"}, orphanRemoval=true)
     */
    protected $instituteCourses;

    /**
     * @return mixed
     */
    public function getInstituteCourses()
    {
        return $this->instituteCourses;
    }

    /**
     * @param mixed $instituteCourses
     */
    public function setInstituteCourses($instituteCourses)
    {
        $this->instituteCourses = $instituteCourses;
    }
}

and the bridge entity.

class InstitutesCourses {

/**
     * @ORM\ManyToOne(targetEntity="PNC\InstitutesBundle\Entity\Institutes", inversedBy="instituteCourses")
     * @ORM\JoinColumn(name="institute_id", referencedColumnName="id")
     */
    protected $institute;
    /**
     * @ORM\ManyToOne(targetEntity="PNC\CoursesBundle\Entity\Courses", inversedBy="instituteCourses")
     * @ORM\JoinColumn(name="course_id", referencedColumnName="id")
     */
    protected $course;
 /**
     * @return mixed
     */
    public function getInstitute()
    {
        return $this->institute;
    }

    /**
     * @param mixed $institute
     */
    public function setInstitute($institute)
    {
        $this->institute = $institute;
    }

    /**
     * @return mixed
     */
    public function getCourse()
    {
        return $this->course;
    }
    /**
     * @param mixed $course
     */
    public function setCourse( $course)
    {
        $this->course = $course;
    }

}

I made a form of institute type,

->add('courses' , 'entity' , array(
'class'    => 'PNCCoursesBundle:Courses' ,
'property' => 'courseTitle' ,
//'expanded' => true ,
'multiple' => true ,
'attr'=> array(
      'class' => 'form-control',
),
'label_attr' => array(
'class' => 'control-label'
),
)

Now When i Go to Create a New It perfectly saves the record, when I update the same record it says.

Attempted to call an undefined method named "getCourses" of class "NC\InstitutesBundle\Entity\InstitutesCourses". Did you mean to call e.g. "getCourse" or "setCourse"?

解决方案

You try mix two different approach. First: to make many to many relation between institutes and courses. Create form for institutes Entity, where courses is collection. In that approach doctrine create records in InstitutesCourses table for each course byself. Second: You create form for InstitutesCourses Entity. One form - one entity. In this case you must use one of the work around solution. One of them: 1. In Form Type make 'courses' as not mapped field ('mapped'=false) 2. After submitting form get array of 'courses' in Controller

$courses=$form->get('courses')->getData();

  1. Make loop for each course in courses with creating InstitutesCourses manually like that:

    foreach ($courses as $course) { $institutesCourse=new InstitutesCourses(); $institutesCourse->setCourse($course) ->setInstitutes($institute); $em->$persist($institutesCourse); } $em->flush();

这篇关于Symfony在Bridge Entity Form中同时分配多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆