Home > Enterprise >  How to eliminate this Column not found error?
How to eliminate this Column not found error?

Time:12-31

Originally, the entity Gut had a field reaction that contained a string. The options for reaction were hard-wired in a template. By adding an entity Reaction and changing the Gut form's reaction to an EntityType I'm now plagued with the error message

SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.reaction' in 'field list'

even though I've rewritten the Gut & Reaction entities. I've probably lost sight of the forest for the trees. What's wrong with the following?

MySQL table gut: reaction column replaced by reaction_id; reaction_id correctly created; foreign key created manually.

Error occurs with this controller method:

    #[Route('/', name: 'app_gut_index', methods: ['GET'])]
    public function index(GutRepository $gutRepository): Response
    {
        $guts = $gutRepository->findBy([], ['happened' => 'DESC']); // error thrown here
        
        return $this->render('gut/index.html.twig', [
                    'guts' => $guts,
        ]);
    }

Gut entity:


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

    #[ORM\Column(length: 255)]
    #[ORM\ManyToOne(targetEntity: Reaction::class)]
    #[ORM\JoinColumn(name: 'reaction_id', referencedColumnName: 'id')]
    protected $reaction;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $description = null;

    #[ORM\Column(name: "datetime")]
    private ?\DateTime $happened = null;

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

    public function getReaction(): ?Reaction
    {
        return $this->reaction;
    }

    public function setReaction(?Reaction $reaction): self
    {
        $this->reaction = $reaction;

        return $this;
    }
...
}

Reaction entity:


use App\Entity\Gut;
use App\Repository\ReactionRepository;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

#[ORM\Entity(repositoryClass: ReactionRepository::class)]
class Reaction
{

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

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

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

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

    public function getReaction(): ?string
    {
        return $this->reaction;
    }

    public function setReaction(string $reaction): self
    {
        $this->reaction = $reaction;

        return $this;
    }

    #[ORM\OneToMany(targetEntity: Gut::class, mappedBy: 'reaction')]
    private $guts;

    /**
     * @return Collection|Product[]
     */
    public function getGuts(): Collection
    {
        return $this->guts;
    }

    public function addGut($gut): self
    {
        $this->guts[] = $gut;

        return $this;
    }

    public function __toString()
    {
        return $this->getReaction();
    }

}

CodePudding user response:

Your $reaction property should not have both ORM\Column and ORM\JoinColumn annotations at the same time.

Because of this Doctrine thinks it's a regular column so it's looking for a database field based on the variable name: $reaction -> gut.reaction.

Remove #[ORM\Column(length: 255)] then make sure that you have gut.reaction_id in your database and now it should work.

As a little side note I don't think you need name: 'reaction_id', referencedColumnName: 'id' in ORM\JoinColumn because that's how Doctrine will name them automatically anyway

CodePudding user response:

Just couldn't let go. I eventually found a path to get the Gut and Reaction entities to play nicely together. What I did:

  • cloned the project
  • manually deleted reaction property from Gut entity; created & executed a migration
  • in MySQL, added back in a reaction column
  • used make:entity Gut to add a reaction property as ManyToOne on Reaction; made a migration
  • used MySQL to populate the reaction_id column from the database of the cloned project.
  • (Probably missed a step in here somewhere, but) gut->getReaction(),etc, now behave as expected - in a ManyToOne relationship.
  • Related