DDC-707: Discriminator column cannot be used in the query builder #874

Closed
opened 2026-01-22 12:53:24 +01:00 by admin · 12 comments
Owner

Originally created by @doctrinebot on GitHub (Jul 21, 2010).

Jira issue originally created by user gedrox:

When having entities

/****
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"b" = "B", "c" = "C"})
 */
class A {
    /*** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;
}
/*** @Entity **/
class B extends A {}
/*** @Entity **/
class C extends A {}

The DQL "SELECT a FROM A a ORDER BY a.discr" cannot be run because of error "Class A has no field or association named discr".

This would be very useful when having to read both - B and C data using one query, but in particular order.

Originally created by @doctrinebot on GitHub (Jul 21, 2010). Jira issue originally created by user gedrox: When having entities ``` /**** * @Entity * @InheritanceType("SINGLE_TABLE") * @DiscriminatorColumn(name="discr", type="string") * @DiscriminatorMap({"b" = "B", "c" = "C"}) */ class A { /*** @Id @Column(type="integer") @GeneratedValue **/ protected $id; } /*** @Entity **/ class B extends A {} /*** @Entity **/ class C extends A {} ``` The DQL "SELECT a FROM A a ORDER BY a.discr" cannot be run because of error "Class A has no field or association named discr". This would be very useful when having to read both - B and C data using one query, but in particular order.
admin added the Improvement label 2026-01-22 12:53:24 +01:00
admin closed this issue 2026-01-22 12:53:24 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jul 21, 2010):

Comment created by gedrox:

Attached fast workaround for this issue

@doctrinebot commented on GitHub (Jul 21, 2010): Comment created by gedrox: Attached fast workaround for this issue
Author
Owner

@doctrinebot commented on GitHub (Jul 21, 2010):

Comment created by romanb:

Duplicate of http://www.doctrine-project.org/jira/browse/DDC-482

Discriminator columns are not part of the object model and will thus not be available in DQL.

@doctrinebot commented on GitHub (Jul 21, 2010): Comment created by romanb: Duplicate of http://www.doctrine-project.org/jira/browse/[DDC-482](http://www.doctrine-project.org/jira/browse/DDC-482) Discriminator columns are not part of the object model and will thus not be available in DQL.
Author
Owner

@doctrinebot commented on GitHub (Jul 21, 2010):

Issue was closed with resolution "Duplicate"

@doctrinebot commented on GitHub (Jul 21, 2010): Issue was closed with resolution "Duplicate"
Author
Owner

@doctrinebot commented on GitHub (Jul 21, 2010):

Comment created by gedrox:

If I redefine the "discr" as the column of the base class (and setting the default values in classes B and C for it) there are 2 places where exception is raised in Doctrine. Works after commenting them out.

Maybe there is some elegant solution for this problem without changing Doctrine?

@doctrinebot commented on GitHub (Jul 21, 2010): Comment created by gedrox: If I redefine the "discr" as the column of the base class (and setting the default values in classes B and C for it) there are 2 places where exception is raised in Doctrine. Works after commenting them out. Maybe there is some elegant solution for this problem without changing Doctrine?
Author
Owner

@doctrinebot commented on GitHub (Jul 21, 2010):

Comment created by @beberlei:

Yes the elegant solution is described in DDC-482, its not yet implemented though.

