php Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue():参数#1($object)必须是类型?对象,给定的字符串

dauxcl2d  于 2023-06-21  发布在  PHP
关注(0)|答案(1)|浏览(105)

我正在开发一个表单来管理Symfony 6.3应用程序中的联系人。联系人创建工作得很好。但是,当我尝试修改联系人时,出现以下错误消息,而不是通常显示的表单:
Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue():参数#1($object)必须是类型?对象,给定字符串,在D:\2中调用。Copyright © 2018 - 2019 www.jsjsj.com All Rights Reserved
screenshot of the error message
当我在将contact变量赋值给表单之前转储它时,它作为一个对象出现:contact variable as an object
这是我的ContactType表单:

  1. `<?php
  2. namespace App\Form;
  3. use App\Entity\Contact;
  4. use App\Entity\Adherent;
  5. use App\Entity\TypeVoie;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  9. use Symfony\Component\Validator\Constraints\Length;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. use Symfony\Component\Form\Extension\Core\Type\TelType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  15. class ContactType extends AbstractType
  16. {
  17. public function buildForm(FormBuilderInterface $builder, array $options): void
  18. {
  19. $typevoies = $options['typevoies'];
  20. $adherents = $options['adherents'];
  21. $builder
  22. ->add('politesse', ChoiceType::class, [
  23. 'label' => 'Politesse',
  24. 'required' => true,
  25. 'multiple' => false,
  26. 'expanded' => false,
  27. 'choices' => [
  28. 'Mr' => 'Mr',
  29. 'Mme' => 'Mme'
  30. ]
  31. ])
  32. ->add('nom', TextType::class, [
  33. 'label' => 'Nom',
  34. 'constraints' => new Length([
  35. 'min' => 2,
  36. 'max' => 30
  37. ]),
  38. 'required' => true,
  39. 'attr' => [
  40. 'placeholder' => "Merci de saisir le nom de contact"
  41. ]
  42. ])
  43. ->add('prenom', TextType::class, [
  44. 'label' => 'Prénom',
  45. 'constraints' => new Length([
  46. 'min' => 2,
  47. 'max' => 30
  48. ]),
  49. 'required' => true,
  50. 'attr' => [
  51. 'placeholder' => "Merci de saisir le prénom de contact"
  52. ]
  53. ])
  54. ->add('tel', TelType::class, [
  55. 'label' => 'Téléphone',
  56. 'attr' => [
  57. 'placeholder' => "Merci de saisir le numero de téléphone"
  58. ]
  59. ])
  60. ->add('mobile', TelType::class, [
  61. 'label' => 'Mobile',
  62. 'attr' => [
  63. 'placeholder' => "Merci de saisir le numero de mobile"
  64. ]
  65. ])
  66. ->add('mail', TextType::class, [
  67. 'label' => 'Mail',
  68. 'attr' => [
  69. 'placeholder' => "Merci de saisir le mail"
  70. ]
  71. ])
  72. ->add('fonction', TextType::class, [
  73. 'label' => 'Fonction',
  74. 'attr' => [
  75. 'placeholder' => "Merci de saisir la fonction"
  76. ]
  77. ])
  78. ->add('annule')
  79. ->add('numvoie', TextType::class, [
  80. 'label' => 'Numero',
  81. 'attr' => [
  82. 'placeholder' => "Voie"
  83. ]
  84. ])
  85. ->add('typevoie', EntityType::class, [
  86. 'label' => 'Type de voie',
  87. 'class' => TypeVoie::class,
  88. 'choices' => $typevoies,
  89. 'choice_label' => 'libelle',
  90. 'required' => true,
  91. 'attr' => [
  92. 'placeholder' => "Merci de choisir le type de voie"
  93. ]
  94. ])
  95. ->add('adresse1')
  96. ->add('adresse2')
  97. ->add('adresse3')
  98. ->add('postal')
  99. ->add('ville')
  100. ->add('adherent', EntityType::class, [
  101. 'label' => 'Adhérent',
  102. 'class' => Adherent::class,
  103. 'choices' => $adherents,
  104. 'choice_label' => 'nom'
  105. ])
  106. ->add('submit', SubmitType::class, [
  107. 'label' => 'Inserer',
  108. 'attr' => [
  109. 'class' => 'btn btn-success w-100'
  110. ]
  111. ])
  112. ;
  113. }
  114. public function configureOptions(OptionsResolver $resolver): void
  115. {
  116. $resolver->setDefaults([
  117. 'data_class' => Contact::class,
  118. 'typevoies' => [],
  119. 'adherents' => []
  120. ]);
  121. }
  122. }`

