简体   繁体   中英

Persisting Collection of Forms in a ManyToOne/OneToMany association

I have read and re-read everywhere I could, and I am unable to find the proper answer to my problem. Here is the problem. I have 2 entities. Equipment, EquipmentPart respectively.

Equipment:

/**
 * @ORM\OneToMany(
 *     targetEntity="EquipmentPart",
 *     mappedBy="equipment",
 *     orphanRemoval=true,
 *     cascade={"persist"})
 */

private $parts;

public function __construct()
{
    $this->parts = new ArrayCollection();
} 
... getters and setters...

...
public function getParts()
{
    return $this->parts;
}

public function setParts($parts)
{
    $this->parts = $parts;
}

EquipmentPart:

/**
 * @ORM\ManyToOne(targetEntity="Equipment", inversedBy="parts")
 * @ORM\JoinColumn(nullable=false)
 */
private $equipment;

...getters and setters
...

public function getEquipment()
{
    return $this->equipment;
}

public function setEquipment(Equipment $equipment)
{
    $this->equipment = $equipment;
}

Equipment Form:

$builder->add("parts", CollectionType::class, [
                "label" => false,
                "entry_type" => EquipmentPartForm::class,
                "allow_delete" => true,
                "allow_add" => true,
                "by_reference" => false

            ]);

The equipmentPart form has just partnumber and name as fields. I keep getting a constraint error due the FK is null.

My controller is pretty simple:

public function addEquipment(Request $request)
{
$equipment = new Equipment();
$form = $this->createForm(EquipmentForm::class, $equipment);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()){
        $em = $this->getDoctrine()->getManager();
        $em->persist($form->getData());
        $em->flush();
 ...
 ...

What am I doing wrong?

you forget something. Add the EquipmentPart Entity to the Equipment entity.

//EquipmentEntity
public function addEquipmentPart(EquipmentPart $equipmentPart)
{
    $this->parts[] = $equipmentPart;
    $equipmentPart->setEquipment($this);
}

In your form, the option "by_reference" => false will call this function see doc

I hope that I help you

After countless hours looking for an answer, I finally found one. The reason I was getting either the constraint error, or the "could not determine access type for property" (when deleting the setParts setter) was because symfony was unable to find my adders and remover methods.

For some strange reason, symfony will not work or find the adder or remover methods if they don't have the proper name. Like so,

* public function addEquipmentPart(...)
* public function addEquipmentParts(...)
* public function addParts(...)

Will not work .

Keep in mind that (in this case), regardless of my property $parts, the method has be singular.

The proper methods are:

public function addPart(EquipmentPart $equipmentPart){
    $equipmentPart->setEquipment($this);
    $this->parts[] = $equipmentPart;
}

public function removePart(EquipmentPart $equipmentPart){
    $this->parts->removeElement($equipmentPart);
}

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