Symfony 4 База данных EntityType не обновлена - PullRequest
0 голосов
/ 25 апреля 2020

У меня проблема с полем формы EntityType.

Сущность "Freelancer" связана с ManyToMany с сущностью "Sapsubdomain". В форме на FreelancerType я добавил «sapsubdomains» как EntityType.

Когда я сохраняю свою форму, все поля правильно сохраняются в базе данных, исключая «sapsubdomains».

Я ожидаю, что будет таблица отношений между «Freelancer» "и" Sapsubdomain "обновлены, но ничего не происходит. У меня нет сообщения об ошибке ...

Спасибо за вашу помощь!

"Freelancer" Entity:

<?php

namespace App\Entity;

/**
 * @ORM\Entity(repositoryClass="App\Repository\FreelancerRepository")
 */
class Freelancer
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $about;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\User", cascade={"persist", "remove"})
     */
    private $Userid;


    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Sapsubdomain", mappedBy="freelancer")
     */
    private $sapsubdomains;

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


    /**
     * @return Collection|Sapsubdomain[]
     */
    public function getSapsubdomains(): Collection
    {
        return $this->sapsubdomains;
    }

    public function addSapsubdomain(Sapsubdomain $sapsubdomain): self
    {
        if (!$this->sapsubdomains->contains($sapsubdomain)) {
            $this->sapsubdomains[] = $sapsubdomain;
            $sapsubdomain->addFreelancer($this);
        }

        return $this;
    }

    public function removeSapsubdomain(Sapsubdomain $sapsubdomain): self
    {
        if ($this->sapsubdomains->contains($sapsubdomain)) {
            $this->sapsubdomains->removeElement($sapsubdomain);
            $sapsubdomain->removeFreelancer($this);
        }

        return $this;
    }




}

"Sapsubdomain" Entity:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\SapsubdomainRepository")
 */
class Sapsubdomain
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $subdomain;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Sapdomain", inversedBy="sapsubdomains")
     * @ORM\JoinColumn(nullable=false)
     */
    private $domain;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\freelancer", inversedBy="sapsubdomains")
     */
    private $freelancer;

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


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

    public function getSubdomain(): ?string
    {
        return $this->subdomain;
    }

    public function setSubdomain(?string $subdomain): self
    {
        $this->subdomain = $subdomain;

        return $this;
    }

    public function getDomain(): ?sapdomain
    {
        return $this->domain;
    }

    public function setDomain(?sapdomain $domain): self
    {
        $this->domain = $domain;

        return $this;
    }

    /**
     * @return Collection|freelancer[]
     */
    public function getFreelancer(): Collection
    {
        return $this->freelancer;
    }

    public function addFreelancer(freelancer $freelancer): self
    {
        if (!$this->freelancer->contains($freelancer)) {
            $this->freelancer[] = $freelancer;
        }

        return $this;
    }

    public function removeFreelancer(freelancer $freelancer): self
    {
        if ($this->freelancer->contains($freelancer)) {
            $this->freelancer->removeElement($freelancer);
        }

        return $this;
    }

}

Форма FreelancerType:

<?php

namespace App\Form;

use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;


class FreelancerType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder

            ->add('sapsubdomains', EntityType::class, array(
                'class' => Sapsubdomain::class,
                'choice_label' => 'subdomain',
                'required' => false,
                'multiple' => true,
                 ))

FreelancerController:

<?php

namespace App\Controller;

/**
* @Route("/freelancer", name="freelancer")
* @Security("is_granted('ROLE_FREELANCER')")
*/
class FreelancerController extends AbstractController
{
    /**
    * @Route("/settings/", name="freelancer_settings")
    */
    public function modifyfreelancesettings(Request $request, FileUploader $fileUploader)
    {
        //On récupére l'utilisateur
        $user = $this->getUser();

        //On recherche le profil freelance du user
        $freelancer = $this->getDoctrine()
        ->getRepository(Freelancer::class)
        ->findOneBy(array('Userid'=>$user));

        // Si le profil freelance n'existe pas pour le user on appel la fonction de création du profil
        if (!$freelancer) {
            $this->createfreelancesettings($request);
        }

        //Sinon on met à jour le profil du freelance
        $form = $this->createForm(FreelancerType::class, $freelancer);
        $form->handleRequest($request);

        //On des données du freelance pour éventuellement les envoyer à TWIG
        $tjm = $form->get('dailyrate')->getData();
        $curr = $form->get('ratecurrency')->getData();

        // On vérifie que le formulaire est soumis

        if ($form->isSubmitted() && $form->isValid()) {


            $freelancer->setUpdateDate(new \DateTime('now'));


            /** @var UploadedFile $pictureFile */
            $pictureFile  = $form['picture']->getData();

                if ($pictureFile ) {
                    $pictureFileName  = $fileUploader->upload($pictureFile);
                    $freelancer->setProfilePicture($pictureFileName);
                }

            //Mise à jour des relations
            $sapsubdomains  = $form['sapsubdomains']->getData();    

            $em = $this->getDoctrine()->getManager();
            $em -> flush();

            $this->addFlash('notice',
                        'Your SAP Profile has been saved !'
                        );

                            return $this->redirect($request->getUri());


        }

        return $this->render('freelancer/settings.html.twig', array(
            'picture' => $freelancer,
            'curr' => $curr,
            'freelancer'=> $form->createView(),
        ));

    }

В шаблоне для этого поля у меня есть только:

{{ form_widget(freelancer.sapsubdomains) }}

РЕДАКТИРОВАТЬ: я добавил это к контроллеру:

$subdomaintexts  = $form['sapsubdomains']->getData();

            $sapsubdomains = $this->getDoctrine()
            ->getRepository(Sapsubdomain::class)
            ->findBy(array('subdomain'=>$subdomaintexts));

            $freelancer->addSapsubdomain($sapsubdomains);

Но теперь у меня появляется это сообщение об ошибке:

Исключительная ситуация при выполнении 'SELECT t0.id AS id_1, t0.subdomain AS subdomain_2, t0 .domain_id AS domain_id_3 ОТ sapsubdomain t0 ГДЕ t0.subdomain =? ' с параметрами [{}]:

Объект класса Doctrine \ ORM \ PersistentCollection не может быть преобразован в строку

1 Ответ

0 голосов
/ 26 апреля 2020

То, что вы пытаетесь сделать, это сохранить коллекцию Sapsubdomain в вашей сущности типа Freelancer. Правильный способ встраивания коллекций в формы - это использование Symfony\Component\Form\Extension\Core\Type\CollectionType::class, чтобы symfony правильно обрабатывал подборку.

<?php

class FreelancerType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        /** Other Fields **/

        $builder->add('sapsubdomains', CollectionType::class, [
            'entry_type' => SapsubdomainType::class,
            'entry_options' => ['label' => false],
             /** Other Options **/
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Freelancer::class,
        ]);
    }
}

Весь пример кода находится по адресу Symfony Документы: Использование коллекций форм

...