簡體   English   中英

我的實體和類型有問題 Symfony 6

[英]Problem with my entity and type Symfony 6

首先,對不起我的英語,我是法國人。

因此,我嘗試創建一個使用許多實體的表單。 他們來了:

Controller:

class  SaisieController extends AbstractController
{
    #[Route('/user/saisie', name: 'app_usersaisie')]
    public function add(Request $request, ManagerRegistry $doctrine, EntityManagerInterface $entityManager,): Response
    {
        $session = new ChasseurAnimauxSession();
        $form = $this->createForm(SaisieFormType::class, $session);

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {//
            $em = $entityManager;
            $em->persist($form->getData());
            $em->flush();

            return $this->render('user/informationsUser.html.twig', [
                'form_index' => $form->createView(),
            ]);
        }
        return $this->render('user/informationsUser.html.twig', [
            'form_index' => $form->createView(),
        ]);
    }
}

第一個實體 ChasseurAniamauxSession:

    namespace App\Entity;

use App\Repository\ChasseurAnimauxSessionRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ChasseurAnimauxSessionRepository::class)]
class ChasseurAnimauxSession
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 10, nullable: true)]
    private ?string $sexe = null;

    #[ORM\Column(nullable: true)]
    private ?int $poids = null;

    #[ORM\OneToMany(mappedBy: 'id', targetEntity: SessionChasse::class)]
    #[ORM\Column(nullable: true)]
    private ?int $session_chasse_id = null;

    #[ORM\OneToMany(mappedBy: 'id', targetEntity: Animaux::class)]
    #[ORM\Column(nullable: true)]
    private ?int $animaux_id = null;

    #[ORM\OneToMany(mappedBy: 'id', targetEntity: User::class)]
    #[ORM\Column(nullable: true)]
    private ?int $chasseur_id = null;

    #[ORM\Column(nullable: true)]
    private ?int $number_bague = null;

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

    public function getSexe(): ?string
    {
        return $this->sexe;
    }

    public function setSexe(?string $sexe): self
    {
        $this->sexe = $sexe;

        return $this;
    }

    public function getPoids(): ?int
    {
        return $this->poids;
    }

    public function setPoids(?int $poids): self
    {
        $this->poids = $poids;

        return $this;
    }

    /**
     * @return int|null
     */
    public function getSessionChasseId(): ?int
    {
        return $this->session_chasse_id;
    }

    /**
     * @param int|null $session_chasse_id
     * @return ChasseurAnimauxSession
     */
    public function setSessionChasseId(?int $session_chasse_id): ChasseurAnimauxSession
    {
        $this->session_chasse_id = $session_chasse_id;
        return $this;
    }

    /**
     * @return int|null
     */
    public function getAnimauxId(): ?int
    {
        return $this->animaux_id;
    }

    /**
     * @param int|null $animaux_id
     * @return ChasseurAnimauxSession
     */
    public function setAnimauxId(?int $animaux_id): ChasseurAnimauxSession
    {
        $this->animaux_id = $animaux_id;
        return $this;
    }

    /**
     * @return int|null
     */
    public function getChasseurId(): ?int
    {
        return $this->chasseur_id;
    }

    /**
     * @param int|null $chasseur_id
     * @return ChasseurAnimauxSession
     */
    public function setChasseurId(?int $chasseur_id): ChasseurAnimauxSession
    {
        $this->chasseur_id = $chasseur_id;
        return $this;
    }



    public function getNumberBague(): ?int
    {
        return $this->number_bague;
    }

    public function setNumberBague(?int $number_bague): self
    {
        $this->number_bague = $number_bague;

        return $this;
    }
}

另一個實體 Animaux:

namespace App\Entity;

use App\Repository\AnimauxRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: AnimauxRepository::class)]
class Animaux
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $espece = null;

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

    public function getEspece(): ?string
    {
        return $this->espece;
    }

    public function setEspece(?string $espece): self
    {
        $this->espece = $espece;

        return $this;
    }
}

