Home > Enterprise >  My edit (CRUD) function remove some DATA when I use it
My edit (CRUD) function remove some DATA when I use it

Time:11-28

i've a problem with symfony project, in my Controller Company when I use edit function to modif some DATA with embedded form, symfony keep only 1 customer (first one) and what I was change and remove all other DATA.

I not understand why symfony do that.

Thank U for Ure help

Best regards

Try to use edit function in symfony with embedded form

CodePudding user response:

Here's screenshot of Controller entities views and log.

Company Entity

<?php


#[ORM\Column(length: 100)]
private ?string $name = null;

#[ORM\Column(length: 50)]
private ?string $phone = null;

#[ORM\Column(length: 100)]
private ?string $location = null;

#[ORM\Column(length: 255, unique: true)]
#[Gedmo\Slug(fields: ['name'])]
private ?string $slug = null;

#[ORM\OneToMany(mappedBy: 'company', targetEntity: Customer::class, orphanRemoval:true, cascade: ['persist', 'all'])]
private Collection $customers;

#[ORM\OneToMany(mappedBy: 'company', targetEntity: Network::class, orphanRemoval:true, cascade: ['persist', 'all'])]
private Collection $networks;

#[ORM\OneToMany(mappedBy: 'company', targetEntity: Process::class, orphanRemoval:true, cascade: ['persist', 'all'])]
private Collection $processes;

#[ORM\ManyToOne(inversedBy: 'companies')]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;


public function __construct()
{
    $this->customers = new ArrayCollection();
    $this->networks = new ArrayCollection();
    $this->processes = new ArrayCollection();
}

public function getId(): ?int
{
    return $this->id;
}

public function getName(): ?string
{
    return $this->name;
}

public function setName(string $name): self
{
    $this->name = $name;

    return $this;
}

public function getPhone(): ?string
{
    return $this->phone;
}

public function setPhone(string $phone): self
{
    $this->phone = $phone;

    return $this;
}

public function getLocation(): ?string
{
    return $this->location;
}

public function setLocation(string $location): self
{
    $this->location = $location;

    return $this;
}

/**
 * @return Collection<int, Customer>
 */
public function getCustomers(): Collection
{
    return $this->customers;
}

public function addCustomer(Customer $customer): self
{
    if (!$this->customers->contains($customer)) {
        $this->customers->add($customer);
        $customer->setCompany($this);
    }

    return $this;
}

public function removeCustomer(Customer $customer): self
{
    if ($this->customers->removeElement($customer)) {
        // set the owning side to null (unless already changed)
        if ($customer->getCompany() === $this) {
            $customer->setCompany(null);
        }
    }

    return $this;
}

/**
 * @return Collection<int, Network>
 */
public function getNetworks(): Collection
{
    return $this->networks;
}

public function addNetwork(Network $network): self
{
    if (!$this->networks->contains($network)) {
        $this->networks->add($network);
        $network->setCompany($this);
    }

    return $this;
}

public function removeNetwork(Network $network): self
{
    if ($this->networks->removeElement($network)) {
        // set the owning side to null (unless already changed)
        if ($network->getCompany() === $this) {
            $network->setCompany(null);
        }
    }

    return $this;
}

/**
 * @return Collection<int, Process>
 */
public function getProcesses(): Collection
{
    return $this->processes;
}

public function addProcess(Process $process): self
{
    if (!$this->processes->contains($process)) {
        $this->processes->add($process);
        $process->setCompany($this);
    }

    return $this;
}

public function removeProcess(Process $process): self
{
    if ($this->processes->removeElement($process)) {
        // set the owning side to null (unless already changed)
        if ($process->getCompany() === $this) {
            $process->setCompany(null);
        }
    }

    return $this;
}

public function getSlug(): ?string
{
    return $this->slug;
}

public function setSlug(string $slug): self
{
    $this->slug = $slug;

    return $this;
}

public function getUser(): ?User
{
    return $this->user;
}

public function setUser(?User $user): self
{
    $this->user = $user;

    return $this;
}

}

CodePudding user response:

Company Controller

<?php

#[Route('/company/add', name: 'company_add', methods: ['GET', 'POST'])]
#[Route('/company/edit/{slug}', name: 'company_edit', methods: ['GET', 'POST'])]
public function addOrEdit(Company $company=null, Request $request, EntityManagerInterface $em): Response
{
    $user = $this->getUser();

    // $this->denyAccessUnlessGranted('IS_AUTHETICATED_FULLY');
    if (is_null($company)) {
        $company = new Company();
        $message = 'Société ajouté avec succès';
        $messageType = 'success';
    }
    $message = 'Société modifié avec succès';
    $messageType = 'info';

    $form = $this->createForm(CompanyType::class, $company);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) { 
        // dd($form);
        // Ajout de Contacts
        foreach ($company->getCustomers() as $customer) {
            $customer->setIsActive(true);
        }
        $em->persist($company->setUser($user));
        $em->flush();

        $this->addFlash($messageType, $message);

        return $this->redirectToRoute('company');
    }

    return $this->renderForm('company/add_or_edit.html.twig', [
        'company' => $company,
        'form' => $form,
    ]);
}

#[Route('/company/{id}', name: 'company_show', methods: ['GET'])]
public function show(Company $company): Response
{
    return $this->render('company/show.html.twig', [
        'company' => $company,
    ]);
}

}

CodePudding user response:

JS

const addFormToCollection = (e) => {
const collectionHolder = document.querySelector('.'   e.currentTarget.dataset.collectionHolderClass);

const item = document.createElement('li');

item.innerHTML = collectionHolder
.dataset
.prototype
.replace(
    /__name__/g,
    collectionHolder.dataset.index
    );
    
    collectionHolder.appendChild(item);
    
    collectionHolder.dataset.index  ;
    
    addTagFormDeleteLink(item);
    
};
const addTagFormDeleteLink = (item) => {
    const removeFormButton = document.createElement('button');
    removeFormButton.innerText = 'Supprimer ';
    
    item.append(removeFormButton);
    
    removeFormButton.addEventListener('click', (e) => {
        e.preventDefault();
        // remove the li for the tag form
            item.remove();
        });
    }
  • Related