@doctrinebot commented on GitHub (Jul 21, 2010): Comment created by @beberlei: Yes the elegant solution is described in [DDC-482](http://www.doctrine-project.org/jira/browse/DDC-482), its not yet implemented though.
Author
Owner

@doctrinebot commented on GitHub (May 11, 2012):

Comment created by robmasters:

I see this was marked as duplicating a resolved thread, but I've just come across what I think is the same problem. Doctrine 2.1 isn't checking the discriminator column name when using the query builder (or in my case, a findBy method) - it only checks the columnNames and associationMappings arrays. See the following stackoverflow post for more information:

http://stackoverflow.com/questions/10550926/doctrine2-1-finding-by-discriminatorcolumn-results-in-unknown-field-exception

Thanks

@doctrinebot commented on GitHub (May 11, 2012): Comment created by robmasters: I see this was marked as duplicating a resolved thread, but I've just come across what I think is the same problem. Doctrine 2.1 isn't checking the discriminator column name when using the query builder (or in my case, a findBy method) - it only checks the columnNames and associationMappings arrays. See the following stackoverflow post for more information: http://stackoverflow.com/questions/10550926/doctrine2-1-finding-by-discriminatorcolumn-results-in-unknown-field-exception Thanks
Author
Owner

@doctrinebot commented on GitHub (Dec 13, 2015):

Imported 1 attachments from Jira into https://gist.github.com/a7d6d5f84741cc4f012b

@doctrinebot commented on GitHub (Dec 13, 2015): Imported 1 attachments from Jira into https://gist.github.com/a7d6d5f84741cc4f012b - [10701_vcs-diff510753717072074737.patch](https://gist.github.com/a7d6d5f84741cc4f012b#file-10701_vcs-diff510753717072074737-patch)
Author
Owner

@jarrettj commented on GitHub (Aug 29, 2017):

Hi,

Good day.

Is there any documentation on how to use it in the findBy method? I've tried using the colon notation with no success. Maybe it is a zend framework 2 specific issue?

namespace Application\Entity\Compliances;
/**
 * @package Application
 *
 * @ORM\Cache("READ_WRITE")
 * @ORM\Entity(repositoryClass="Application\Repository\Compliances\Report")
 * @ORM\Table(name="ComplianceReport")
 *
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"report" = "Report", "reviewedReport" = "ReviewedReport"})
 * 
 * @Annotation\Name("ComplianceReport")
 * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 *
 */
class Report {…}

namespace Application\Entity\Compliances;
/**
 * @package Application
 *
 * @ORM\Cache("READ_WRITE")
 * @ORM\Entity(repositoryClass="Application\Repository\Compliances\ReviewedReport")
 * @ORM\Table(name="ReviewedReport")
 *
 * @Annotation\Name("ReviewedReport")
 * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 *
 */
class ReviewedReport extends Report {…}

I’m trying to do a:

$reports = $this->getEm()->getRepository('Application\Entity\Compliances:Report')
               ->findBy($fieldValues);

When I step through the code, it dies at Doctrine\ORM\Configuration line 195:

if ( ! isset($this->_attributes['entityNamespaces'][$entityNamespaceAlias])) {
        throw ORMException::unknownEntityNamespace($entityNamespaceAlias);
}

There are no values set in $this->_attributes['entityNamespaces'].

Thanks.
JJ

@jarrettj commented on GitHub (Aug 29, 2017): Hi, Good day. Is there any documentation on how to use it in the findBy method? I've tried using the colon notation with no success. Maybe it is a zend framework 2 specific issue? ``` namespace Application\Entity\Compliances; /** * @package Application * * @ORM\Cache("READ_WRITE") * @ORM\Entity(repositoryClass="Application\Repository\Compliances\Report") * @ORM\Table(name="ComplianceReport") * * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="discr", type="string") * @ORM\DiscriminatorMap({"report" = "Report", "reviewedReport" = "ReviewedReport"}) * * @Annotation\Name("ComplianceReport") * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty") * */ class Report {…} namespace Application\Entity\Compliances; /** * @package Application * * @ORM\Cache("READ_WRITE") * @ORM\Entity(repositoryClass="Application\Repository\Compliances\ReviewedReport") * @ORM\Table(name="ReviewedReport") * * @Annotation\Name("ReviewedReport") * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty") * */ class ReviewedReport extends Report {…} ``` I’m trying to do a: ``` $reports = $this->getEm()->getRepository('Application\Entity\Compliances:Report') ->findBy($fieldValues); ``` When I step through the code, it dies at Doctrine\ORM\Configuration line 195: ``` if ( ! isset($this->_attributes['entityNamespaces'][$entityNamespaceAlias])) { throw ORMException::unknownEntityNamespace($entityNamespaceAlias); } ``` There are no values set in $this->_attributes['entityNamespaces']. Thanks. JJ
Author
Owner

@Ocramius commented on GitHub (Aug 29, 2017):

You can't: you will need DQL and the INSTANCEOF operator.

On 29 Aug 2017 10:02 AM, "jarrett jordaan" notifications@github.com wrote:

Hi,

Good day.

Is there any documentation on how to use it in the findBy method? I've
tried using the colon notation with no success. Maybe it is a zend
framework 2 specific issue?

namespace Application\Entity\Compliances;
/**

  • @package Application
  • @ORM\Cache("READ_WRITE")
  • @ORM\Entity(repositoryClass="Application\Repository\Compliances\Report")
  • @ORM\Table(name="ComplianceReport")
  • @ORM\InheritanceType("JOINED")
  • @ORM\DiscriminatorColumn(name="discr", type="string")
  • @ORM\DiscriminatorMap({"report" = "Report", "reviewedReport" = "ReviewedReport"})
  • @Annotation\Name("ComplianceReport")
  • @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")

*/
class Report {…}

namespace Application\Entity\Compliances;
/**

  • @package Application
  • @ORM\Cache("READ_WRITE")
  • @ORM\Entity(repositoryClass="Application\Repository\Compliances\ReviewedReport")
  • @ORM\Table(name="ReviewedReport")
  • @Annotation\Name("ReviewedReport")
  • @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")

*/
class ReviewedReport extends Report {…}

I’m trying to do a:

$reports = $this->getEm()->getRepository('Application\Entity\Compliances:Report')
->findBy($fieldValues);

Thanks.
JJ


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/doctrine2/issues/5220#issuecomment-325586319,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakNXX4Ia_CaIWPled38vRJas2R2oAks5sc8WkgaJpZM4G0XGq
.

@Ocramius commented on GitHub (Aug 29, 2017): You can't: you will need DQL and the `INSTANCEOF` operator. On 29 Aug 2017 10:02 AM, "jarrett jordaan" <notifications@github.com> wrote: > Hi, > > Good day. > > Is there any documentation on how to use it in the findBy method? I've > tried using the colon notation with no success. Maybe it is a zend > framework 2 specific issue? > > namespace Application\Entity\Compliances; > /** > * @package Application > * > * @ORM\Cache("READ_WRITE") > * @ORM\Entity(repositoryClass="Application\Repository\Compliances\Report") > * @ORM\Table(name="ComplianceReport") > * > * @ORM\InheritanceType("JOINED") > * @ORM\DiscriminatorColumn(name="discr", type="string") > * @ORM\DiscriminatorMap({"report" = "Report", "reviewedReport" = "ReviewedReport"}) > * > * @Annotation\Name("ComplianceReport") > * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty") > * > */ > class Report {…} > > namespace Application\Entity\Compliances; > /** > * @package Application > * > * @ORM\Cache("READ_WRITE") > * @ORM\Entity(repositoryClass="Application\Repository\Compliances\ReviewedReport") > * @ORM\Table(name="ReviewedReport") > * > * @Annotation\Name("ReviewedReport") > * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty") > * > */ > class ReviewedReport extends Report {…} > > I’m trying to do a: > > $reports = $this->getEm()->getRepository('Application\Entity\Compliances:Report') > ->findBy($fieldValues); > > Thanks. > JJ > > — > You are receiving this because you are subscribed to this thread. > Reply to this email directly, view it on GitHub > <https://github.com/doctrine/doctrine2/issues/5220#issuecomment-325586319>, > or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAJakNXX4Ia_CaIWPled38vRJas2R2oAks5sc8WkgaJpZM4G0XGq> > . >
Author
Owner

@jarrettj commented on GitHub (Aug 29, 2017):

Thanks @Ocramius.

Is this specific to zf2 though? Googling around seems it works for symphony.

@jarrettj commented on GitHub (Aug 29, 2017): Thanks @Ocramius. Is this specific to zf2 though? Googling around seems it works for symphony.
Author
Owner

@Ocramius commented on GitHub (Aug 29, 2017):

No, it is generally about D2. Also, this is the D2 repo: zf/sf specifics
are irrelevant.

On 29 Aug 2017 10:26 AM, "jarrett jordaan" notifications@github.com wrote:

Thanks @Ocramius https://github.com/ocramius.

Is this specific to zf2 though? Googling around seems it works for
symphony.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/doctrine2/issues/5220#issuecomment-325591910,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakN0nGrb68t--OnNtAxdqT5DO3sRGks5sc8tSgaJpZM4G0XGq
.

@Ocramius commented on GitHub (Aug 29, 2017): No, it is generally about D2. Also, this is the D2 repo: zf/sf specifics are irrelevant. On 29 Aug 2017 10:26 AM, "jarrett jordaan" <notifications@github.com> wrote: > Thanks @Ocramius <https://github.com/ocramius>. > > Is this specific to zf2 though? Googling around seems it works for > symphony. > > — > You are receiving this because you were mentioned. > Reply to this email directly, view it on GitHub > <https://github.com/doctrine/doctrine2/issues/5220#issuecomment-325591910>, > or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAJakN0nGrb68t--OnNtAxdqT5DO3sRGks5sc8tSgaJpZM4G0XGq> > . >
Author
Owner

@jarrettj commented on GitHub (Aug 29, 2017):

Awesome, thanks again for the speedy help @Ocramius! 👍

@jarrettj commented on GitHub (Aug 29, 2017): Awesome, thanks again for the speedy help @Ocramius! 👍
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#874