set multiple='false' in a form in a many to many relation symfony2

后端 未结 2 1208
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 03:41

I have a many-to-many relationship between two entities A and B.

So when adding a form, in order to add entityA to entityB, I am doing the

相关标签:
2条回答
  • 2020-12-06 04:20

    In EntityA, you have something like this, right?

    public function setEntitiesB($data)
    {
        $this->entitiesB = $data ;
    }
    

    Now because you can also receive single value instead of array of values, you need something like this:

    public function setEntitiesB($data)
    {
        if ( is_array($data) ) {
            $this->entitiesB = $data ;
        } else {
            $this->entitiesB->clear() ;
            $this->entitiesB->add($data) ;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 04:30

    i would check the entityA value in the controller and depending on it create different forms.

    in controller:

    if ($entityA->getType() == 'a') { 
        $form = new FormB(); // form with multiple true
    } else {
        $form = new FormA(); // form with multiple false
    }
    
    0 讨论(0)
提交回复
热议问题