Doctrine smazání dat při editaci

před 4 měsíci

Chuchy
Člen | 48
+
0
-

Ahoj,
potřeboval bych radu, mám formulář v něm mám multiselectbox, při vytváření se mi všechno uložení správně, ale jakmile odeberu některou vybranou položku s multiselectboxu, tak v tabulce se mi nesmaže. Může mi někdo poradit, kde mám problém?

entity FormAgreement

/**
 * @var Form
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="App\AdminModule\Doctrine\Entities\Form\Form", inversedBy="id", cascade={"persist"})
 * @ORM\JoinColumn(name="form_id", referencedColumnName="id")
 */
private $form;

/**
 * @var Agreement
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="App\AdminModule\Doctrine\Entities\Agreement\Agreement", inversedBy="id", cascade={"persist","remove","merge","detach"})
 * @ORM\JoinColumn(name="agreement_id", referencedColumnName="id")
 */
private $agreement;

agreement

/**
 * @var FormAgreement[]
 * @ORM\OneToMany(targetEntity="App\AdminModule\Doctrine\Entities\FormAgreement\FormAgreement", mappedBy="agreement", cascade={"persist","remove","merge","detach"})
 */
private $formAgreements;

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

/**
 * @param FormAgreement $formAgreement
 */
public function addFormAgreement(FormAgreement $formAgreement)
{
    $formAgreement->setAgreement($this);
    $this->formAgreements->add($formAgreement);
}

ukládání

public function saveAgreement(array $values, string $agreementType, ?int $id): ?Agreement
{
    $agreement = $this->agreementFactory->create();
    $agreement->setType($agreementType);
    $agreement->setTitle($values["title"]);
    $agreement->setDescription($values["description"]);
    $agreement->setDescriptionFull($values["description_full"]);
    $agreement->setCompulsory($values["compulsory"]);
    switch ($agreementType) {
        case AgreementTypeEnum::COMMON:
            $agreement->setDisplayCompetition($values["display_competition"]);
            $agreement->setDisplayNonCompetition($values["display_non_competition"]);
            break;
        case AgreementTypeEnum::SPECIFIC:
            foreach ($this->formFacade->getAllForms() as $form) {
                $formAgreement = $this->formAgreementFactory->create();
                $formAgreement->setForm($form);
                $agreement->addFormAgreement($formAgreement);
            }
            break;
    }

    if ($id === null) {
        $this->agreementFacade->save($agreement);
    } else {
        $agreement->setId($id);
        $this->agreementFacade->update($agreement);
    }

    retu

před 4 měsíci

MajklNajt
Člen | 221
+
0
-

pretože ju pri update nezmažeš… buď si pred ukladaním urob diff, kde odobraté položky vyhodíš z kolekcie pomocou Collection::removeElement(), alebo si pri každom update zavolaj Collection::clear() a nahádž tam aktuálne položky

Editoval MajklNajt (14. 4. 19:04)

před 4 měsíci

Chuchy
Člen | 48
+
0
-

OK, upravil jsem to do takovéto podoby, nicméně si nejsem jist, zda je to správně použité?

/**
 * @param array $values
 * @param string $agreementType
 * @param Agreement|null $agreement
 * @return Agreement|null
 * @throws ORMException
 * @throws ORMInvalidArgumentException
 * @throws OptimisticLockException
 */
public function saveAgreement(array $values, string $agreementType, ?Agreement $agreement): ?Agreement
{
    $id = null;
    if ($agreement === null) {
        $agreement = $this->agreementFactory->create();
    } else {
        $id = $agreement->getId();
    }

    $agreement->setType($agreementType);
    $agreement->setTitle($values["title"]);
    $agreement->setDescription($values["description"]);
    $agreement->setDescriptionFull($values["description_full"]);
    $agreement->setCompulsory($values["compulsory"]);
    switch ($agreementType) {
        case AgreementTypeEnum::COMMON:
            $agreement->setDisplayCompetition($values["display_competition"]);
            $agreement->setDisplayNonCompetition($values["display_non_competition"]);
            break;
        case AgreementTypeEnum::SPECIFIC:
            foreach ($agreement->getFormAgreements() as $formAgreement) {
                $agreement->removeFormAgreement($formAgreement);
                $this->formAgreementFacade->update($formAgreement);
            }

            foreach ($this->formFacade->getByIds($values["forms"]) as $form) {
                $formAgreement = $this->formAgreementFactory->create();
                $formAgreement->setForm($form);
                $agreement->addFormAgreement($formAgreement);
            }
            break;
    }

    if ($id === null) {
        $this->agreementFacade->save($agreement);
    } else {
        $agreement->setId($id);
        $this->agreementFacade->update($agreement);
    }

    return $agreement;
}

Editoval Chuchy (14. 4. 21:24)