Home > database >  Notice: Undefined index: - Unable to set AssociationField
Notice: Undefined index: - Unable to set AssociationField

Time:10-20

I'm trying to set a AssociationField in Easy Admin 3

I get the following error

"Notice: Undefined index: comment File:/home/wwwroot/htdocs/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php Line: 1783"

Here is my field:

            AssociationField::new('comment')

EDIT: Here are my Updated Enities:

Answer:

<?php

namespace App\Entity;

use App\Repository\AnswerRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=AnswerRepository::class)
 */
class Answer extends BaseEntity
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @var $id int
     */
    private $id;

    /**
     * @ORM\Column(type="text", nullable=true)
     * @var $answer string
     */
    private $answer;

    /**
     * @ORM\Column(type="integer")
     * @var $countVoteCreative int
     */
    private $countVoteCreative = 0;

    /**
     * @ORM\Column(type="integer")
     * @var $countVoteCorrect int
     */
    private $countVoteCorrect = 0;

    /**
     * Defines at what dive the answer was given
     *
     * @ORM\Column(type="integer")
     * @var $diveIndex int
     */
    private $diveIndex = 0;

    /**
     * One Answer has many comments, this is the inverse side
     * @var ArrayCollection|Comment[]
     * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="answer", cascade={"persist", "remove"})
     */
    private $comment;

    /**
     * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="category", cascade={"persist", "remove"})
     * @ORM\JoinColumn(onDelete="CASCADE")
     * @var Category $category
     */
    private $category;

    /**
     * @ORM\ManyToOne(targetEntity=User::class, inversedBy="answerBody", cascade={"persist", "remove"})
     * @ORM\JoinColumn(onDelete="CASCADE")
     * @var User $user
     */
    private $user;

    public function __construct()
    {
        parent::__construct();
        $this->comment = new ArrayCollection();
    }

    /**
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $perfectAnswer;

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

    /**
     * Returns the Answer
     *
     * @return string|null
     */
    public function getAnswer()
    {
        return $this->answer;
    }

    /**
     * Sets the Answer
     *
     * @param string|null $answer
     * @return void
     */
    public function setAnswer(?string $answer)
    {
        $this->answer = $answer;
    }

    /**
     * Returns the CountVoteCreate
     *
     * @return int|null
     */
    public function getCountVoteCreative(): ?int
    {
        return $this->countVoteCreative;
    }

    /**
     * Sets the CountVoteCreate
     *
     * @param int|null $countVoteCreative
     * @return void
     */
    public function setCountVoteCreative(?int $countVoteCreative)
    {
        $this->countVoteCreative = $countVoteCreative;
    }

    /**
     * Returns the CountVoteCorrect
     *
     * @return int|null
     */
    public function getCountVoteCorrect(): ?int
    {
        return $this->countVoteCorrect;
    }

    /**
     * Sets the CountVoteCorrect
     *
     * @param int|null $countVoteCorrect
     * @return void
     */
    public function setCountVoteCorrect(?int $countVoteCorrect)
    {
        $this->countVoteCorrect = $countVoteCorrect;
    }

    /**
     * Returns the DiveIndex
     *
     * @return int|null
     */
    public function getDiveIndex(): ?int
    {
        return $this->diveIndex;
    }

    /**
     * Sets the DiveIndex
     *
     * @param int|null $diveIndex
     * @return void
     */
    public function setDiveIndex(?int $diveIndex)
    {
        $this->diveIndex = $diveIndex;
    }

    /**
     * @return Collection|ArrayCollection|Comment[]
     */
    public function getComments()
    {
        return $this->comment;
    }

    public function addComment(Comment $comment)
    {
        $this->comment->add($comment);
    }

    public function removeComment(Comment $comment)
    {
        $this->comment->removeElement($comment);
    }

    /**
     * @return Category|null
     */
    public function getCategory(): ?Category
    {
        return $this->category;
    }

    /**
     * @param Category|null $category
     * @return $this
     */
    public function setCategory(?Category $category): self
    {
        $this->category = $category;

        return $this;
    }

    /**
     * @return User|null
     */
    public function getUser(): ?User
    {
        return $this->user;
    }

    /**
     * @param User|null $user
     * @return $this
     */
    public function setUser(?User $user): self
    {
        $this->user = $user;

        return $this;
    }

    /**
     * @return bool|null
     */
    public function isPerfectAnswer(): ?bool
    {
        return $this->perfectAnswer;
    }

    /**
     * @param bool|null $perfectAnswer
     * @return $this
     */
    public function setPerfectAnswer(?bool $perfectAnswer): self
    {
        $this->perfectAnswer = $perfectAnswer;

        return $this;
    }

    /**
     * @return mixed
     */
    public function __toString()
    {
        return $this->answer;
    }
}

