AttributeOverride being ignored for JOINED InheritanceType on Id field when using Query Builder #5030

Open
opened 2026-01-22 14:56:57 +01:00 by admin · 2 comments
Owner

Originally created by @magickatt on GitHub (Mar 2, 2016).

I've come across what I think is a bug (I've tried to see if there is something I'm doing wrong in terms of the annotations) with how AttributeOverride is being used when trying to query a repository of a child class using the Query Builder.

In this case, the parent class has an Id field with name id, but the child class has an Id field with name application_id. The AttributeOverride seems to work correctly when persisting an object of the child class, but when trying to query a repository (default or custom) using the Query Builder it appears to be trying to join both tables using the overridden field name on both tables, instead of just the child class table

namespace Application;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="user_applications")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="product_id", type="integer")
 * @ORM\DiscriminatorMap({"1" = "Application\Mortgage"})
 */
abstract class AbstractApplication
{
    /**
     * @var integer
     * @ORM\Id
     * @ORM\Column(type="integer", name="id")
     * @ORM\GeneratedValue
     * @Serializer\Type("integer")
     * @Serializer\Expose
     */
    protected $id;
namespace Application;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="user_application_mortgages")
 *
 * @ORM\AttributeOverrides({
 *   @ORM\AttributeOverride(name="id",
 *     column=@ORM\Column(
 *       name     = "application_id",
 *       type       = "integer"
 *     )
 *   )
 * })
 */
class Mortgage extends AbstractApplication

As mentioned this persists correctly when creating new entries, but when trying to query Application\Mortgage I get the following exception because it's trying to join on application_id in both tables, instead of id on r0_ and application_id on r1_

Doctrine\DBAL\Exception\InvalidFieldNameException: An exception occurred while executing 'SELECT r0_.first_name AS first_name0, r0_.last_name AS last_name1, r0_.email AS email2, r0_.phone AS phone3, r0_.affiliate_id AS affiliate_id4, r0_.source_ip AS source_ip5, r0_.time_to_call AS time_to_call6, r0_.extra AS extra7, r0_.hash AS hash8, r0_.user_comments AS user_comments9, r0_.created_date AS created_date10, r0_.modified_date AS modified_date11, r1_.rate AS rate12, r1_.property_value AS property_value13, r1_.mortgage_amount AS mortgage_amount14, r1_.application_id AS application_id15, r0_.product_id AS product_id16, r0_.source_id AS source_id17, r0_.province_id AS province_id18, r1_.mortgage_id AS mortgage_id19, r1_.purchase_type_id AS purchase_type_id20 FROM user_application_mortgages r1_ INNER JOIN user_applications r0_ ON r1_.application_id = r0_.application_id LIMIT 10': Unknown column 'r0_.application_id' in 'on clause'

Any idea how this might be solved or what I'm doing wrong? Thanks!

Originally created by @magickatt on GitHub (Mar 2, 2016). I've come across what I think is a bug (I've tried to see if there is something I'm doing wrong in terms of the annotations) with how AttributeOverride is being used when trying to query a repository of a child class using the Query Builder. In this case, the parent class has an Id field with name id, but the child class has an Id field with name application_id. The AttributeOverride seems to work correctly when persisting an object of the child class, but when trying to query a repository (default or custom) using the Query Builder it appears to be trying to join both tables using the overridden field name on both tables, instead of just the child class table ``` php namespace Application; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="user_applications") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="product_id", type="integer") * @ORM\DiscriminatorMap({"1" = "Application\Mortgage"}) */ abstract class AbstractApplication { /** * @var integer * @ORM\Id * @ORM\Column(type="integer", name="id") * @ORM\GeneratedValue * @Serializer\Type("integer") * @Serializer\Expose */ protected $id; ``` ``` php namespace Application; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="user_application_mortgages") * * @ORM\AttributeOverrides({ * @ORM\AttributeOverride(name="id", * column=@ORM\Column( * name = "application_id", * type = "integer" * ) * ) * }) */ class Mortgage extends AbstractApplication ``` As mentioned this persists correctly when creating new entries, but when trying to query Application\Mortgage I get the following exception because it's trying to join on application_id in both tables, instead of id on r0_ and application_id on r1_ ``` Doctrine\DBAL\Exception\InvalidFieldNameException: An exception occurred while executing 'SELECT r0_.first_name AS first_name0, r0_.last_name AS last_name1, r0_.email AS email2, r0_.phone AS phone3, r0_.affiliate_id AS affiliate_id4, r0_.source_ip AS source_ip5, r0_.time_to_call AS time_to_call6, r0_.extra AS extra7, r0_.hash AS hash8, r0_.user_comments AS user_comments9, r0_.created_date AS created_date10, r0_.modified_date AS modified_date11, r1_.rate AS rate12, r1_.property_value AS property_value13, r1_.mortgage_amount AS mortgage_amount14, r1_.application_id AS application_id15, r0_.product_id AS product_id16, r0_.source_id AS source_id17, r0_.province_id AS province_id18, r1_.mortgage_id AS mortgage_id19, r1_.purchase_type_id AS purchase_type_id20 FROM user_application_mortgages r1_ INNER JOIN user_applications r0_ ON r1_.application_id = r0_.application_id LIMIT 10': Unknown column 'r0_.application_id' in 'on clause' ``` Any idea how this might be solved or what I'm doing wrong? Thanks!
Author
Owner

@coder-pm commented on GitHub (Oct 16, 2018):

Did you figure out any workaround?

@coder-pm commented on GitHub (Oct 16, 2018): Did you figure out any workaround?
Author
Owner

@fsou commented on GitHub (May 17, 2019):

I've stepped into this problem too.

This issue occurs whenever I try to get either the parent or subclass, switching the Id.

Haven't found any Workaround.

Have found the place where to check if there was an AttributeMapping and set the mappped column name and not the $idColumn.

@fsou commented on GitHub (May 17, 2019): I've stepped into this problem too. This issue occurs whenever I try to get either the parent or subclass, switching the Id. Haven't found any Workaround. Have found the place where to check if there was an AttributeMapping and set the mappped column name and not the $idColumn.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5030