我正在开发一个表单来管理 Symfony 6.3 应用程序中的联系人。联系人创建效果很好。 但是,当我尝试修改联系人时,而不是通常显示的表单,出现以下错误消息:
Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue():参数 #1 ($object) 必须是 ?object 类型,给定字符串,在 D: 中调用。 TDEKDO endor\symfony orm\ChoiceList\ArrayChoiceList.php 第 134 行
当我在将联系人变量分配给表单之前转储它时,它会作为一个对象出现:
这是我的联系方式表格:
`<?php
namespace App\Form;
use App\Entity\Contact;
use App\Entity\Adherent;
use App\Entity\TypeVoie;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$typevoies = $options['typevoies'];
$adherents = $options['adherents'];
$builder
->add('politesse', ChoiceType::class, [
'label' => 'Politesse',
'required' => true,
'multiple' => false,
'expanded' => false,
'choices' => [
'Mr' => 'Mr',
'Mme' => 'Mme'
]
])
->add('nom', TextType::class, [
'label' => 'Nom',
'constraints' => new Length([
'min' => 2,
'max' => 30
]),
'required' => true,
'attr' => [
'placeholder' => "Merci de saisir le nom de contact"
]
])
->add('prenom', TextType::class, [
'label' => 'Prénom',
'constraints' => new Length([
'min' => 2,
'max' => 30
]),
'required' => true,
'attr' => [
'placeholder' => "Merci de saisir le prénom de contact"
]
])
->add('tel', TelType::class, [
'label' => 'Téléphone',
'attr' => [
'placeholder' => "Merci de saisir le numero de téléphone"
]
])
->add('mobile', TelType::class, [
'label' => 'Mobile',
'attr' => [
'placeholder' => "Merci de saisir le numero de mobile"
]
])
->add('mail', TextType::class, [
'label' => 'Mail',
'attr' => [
'placeholder' => "Merci de saisir le mail"
]
])
->add('fonction', TextType::class, [
'label' => 'Fonction',
'attr' => [
'placeholder' => "Merci de saisir la fonction"
]
])
->add('annule')
->add('numvoie', TextType::class, [
'label' => 'Numero',
'attr' => [
'placeholder' => "Voie"
]
])
->add('typevoie', EntityType::class, [
'label' => 'Type de voie',
'class' => TypeVoie::class,
'choices' => $typevoies,
'choice_label' => 'libelle',
'required' => true,
'attr' => [
'placeholder' => "Merci de choisir le type de voie"
]
])
->add('adresse1')
->add('adresse2')
->add('adresse3')
->add('postal')
->add('ville')
->add('adherent', EntityType::class, [
'label' => 'Adhérent',
'class' => Adherent::class,
'choices' => $adherents,
'choice_label' => 'nom'
])
->add('submit', SubmitType::class, [
'label' => 'Inserer',
'attr' => [
'class' => 'btn btn-success w-100'
]
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Contact::class,
'typevoies' => [],
'adherents' => []
]);
}
}`
这是我的联系人控制器:
`<?php
namespace App\Controller;
use App\Entity\Contact;
use App\Entity\Adherent;
use App\Entity\TypeVoie;
use App\Form\ContactType;
use App\Entity\Utilisateur;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ContactController extends AbstractController
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
#[Route('/administration/contacts', name: 'app_admin_contacts')]
public function admin(): Response
{
$adherents = $this->entityManager->getRepository(Adherent::class)->findAll();
$contacts = [];
foreach ($adherents as $key => $adherent) {
$contacts[] = $this->entityManager->getRepository(Contact::class)->contactsByAdherentId($adherent->getId());
}
return $this->render('contact/index.html.twig', [
'contacts' => $contacts,
]);
}
#[Route('/utilisateur/{user_id}/contacts', name: 'app_user_contacts')]
public function user($user_id): Response
{
$loggedInUser = $this->getUser()->getId();
$user = $this->entityManager->getRepository(Utilisateur::class)->find($user_id);
if ($loggedInUser !== $user->getId()) {
return $this->redirectToRoute('app_login');
}
$association_id = $user->getAssociation()->getId();
$adherents = $this->entityManager->getRepository(Adherent::class)->adherentsByAssocId($association_id);
$contacts = [];
foreach ($adherents as $key => $adherent) {
$contacts[] = $this->entityManager->getRepository(Contact::class)->contactsByAdherentId($adherent->getId());
}
return $this->render('contact/index.html.twig', [
'contacts' => $contacts,
]);
}
#[Route('/contact/ajouter', name: 'app_add_contact')]
public function add(Request $request): Response
{
$user = $this->getUser();
$associationId = $user->getAssociation()->getId();
if ($associationId === 1) {
$typevoies = $this->entityManager->getRepository(TypeVoie::class)->findAll();
$adherents = $this->entityManager->getRepository(Adherent::class)->findAll();
}
if ($associationId > 1) {
$typevoies = $this->entityManager->getRepository(TypeVoie::class)->typevoiesByAssocId($associationId);
$adherents = $this->entityManager->getRepository(Adherent::class)->adherentsByAssocId($associationId);
}
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact, [
'typevoies' => $typevoies,
'adherents' => $adherents
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($contact);
$this->entityManager->flush();
if ($user->getRoles() === 'ROLE_ADMIN') {
return $this->redirectToRoute('app_admin_contacts');
}
return $this->redirectToRoute('app_user_contacts', [
'user_id' => $user->getId()
]);
}
return $this->render('contact/new.html.twig', [
'form' => $form->createView(),
]);
}
#[Route('/contact/modifier/{contactId}', name: 'app_edit_contact')]
public function edit($contactId): Response
{
$user = $this->getUser();
$associationId = $user->getAssociation()->getId();
if ($associationId === 1) {
$typevoies = $this->entityManager->getRepository(TypeVoie::class)->findAll();
$adherents = $this->entityManager->getRepository(Adherent::class)->findAll();
}
if ($associationId > 1) {
$typevoies = $this->entityManager->getRepository(TypeVoie::class)->typevoiesByAssocId($associationId);
$adherents = $this->entityManager->getRepository(Adherent::class)->adherentsByAssocId($associationId);
}
$contact = $this->entityManager->getRepository(Contact::class)->findOneById($contactId);
//dd($contact);
$form = $this->createForm(ContactType::class, $contact, [
'typevoies' => $typevoies,
'adherents' => $adherents
]);
return $this->render('contact/new.html.twig', [
'form' => $form->createView()
]);
}
}`
这是我的联系人实体:
`<?php
namespace App\Entity;
use App\Repository\ContactRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ContactRepository::class)]
class Contact
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 10)]
private ?string $politesse = null;
#[ORM\Column(length: 30)]
private ?string $nom = null;
#[ORM\Column(length: 40)]
private ?string $prenom = null;
#[ORM\Column(length: 20, nullable: true)]
private ?string $tel = null;
#[ORM\Column(length: 20, nullable: true)]
private ?string $mobile = null;
#[ORM\Column(length: 50)]
private ?string $mail = null;
#[ORM\Column(length: 40)]
private ?string $fonction = null;
#[ORM\Column]
private ?bool $annule = null;
#[ORM\Column(length: 5, nullable: true)]
private ?string $numvoie = null;
#[ORM\Column(length: 20, nullable: true)]
private ?string $typevoie = null;
#[ORM\Column(length: 50)]
private ?string $adresse1 = null;
#[ORM\Column(length: 50, nullable: true)]
private ?string $adresse2 = null;
#[ORM\Column(length: 50, nullable: true)]
private ?string $adresse3 = null;
#[ORM\Column(length: 6)]
private ?string $postal = null;
#[ORM\Column(length: 50)]
private ?string $ville = null;
#[ORM\ManyToOne(inversedBy: 'contacts')]
#[ORM\JoinColumn(nullable: false)]
private ?Adherent $adherent = null;
public function getId(): ?int
{
return $this->id;
}
public function getPolitesse(): ?string
{
return $this->politesse;
}
public function setPolitesse(string $politesse): self
{
$this->politesse = $politesse;
return $this;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getPrenom(): ?string
{
return $this->prenom;
}
public function setPrenom(string $prenom): self
{
$this->prenom = $prenom;
return $this;
}
public function getTel(): ?string
{
return $this->tel;
}
public function setTel(?string $tel): self
{
$this->tel = $tel;
return $this;
}
public function getMobile(): ?string
{
return $this->mobile;
}
public function setMobile(?string $mobile): self
{
$this->mobile = $mobile;
return $this;
}
public function getMail(): ?string
{
return $this->mail;
}
public function setMail(string $mail): self
{
$this->mail = $mail;
return $this;
}
public function getFonction(): ?string
{
return $this->fonction;
}
public function setFonction(string $fonction): self
{
$this->fonction = $fonction;
return $this;
}
public function isAnnule(): ?bool
{
return $this->annule;
}
public function setAnnule(bool $annule): self
{
$this->annule = $annule;
return $this;
}
public function getNumvoie(): ?string
{
return $this->numvoie;
}
public function setNumvoie(?string $numvoie): self
{
$this->numvoie = $numvoie;
return $this;
}
public function getTypevoie(): ?string
{
return $this->typevoie;
}
public function setTypevoie(?string $typevoie): self
{
$this->typevoie = $typevoie;
return $this;
}
public function getAdresse1(): ?string
{
return $this->adresse1;
}
public function setAdresse1(string $adresse1): self
{
$this->adresse1 = $adresse1;
return $this;
}
public function getAdresse2(): ?string
{
return $this->adresse2;
}
public function setAdresse2(?string $adresse2): self
{
$this->adresse2 = $adresse2;
return $this;
}
public function getAdresse3(): ?string
{
return $this->adresse3;
}
public function setAdresse3(?string $adresse3): self
{
$this->adresse3 = $adresse3;
return $this;
}
public function getPostal(): ?string
{
return $this->postal;
}
public function setPostal(string $postal): self
{
$this->postal = $postal;
return $this;
}
public function getVille(): ?string
{
return $this->ville;
}
public function setVille(string $ville): self
{
$this->ville = $ville;
return $this;
}
public function getAdherent(): ?Adherent
{
return $this->adherent;
}
public function setAdherent(?Adherent $adherent): self
{
$this->adherent = $adherent;
return $this;
}
}`
这里缺少什么?
提前致谢。
我通过将以下代码添加到表单中成功解决了该问题:
'multiple' => false,
'expanded' => false,
'mapped' => false
它已添加到表单的“typevoie”元素中。
SF 6.3 - 问题出现在
Form Entitytype -> 'query_bulider' => custom query,
仅在控制器中进行编辑操作期间(控制器和命令创建的 CRUD)
新产品工作没有问题
解决方案:
在实体类型'mapped'中使用=> false