Comment:

    <?php

namespace App\Entity;

use App\Repository\CommentRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=CommentRepository::class)
 */
class Comment
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="text", nullable=true)
     * @var string
     */
    private $comment;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $commentAuthor;

    /**
     * Many comments have one answer, this is the owning side
     * @var Answer
     * @ORM\ManyToOne(targetEntity=Answer::class, inversedBy="comments")
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    private $answer;

    /**
     * @ORM\JoinColumn(onDelete="CASCADE")
     * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="category", cascade={"persist", "remove"})
     * @var Category
     */
    private $category;

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

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

    /**
     * @param string|null $comment
     * @return $this
     */
    public function setComment(?string $comment): self
    {
        $this->comment = $comment;

        return $this;
    }

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

    /**
     * @param string|null $commentAuthor
     * @return $this
     */
    public function setCommentAuthor(?string $commentAuthor): self
    {
        $this->commentAuthor = $commentAuthor;

        return $this;
    }

    /**
     * Returns the Answer
     *
     * @return mixed
     */
    public function getAnswer()
    {
        return $this->answer;
    }

    /**
     * Sets the Answer
     *
     * @param mixed $answer
     * @return void
     */
    public function setAnswer($answer)
    {
        $this->answer = $answer;
    }

    /**
     * Returns the Category
     *
     * @return mixed
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * Sets the Category
     *
     * @param mixed $category
     * @return void
     */
    public function setCategory($category)
    {
        $this->category = $category;
    }

    /**
     * @return string|null
     */
    public function __toString()
    {
        return (string)$this->comment;
    }
}
    

Why am i Not able to set this ?

I have tried using other field types but none work. I'm not sure if my comment annotation is wrong or why this error is being thrown.

p.S; I removed unnecessary field from the Entitiy to keep it small.

Thank you.

Also here is my AnswerCrudController:

        $fields = [
        IdField::new('id')->hideOnForm(),
        TextField::new('answer'),
        BooleanField::new('perfectAnswer')->hideOnForm()->setFormTypeOption('disabled', 'disabled'),
        AssociationField::new('comment')->setFormTypeOptions(['by_reference' => false])
    ];

An

CodePudding user response:

did you by any chance forgot to update/migrate your database ?

if yes try : php bin/console doctrine:schema:update --force

CodePudding user response:

The declaration for @ORM\OneToMany(targetEntity=Comment::class, mappedBy="comment") appears to be incorrect. The error is thrown because the Comment::$comment property does not exist as a ManyToOne association.

Since the mapping is associated to the Comment::$answer property and Answer::$comments property. For the OneToMany bi-directional association mapping, it should be mappedBy="answer" as one Answer that is associated with a collection of many Comment entities.

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

class Answer
{
    /**
     * One Answer has many comments, this is the inverse side
     * @var ArrayCollection|Comment[]
     * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="answer", cascade={"persist", "remove"})
     */
    private $comments;

    public function __construct()
    {
        $this->comments = new ArrayCollection();
    }

    /**
     * @return Collection|ArrayCollection|Comment[]
     */
    public function getComments()
    {
        return $this->comments;
    }

    public function addComment(Comment $comment)
    {
        $this->comments->add($comment);
    }

    public function removeComment(Comment $comment)
    {
        $this->comments->removeElement($comment);
    }
}
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

class Comment
{
    /**
     * Many comments have one answer, this is the owning side
     * @var Answer
     * @ORM\ManyToOne(targetEntity=Answer::class, inversedBy="comments")
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    private $answer;

    /**
     * @return Answer
     */
    public function getAnswer()
    {
        return $this->answer;
    }

    public function setAnswer(Answer $answer)
    {
        $this->answer = $answer;
    }
}
  • Related