I found the following part of code in EagerLoadingExtension
} else {
$queryBuilder->addSelect($associationAlias);
}
// Avoid recursion
if ($mapping['targetEntity'] === $resourceClass) {
$queryBuilder->addSelect($associationAlias);
continue;
}
The problem is that we add the same alias to select for the second time. It confuses Doctrine and can work only occasionally.
The part "Avoid recursion" was added in commit https://github.com/api-platform/core/commit/5ba518014e2770e1ad5686b0124b2db45245fee5#diff-0e280fa016b79eacb061c504a6348752 with is marked as "Temporary solution". I do not know, what exactly was solved, but it seems very strange.
Below is my code, that is failed because of this.
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity()
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="Type", type="string")
* @ORM\DiscriminatorMap({"C" = "Category"})
*/
abstract class CategoryBase
{
/**
* @var int
*
* @ORM\Id
* @Groups({"category"})
*/
public $id;
}
////////////////
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* Group
*
* @ORM\Entity
* @ApiResource(
* attributes={ "normalization_context"= {"groups"={"category"} }}
* )
*/
class Category extends CategoryBase
{
/**
* @var Category[]
* @ORM\OneToMany(targetEntity="Category", mappedBy="parentCategory")
* @ORM\JoinColumn(referencedColumnName="parentId")
* @Groups({"category"})
*/
public $subcategories;
/**
* @var Group
* @ORM\ManyToOne(targetEntity="Category", inversedBy="subcategories")
* @ORM\JoinColumn(name="ParentId", referencedColumnName="id")
*/
public $parentCategory;
}
I tryed to fetch /category/1 , that does not have children and got The discriminator column \"Type\" is missing for \"App\\Entity\\Category\" using the DQL alias \"subcategories_a1\"."
It is long to explain, but the real problem is described above. The generated DQL is
SELECT o, subcategories_a1, subcategories_a1 FROM App\Entity\Category o LEFT JOIN o.subcategories subcategories_a1 WHERE o.id = :id_id
About that, this is to prevent joining relations twice for the same class. Assuming that you'd have a relation Foo in the Category you'd want 1 join in the query, not 3 (root entity, subcategories and parentCategory).
That said, it looks like we may need a check for your case. I've attempted to patch this : https://github.com/soyuka/core/tree/fix-1959
May you try it? Thanks!
By the way, if you disable eager_loading on this class it should work.
Thank you. The patch fixes it for me.
Disabling eager of course works too.
It took me quite some time to find this fix for my old Api Platform version. After i found it it was quite easy to prepare a patch and get rid of the problem. Id like to provide some google keywords below to make it easier for others to find it ;) Thanks for the fix!!
Error message usually is something like:
The discriminator column "type" is missing for "User" using the DQL alias "relatedUser_p2"
I've ran into the same issue. API Platform generates this query:
"SELECT o FROM App\EntityFoo o WHERE o.id = :id_id"
How do I prevent this? My entity Foo has a (nullable) relationships to itself.
@stephanvierkant Until we were able to upgrade our project to recent api platform (were its no longer an issue) we prepared a git patch for the core file and applied the patch on composer. The fix is this one from @soyuka -> https://github.com/api-platform/core/pull/1961/files#diff-0e280fa016b79eacb061c504a6348752
Most helpful comment
https://github.com/api-platform/core/blob/5ba518014e2770e1ad5686b0124b2db45245fee5/src/Bridge/Doctrine/Orm/Extension/EagerLoadingExtension.php#L154
About that, this is to prevent joining relations twice for the same class. Assuming that you'd have a relation
Fooin theCategoryyou'd want 1 join in the query, not 3 (rootentity,subcategoriesandparentCategory).That said, it looks like we may need a check for your case. I've attempted to patch this : https://github.com/soyuka/core/tree/fix-1959
May you try it? Thanks!
By the way, if you disable
eager_loadingon this class it should work.