首先,抱歉我的英语不好,我是法国人。
所以,我尝试创建一个使用许多实体的表单。如下:
控制器:
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 AppEntity;
use AppRepositoryChasseurAnimauxSessionRepository;
use DoctrineORMMapping as ORM;
#[ORMEntity(repositoryClass: ChasseurAnimauxSessionRepository::class)]
class ChasseurAnimauxSession
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMColumn(length: 10, nullable: true)]
private ?string $sexe = null;
#[ORMColumn(nullable: true)]
private ?int $poids = null;
#[ORMOneToMany(mappedBy: 'id', targetEntity: SessionChasse::class)]
#[ORMColumn(nullable: true)]
private ?int $session_chasse_id = null;
#[ORMOneToMany(mappedBy: 'id', targetEntity: Animaux::class)]
#[ORMColumn(nullable: true)]
private ?int $animaux_id = null;
#[ORMOneToMany(mappedBy: 'id', targetEntity: User::class)]
#[ORMColumn(nullable: true)]
private ?int $chasseur_id = null;
#[ORMColumn(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;
}
}
另一个实体动画:
namespace AppEntity;
use AppRepositoryAnimauxRepository;
use DoctrineORMMapping as ORM;
#[ORMEntity(repositoryClass: AnimauxRepository::class)]
class Animaux
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMColumn(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 AppFormuser;
use AppEntityAnimaux;
use AppEntitySessionChasse;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeIntegerType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormFormBuilderInterface;
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']);
}
}
我不给你另一个实体,因为我认为如果这个的答案将有助于解决其他的问题。如果你需要更多的信息,请告诉我。
我得到的错误是这个:
类型为"?int", "AppEntityAnimaux"给定属性path "animaux_id"
我认为这是一个基本的错误,但我花了几个小时在symfony文档和论坛上,我没有任何进展。
谢谢你的帮助。
编辑:我添加了我一开始忘记的OneToMany。
首先,非常感谢您的回答!我很欣赏
我找到了一个解决方案,我张贴你的代码工作(但我不认为这是一个很好的方式来做的事情)
控制器:
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 AppEntity;
use AppRepositoryChasseurAnimauxSessionRepository;
use DoctrineORMMapping as ORM;
#[ORMEntity(repositoryClass: ChasseurAnimauxSessionRepository::class)]
class ChasseurAnimauxSession
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMColumn(length: 10, nullable: true)]
private ?string $sexe = null;
#[ORMColumn(nullable: true)]
private ?int $poids = null;
#[ORMOneToMany(mappedBy: 'id', targetEntity: SessionChasse::class)]
#[ORMColumn(nullable: true)]
private ?SessionChasse $session_chasse_id = null;
#[ORMOneToMany(mappedBy: 'id', targetEntity: Animaux::class)]
#[ORMColumn(nullable: true)]
private Animaux|null $animaux_id = null;
#[ORMOneToMany(mappedBy: 'id', targetEntity: User::class)]
#[ORMColumn(nullable: true)]
private ?int $chasseur_id = null;
#[ORMColumn(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;
}
}
另一个实体动画:
namespace AppEntity;
use AppRepositoryAnimauxRepository;
use DoctrineORMMapping as ORM;
#[ORMEntity(repositoryClass: AnimauxRepository::class)]
class Animaux
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMColumn(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']);
}
}
我要研究一下链接和Alexandra给我的答案,它看起来比我做的要好。
如果我做得更好,我会贴在这里
亚历山大,实际上,我们在课程中学习了Merise的方法;)
最后,一个ChasseurAnimauxSession可以有很多动物。我解释说:在这个实体中,我想为每一天的狩猎储存被猎人杀死的动物。因此,我在这个实体中放入了用户(杀死动物的猎人)、动物和SessionChasse(对应于狩猎的日期,一个数据)以及一些信息,如性别或重量。
看起来你是用MERISE的方式做的,一种法国方法(在我看来是个好方法)。据我所知,SessionChasseurAnimaux连接到一个动物,因为你链接到一个整数,而不是一个集合。所以这更像是一种一对一的关系,不是吗?(如果没有在评论中解释我们)
你不应该链接主键(animaux_id),而是实体Animal,因为Doctrine为你做了这项工作。它将跟随主键,将填充Animal对象,然后将其关联到你的SessionChasseurAnimaux实体。换句话说,SessionChasseurAnimaux->getAnimaux()将返回水合动物对象,而不是动物的id。所以你应该替换:
#[ORMOneToOne(mappedBy: 'id', targetEntity: Animaux::class)]
#[ORMColumn(nullable: true)]
private ?int $animaux = null;
#[ORMOneToOne(mappedBy: 'id', targetEntity: Animaux::class)]
#[ORMColumn(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。这个包将帮助您创建实体。它生成所有的代码!如果没有很好地描述实体,则很难构建表单。在我看来,这个教程非常好。