这是我的ContactController:

  1. `<?php
  2. namespace App\Controller;
  3. use App\Entity\Contact;
  4. use App\Entity\Adherent;
  5. use App\Entity\TypeVoie;
  6. use App\Form\ContactType;
  7. use App\Entity\Utilisateur;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. class ContactController extends AbstractController
  14. {
  15. private $entityManager;
  16. public function __construct(EntityManagerInterface $entityManager)
  17. {
  18. $this->entityManager = $entityManager;
  19. }
  20. #[Route('/administration/contacts', name: 'app_admin_contacts')]
  21. public function admin(): Response
  22. {
  23. $adherents = $this->entityManager->getRepository(Adherent::class)->findAll();
  24. $contacts = [];
  25. foreach ($adherents as $key => $adherent) {
  26. $contacts[] = $this->entityManager->getRepository(Contact::class)->contactsByAdherentId($adherent->getId());
  27. }
  28. return $this->render('contact/index.html.twig', [
  29. 'contacts' => $contacts,
  30. ]);
  31. }
  32. #[Route('/utilisateur/{user_id}/contacts', name: 'app_user_contacts')]
  33. public function user($user_id): Response
  34. {
  35. $loggedInUser = $this->getUser()->getId();
  36. $user = $this->entityManager->getRepository(Utilisateur::class)->find($user_id);
  37. if ($loggedInUser !== $user->getId()) {
  38. return $this->redirectToRoute('app_login');
  39. }
  40. $association_id = $user->getAssociation()->getId();
  41. $adherents = $this->entityManager->getRepository(Adherent::class)->adherentsByAssocId($association_id);
  42. $contacts = [];
  43. foreach ($adherents as $key => $adherent) {
  44. $contacts[] = $this->entityManager->getRepository(Contact::class)->contactsByAdherentId($adherent->getId());
  45. }
  46. return $this->render('contact/index.html.twig', [
  47. 'contacts' => $contacts,
  48. ]);
  49. }
  50. #[Route('/contact/ajouter', name: 'app_add_contact')]
  51. public function add(Request $request): Response
  52. {
  53. $user = $this->getUser();
  54. $associationId = $user->getAssociation()->getId();
  55. if ($associationId === 1) {
  56. $typevoies = $this->entityManager->getRepository(TypeVoie::class)->findAll();
  57. $adherents = $this->entityManager->getRepository(Adherent::class)->findAll();
  58. }
  59. if ($associationId > 1) {
  60. $typevoies = $this->entityManager->getRepository(TypeVoie::class)->typevoiesByAssocId($associationId);
  61. $adherents = $this->entityManager->getRepository(Adherent::class)->adherentsByAssocId($associationId);
  62. }
  63. $contact = new Contact();
  64. $form = $this->createForm(ContactType::class, $contact, [
  65. 'typevoies' => $typevoies,
  66. 'adherents' => $adherents
  67. ]);
  68. $form->handleRequest($request);
  69. if ($form->isSubmitted() && $form->isValid()) {
  70. $this->entityManager->persist($contact);
  71. $this->entityManager->flush();
  72. if ($user->getRoles() === 'ROLE_ADMIN') {
  73. return $this->redirectToRoute('app_admin_contacts');
  74. }
  75. return $this->redirectToRoute('app_user_contacts', [
  76. 'user_id' => $user->getId()
  77. ]);
  78. }
  79. return $this->render('contact/new.html.twig', [
  80. 'form' => $form->createView(),
  81. ]);
  82. }
  83. #[Route('/contact/modifier/{contactId}', name: 'app_edit_contact')]
  84. public function edit($contactId): Response
  85. {
  86. $user = $this->getUser();
  87. $associationId = $user->getAssociation()->getId();
  88. if ($associationId === 1) {
  89. $typevoies = $this->entityManager->getRepository(TypeVoie::class)->findAll();
  90. $adherents = $this->entityManager->getRepository(Adherent::class)->findAll();
  91. }
  92. if ($associationId > 1) {
  93. $typevoies = $this->entityManager->getRepository(TypeVoie::class)->typevoiesByAssocId($associationId);
  94. $adherents = $this->entityManager->getRepository(Adherent::class)->adherentsByAssocId($associationId);
  95. }
  96. $contact = $this->entityManager->getRepository(Contact::class)->findOneById($contactId);
  97. //dd($contact);
  98. $form = $this->createForm(ContactType::class, $contact, [
  99. 'typevoies' => $typevoies,
  100. 'adherents' => $adherents
  101. ]);
  102. return $this->render('contact/new.html.twig', [
  103. 'form' => $form->createView()
  104. ]);
  105. }
  106. }`

这是我的联系人实体:

  1. `<?php
  2. namespace App\Entity;
  3. use App\Repository\ContactRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. #[ORM\Entity(repositoryClass: ContactRepository::class)]
  6. class Contact
  7. {
  8. #[ORM\Id]
  9. #[ORM\GeneratedValue]
  10. #[ORM\Column]
  11. private ?int $id = null;
  12. #[ORM\Column(length: 10)]
  13. private ?string $politesse = null;
  14. #[ORM\Column(length: 30)]
  15. private ?string $nom = null;
  16. #[ORM\Column(length: 40)]
  17. private ?string $prenom = null;
  18. #[ORM\Column(length: 20, nullable: true)]
  19. private ?string $tel = null;
  20. #[ORM\Column(length: 20, nullable: true)]
  21. private ?string $mobile = null;
  22. #[ORM\Column(length: 50)]
  23. private ?string $mail = null;
  24. #[ORM\Column(length: 40)]
  25. private ?string $fonction = null;
  26. #[ORM\Column]
  27. private ?bool $annule = null;
  28. #[ORM\Column(length: 5, nullable: true)]
  29. private ?string $numvoie = null;
  30. #[ORM\Column(length: 20, nullable: true)]
  31. private ?string $typevoie = null;
  32. #[ORM\Column(length: 50)]
  33. private ?string $adresse1 = null;
  34. #[ORM\Column(length: 50, nullable: true)]
  35. private ?string $adresse2 = null;
  36. #[ORM\Column(length: 50, nullable: true)]
  37. private ?string $adresse3 = null;
  38. #[ORM\Column(length: 6)]
  39. private ?string $postal = null;
  40. #[ORM\Column(length: 50)]
  41. private ?string $ville = null;
  42. #[ORM\ManyToOne(inversedBy: 'contacts')]
  43. #[ORM\JoinColumn(nullable: false)]
  44. private ?Adherent $adherent = null;
  45. public function getId(): ?int
  46. {
  47. return $this->id;
  48. }
  49. public function getPolitesse(): ?string
  50. {
  51. return $this->politesse;
  52. }
  53. public function setPolitesse(string $politesse): self
  54. {
  55. $this->politesse = $politesse;
  56. return $this;
  57. }
  58. public function getNom(): ?string
  59. {
  60. return $this->nom;
  61. }
  62. public function setNom(string $nom): self
  63. {
  64. $this->nom = $nom;
  65. return $this;
  66. }
  67. public function getPrenom(): ?string
  68. {
  69. return $this->prenom;
  70. }
  71. public function setPrenom(string $prenom): self
  72. {
  73. $this->prenom = $prenom;
  74. return $this;
  75. }
  76. public function getTel(): ?string
  77. {
  78. return $this->tel;
  79. }
  80. public function setTel(?string $tel): self
  81. {
  82. $this->tel = $tel;
  83. return $this;
  84. }
  85. public function getMobile(): ?string
  86. {
  87. return $this->mobile;
  88. }
  89. public function setMobile(?string $mobile): self
  90. {
  91. $this->mobile = $mobile;
  92. return $this;
  93. }
  94. public function getMail(): ?string
  95. {
  96. return $this->mail;
  97. }
  98. public function setMail(string $mail): self
  99. {
  100. $this->mail = $mail;
  101. return $this;
  102. }
  103. public function getFonction(): ?string
  104. {
  105. return $this->fonction;
  106. }
  107. public function setFonction(string $fonction): self
  108. {
  109. $this->fonction = $fonction;
  110. return $this;
  111. }
  112. public function isAnnule(): ?bool
  113. {
  114. return $this->annule;
  115. }
  116. public function setAnnule(bool $annule): self
  117. {
  118. $this->annule = $annule;
  119. return $this;
  120. }
  121. public function getNumvoie(): ?string
  122. {
  123. return $this->numvoie;
  124. }
  125. public function setNumvoie(?string $numvoie): self
  126. {
  127. $this->numvoie = $numvoie;
  128. return $this;
  129. }
  130. public function getTypevoie(): ?string
  131. {
  132. return $this->typevoie;
  133. }
  134. public function setTypevoie(?string $typevoie): self
  135. {
  136. $this->typevoie = $typevoie;
  137. return $this;
  138. }
  139. public function getAdresse1(): ?string
  140. {
  141. return $this->adresse1;
  142. }
  143. public function setAdresse1(string $adresse1): self
  144. {
  145. $this->adresse1 = $adresse1;
  146. return $this;
  147. }
  148. public function getAdresse2(): ?string
  149. {
  150. return $this->adresse2;
  151. }
  152. public function setAdresse2(?string $adresse2): self
  153. {
  154. $this->adresse2 = $adresse2;
  155. return $this;
  156. }
  157. public function getAdresse3(): ?string
  158. {
  159. return $this->adresse3;
  160. }
  161. public function setAdresse3(?string $adresse3): self
  162. {
  163. $this->adresse3 = $adresse3;
  164. return $this;
  165. }
  166. public function getPostal(): ?string
  167. {
  168. return $this->postal;
  169. }
  170. public function setPostal(string $postal): self
  171. {
  172. $this->postal = $postal;
  173. return $this;
  174. }
  175. public function getVille(): ?string
  176. {
  177. return $this->ville;
  178. }
  179. public function setVille(string $ville): self
  180. {
  181. $this->ville = $ville;
  182. return $this;
  183. }
  184. public function getAdherent(): ?Adherent
  185. {
  186. return $this->adherent;
  187. }
  188. public function setAdherent(?Adherent $adherent): self
  189. {
  190. $this->adherent = $adherent;
  191. return $this;
  192. }
  193. }`

这里缺少了什么?
先谢谢你了。

ssm49v7z

ssm49v7z1#

我通过在表单中添加以下代码解决了这个问题:

  1. 'multiple' => false,
  2. 'expanded' => false,
  3. 'mapped' => false

它已被添加到表单的“typevoie”元素中。

相关问题