類型:

<?php

namespace App\Form\user;

use App\Entity\Animaux;
use App\Entity\SessionChasse;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class SaisieFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('animaux_id', EntityType::class,
                ['class' => Animaux::class,
                    'choice_label' => 'id'])
            ->add('session_chasse_id', EntityType::class,
                ['class' => SessionChasse::class,])
            ->add('number_bague', IntegerType::class,
                ['label' => 'Numéro de bague'])
            ->add('sexe', TextType::class,)
            ->add('poids', IntegerType::class,)
            ->add('save', SubmitType::class,
                ['label' => 'Enregistrer']);
    }
}

我不會給你另一個實體,因為我認為這個實體的答案是否有助於解決其他實體的問題。 如果您需要更多信息,請告訴我。

我的錯誤是這個:

在屬性路徑“animaux_id”中給出的類型為“?int”、“App\Entity\Animaux”的預期參數。

我認為這是基本錯誤,但我在 symfony 文檔和論壇上花了幾個小時,但沒有取得任何進展。

謝謝你的幫助。

編輯:我添加了我一開始忘記的 OneToMany 。

看起來你是用 MERISE 的方式做的,這是一種法國方法(IMO 是一種很好的方法)。 據我所知,一個 SessionChasseurAnimaux 連接到一個動物,因為你鏈接到一個 integer,而不是一個集合。 所以它更像是一種 OneToOne 關系,不是嗎? (如果沒有在評論中解釋我們)

您不應鏈接主鍵 (animaux_id),而應鏈接實體 Animal,因為 Doctrine 會為您完成這項工作。 它將遵循主鍵,將動物 object 水化,然后將其關聯到您的 SessionChasseurAnimaux 實體。 換句話說,SessionChasseurAnimaux->getAnimaux() 將返回水合動物 object,而不是動物的 ID。 所以你應該更換:

    #[ORM\OneToOne(mappedBy: 'id', targetEntity: Animaux::class)]
    #[ORM\Column(nullable: true)]
    private ?int $animaux = null;

經過

    #[ORM\OneToOne(mappedBy: 'id', targetEntity: Animaux::class)]
    #[ORM\Column(name: "animaux_id", referencedColumnName: "id", nullable: true)]
    private ?int $animaux = null;

然后,不要忘記更改 getter 和 setter。

    public function getAnimaux(): ?Animaux
    {
        return $this->animaux;
    }

    public function setAnimaux(?Animaux $animaux_): ChasseurAnimauxSession
    {
        $this->animaux = $animaux;
        return $this;
    }

你應該看看symfony/maker-bundle 這個包將幫助您創建您的實體。 如果您的實體沒有得到很好的描述,它會生成所有代碼。 您的 forms 將很難構建,IMO, 本教程非常好

首先非常感謝您的回答! 我很感激

我找到了一個解決方案並向您發布了有效的代碼(但我認為這不是做事的好方法)

Controller:

class SaisieFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('animaux_id', EntityType::class,
                ['class' => Animaux::class,
                    'choice_label' => 'espece'])
            ->add('session_chasse_id', EntityType::class,
                ['class' => SessionChasse::class,
                    'choice_label' => 'date'])
            ->add('number_bague', IntegerType::class,
                ['label' => 'Numéro de bague'])
            ->add('sexe', TextType::class,)
            ->add('poids', IntegerType::class,)
            ->add('save', SubmitType::class,
                ['label' => 'Enregistrer']);
    }
}

第一個實體 ChasseurAniamauxSession:

namespace App\Entity;

use App\Repository\ChasseurAnimauxSessionRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ChasseurAnimauxSessionRepository::class)]
class ChasseurAnimauxSession
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 10, nullable: true)]
    private ?string $sexe = null;

    #[ORM\Column(nullable: true)]
    private ?int $poids = null;

    #[ORM\OneToMany(mappedBy: 'id', targetEntity: SessionChasse::class)]
    #[ORM\Column(nullable: true)]
    private ?SessionChasse $session_chasse_id = null;

    #[ORM\OneToMany(mappedBy: 'id', targetEntity: Animaux::class)]
    #[ORM\Column(nullable: true)]
    private Animaux|null $animaux_id = null;

    #[ORM\OneToMany(mappedBy: 'id', targetEntity: User::class)]
    #[ORM\Column(nullable: true)]
    private ?int $chasseur_id = null;

    #[ORM\Column(nullable: true)]
    private ?int $number_bague = null;

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

    public function getSexe(): ?string
    {
        return $this->sexe;
    }

    public function setSexe(?string $sexe): self
    {
        $this->sexe = $sexe;

        return $this;
    }

    public function getPoids(): ?int
    {
        return $this->poids;
    }

    public function setPoids(?int $poids): self
    {
        $this->poids = $poids;

        return $this;
    }

    /**
     * @return SessionChasse|null
     */
    public function getSessionChasseId(): ?SessionChasse
    {
        return $this->session_chasse_id;
    }

    /**
     * @param SessionChasse|null $session_chasse_id
     * @return ChasseurAnimauxSession
     */
    public function setSessionChasseId(?SessionChasse $session_chasse_id): ChasseurAnimauxSession
    {
        $this->session_chasse_id = $session_chasse_id;
        return $this;
    }

    /**
     * @return Animaux|null
     */
    public function getAnimauxId(): ?Animaux
    {
        return $this->animaux_id;
    }

    /**
     * @param Animaux $animaux_id
     * @return ChasseurAnimauxSession
     */
    public function setAnimauxId(Animaux $animaux_id): ChasseurAnimauxSession
    {
        $this->animaux_id = $animaux_id;
        return $this;
    }

    /**
     * @return int|null
     */
    public function getChasseurId(): ?int
    {
        return $this->chasseur_id;
    }

    /**
     * @param int|null $chasseur_id
     * @return ChasseurAnimauxSession
     */
    public function setChasseurId(?int $chasseur_id): ChasseurAnimauxSession
    {
        $this->chasseur_id = $chasseur_id;
        return $this;
    }



    public function getNumberBague(): ?int
    {
        return $this->number_bague;
    }

    public function setNumberBague(?int $number_bague): self
    {
        $this->number_bague = $number_bague;

        return $this;
    }
}

另一個實體 Animaux:

namespace App\Entity;

use App\Repository\AnimauxRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: AnimauxRepository::class)]
class Animaux
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $espece = null;

    public function __toString(): string
    {
        return $this->getId();
    }

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

    public function getEspece(): ?string
    {
        return $this->espece;
    }

    public function setEspece(?string $espece): self
    {
        $this->espece = $espece;

        return $this;
    }
}

類型:

class SaisieFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('animaux_id', EntityType::class,
                ['class' => Animaux::class,
                    'choice_label' => 'espece'])
            ->add('session_chasse_id', EntityType::class,
                ['class' => SessionChasse::class,
                    'choice_label' => 'date'])
            ->add('number_bague', IntegerType::class,
                ['label' => 'Numéro de bague'])
            ->add('sexe', TextType::class,)
            ->add('poids', IntegerType::class,)
            ->add('save', SubmitType::class,
                ['label' => 'Enregistrer']);
    }
}

我將研究亞歷山德拉給我的鏈接和答案,它看起來真的比我所做的要好。

如果我做得更好,我會在這里發布

Alexendre,實際上,我們在課程中學習了 Merise 方式;)

最后,一個 ChasseurAnimauxSession 可以有很多動物。 我解釋:在這個實體中,我想為每一天的狩獵儲存被獵人殺死的動物。 所以我在這個實體中放入了用戶(殺死動物的獵人)、動物和 SessionChasse(對應於狩獵日,一個數據)和一些信息,如性別或體重。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM