繁体   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