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

后端 未结 2 1210
没有蜡笔的小新
没有蜡笔的小新 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) ;
        }
    }
    

提交回复
热议问题