0

I have a table Message which contain some messages template, my fields are the name of the message and a field for the body of the message.

and I have a table Suggestion, which content some fields and a Message field.

how it's works : Suggestiopn is a form to be filled with EasyAdminBundle, and the Message field is displayed as a Choice field. It contain the data from the Message table like this :

enter image description here

My issue : when I try to create a suggest, I got this error : Expected argument of type "?App\Entity\Message", "int" given at property path "message".

My field "message" in my "Suggestion" entity is ManyToOne relation

there is some code:

entity Suggestion :

    #[ORM\ManyToOne(targetEntity: Suggestion::class, inversedBy: 'suggestion')]
    #[ORM\JoinColumn(nullable: false)]
    private $message;

 public function getMessage(): ?Message
    {
        return $this->message;
    }

    public function setMessage(?Message $message): self
    {
        $this->message = $message;

        return $this;
    }

Message entity :

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $name = null;

    #[ORM\Column(type: Types::TEXT)]
    private ?string $message = null;

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

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return string|null
     */
    public function getMessage(): ?string
    {
        return $this->message;
    }

    /**
     * @param string|null $message
     */
    public function setMessage(?string $message): void
    {
        $this->message = $message;
    }

My crud controller of Suggestion :

  public function configureFields(string $pageName): iterable
    {
        $allMessages = $this->messageRepository->findAll();
        $messagesChoices = array();
        /** @var Message $message */
        foreach ($allMessages as $message) {
            $messagesChoices[$message->getName()] = $message->getId();
        }
        return [
           some fields...
            ChoiceField::new('message')->setChoices($messagesChoices),

        ];
    }

I checked the table suggestion with phpmyadmin, and the message field is "message_id" I suppose it expect an integer

Someone have an idea to how can I solve this problem ?

johndoe80
  • 87
  • 10
  • use a AssociationField instead of ChoiceField like it is explained here https://stackoverflow.com/questions/70806029/how-to-list-entities-in-a-choicefield-with-easyadmin – Fabien Papet Nov 09 '22 at 17:51

0 Answers0