One-To-One, Bidirectional: Unknown column #7402

Open
opened 2026-01-22 15:51:17 +01:00 by admin · 0 comments
Owner

Originally created by @sh3bang on GitHub (Jul 18, 2024).

Hello,

i made two entities in one-to-one relation and get a "Unknown column" error when i query all rows in the following way

$entityManager->getRepository(Foo::class)->findAll();

Unknown column 't5.id' in 'on clause':

SELECT
	t0.id AS id_1,
	t2.id AS id_3,
	t2.foo_id AS foo_id_4
FROM
	foo t0
LEFT JOIN bar t2 ON
	t2.foo_id = t5.id

This way will work:

$entityManager->getRepository(Foo::class)
    ->createQueryBuilder('r')
    ->getQuery()
    ->getResult();

Entity/Foo.php

<?php

namespace App\Entity;

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

#[ORM\Entity(repositoryClass: FooRepository::class)]
class Foo
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\OneToOne(mappedBy: 'foo', cascade: ['persist', 'remove'])]
    private ?Bar $bar = null;

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

    public function getBar(): ?Bar
    {
        return $this->bar;
    }

    public function setBar(Bar $bar): static
    {
        // set the owning side of the relation if necessary
        if ($bar->getFoo() !== $this) {
            $bar->setFoo($this);
        }

        $this->bar = $bar;

        return $this;
    }
}

Entity/Bar.php

<?php

namespace App\Entity;

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

#[ORM\Entity(repositoryClass: BarRepository::class)]
class Bar
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\OneToOne(inversedBy: 'bar', cascade: ['persist', 'remove'])]
    #[ORM\JoinColumn(nullable: false)]
    private ?foo $foo = null;

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

    public function getFoo(): ?foo
    {
        return $this->foo;
    }

    public function setFoo(foo $foo): static
    {
        $this->foo = $foo;

        return $this;
    }
}

Using doctrine/orm
versions: 3.2.1

Originally created by @sh3bang on GitHub (Jul 18, 2024). Hello, i made two entities in one-to-one relation and get a "Unknown column" error when i query all rows in the following way ```php $entityManager->getRepository(Foo::class)->findAll(); ``` `Unknown column 't5.id' in 'on clause'`: ```sql SELECT t0.id AS id_1, t2.id AS id_3, t2.foo_id AS foo_id_4 FROM foo t0 LEFT JOIN bar t2 ON t2.foo_id = t5.id ``` This way will work: ```php $entityManager->getRepository(Foo::class) ->createQueryBuilder('r') ->getQuery() ->getResult(); ``` **Entity/Foo.php** ```php <?php namespace App\Entity; use App\Repository\FooRepository; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: FooRepository::class)] class Foo { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\OneToOne(mappedBy: 'foo', cascade: ['persist', 'remove'])] private ?Bar $bar = null; public function getId(): ?int { return $this->id; } public function getBar(): ?Bar { return $this->bar; } public function setBar(Bar $bar): static { // set the owning side of the relation if necessary if ($bar->getFoo() !== $this) { $bar->setFoo($this); } $this->bar = $bar; return $this; } } ``` **Entity/Bar.php** ```php <?php namespace App\Entity; use App\Repository\BarRepository; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: BarRepository::class)] class Bar { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\OneToOne(inversedBy: 'bar', cascade: ['persist', 'remove'])] #[ORM\JoinColumn(nullable: false)] private ?foo $foo = null; public function getId(): ?int { return $this->id; } public function getFoo(): ?foo { return $this->foo; } public function setFoo(foo $foo): static { $this->foo = $foo; return $this; } } ``` **Using doctrine/orm** versions: 3.2.1
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7402