DDC-1595: Wrong count in relation with inheritance #1998

Closed
opened 2026-01-22 13:37:11 +01:00 by admin · 7 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 11, 2012).

Originally assigned to: @guilhermeblanco on GitHub.

Jira issue originally created by user ghennady:

I have some code like this:

/****
 * @Orm\Entity
 * @Orm\Table(name="resource")
 * @Orm\InheritanceType("SINGLE_TABLE")
 * @Orm\DiscriminatorColumn(name="type", type="string")
 * @Orm\DiscriminatorMap({"Container" = "Container", "News"="News"})
 */
abstract class Resource
{
}
/****
 * @Orm\Entity
 */
class Container extends Resource
{
    /****
     * @Orm\ManyToMany(targetEntity="News", fetch="EXTRA_LAZY")
     * @Orm\JoinTable(name="resource_path",
     *      joinColumns={@Orm\JoinColumn(name="parent", referencedColumnName="id")},
     *      inverseJoinColumns={@Orm\JoinColumn(name="item", referencedColumnName="id")}
     *      )
     */
    protected $news;

    public function getNews()
    {
        return $this->news;
    }
}
/****
 * @Orm\Entity
 */
class News extends Resource
{
}
$container = $repository->find($id);
$news = $container->getNews();


$news->getValues(); 
/* generates SQL like this: 
**  SELECT ** FROM resource t0
*  INNER JOIN resource*path ON t0.id = resource*path.item 
*  WHERE resource_path.parent = ? AND t0.type IN ('News') 
*/

$news->count(); 
/* generates SQL like this: 
**  SELECT COUNT(**) FROM resource_path t WHERE parent = ? 
*
* Expected SQL like this:
**  SELECT count(**) FROM resource t0
*  INNER JOIN resource*path ON t0.id = resource*path.item
*  WHERE resource_path.parent = ? AND t0.type IN ('News')
*/


Originally created by @doctrinebot on GitHub (Jan 11, 2012). Originally assigned to: @guilhermeblanco on GitHub. Jira issue originally created by user ghennady: I have some code like this: ``` /**** * @Orm\Entity * @Orm\Table(name="resource") * @Orm\InheritanceType("SINGLE_TABLE") * @Orm\DiscriminatorColumn(name="type", type="string") * @Orm\DiscriminatorMap({"Container" = "Container", "News"="News"}) */ abstract class Resource { } ``` ``` /**** * @Orm\Entity */ class Container extends Resource { /**** * @Orm\ManyToMany(targetEntity="News", fetch="EXTRA_LAZY") * @Orm\JoinTable(name="resource_path", * joinColumns={@Orm\JoinColumn(name="parent", referencedColumnName="id")}, * inverseJoinColumns={@Orm\JoinColumn(name="item", referencedColumnName="id")} * ) */ protected $news; public function getNews() { return $this->news; } } ``` ``` /**** * @Orm\Entity */ class News extends Resource { } ``` ``` $container = $repository->find($id); $news = $container->getNews(); $news->getValues(); /* generates SQL like this: ** SELECT ** FROM resource t0 * INNER JOIN resource*path ON t0.id = resource*path.item * WHERE resource_path.parent = ? AND t0.type IN ('News') */ $news->count(); /* generates SQL like this: ** SELECT COUNT(**) FROM resource_path t WHERE parent = ? * * Expected SQL like this: ** SELECT count(**) FROM resource t0 * INNER JOIN resource*path ON t0.id = resource*path.item * WHERE resource_path.parent = ? AND t0.type IN ('News') */ ```
admin added the Bug label 2026-01-22 13:37:11 +01:00
admin closed this issue 2026-01-22 13:37:11 +01:00
Author
Owner

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

@doctrinebot commented on GitHub (Jan 11, 2012): - is referenced by [DDC-3104: Invalid count on EXTRA LAZY collection of SINGLE TABLE entities](http://www.doctrine-project.org/jira/browse/DDC-3104)
Author
Owner

@doctrinebot commented on GitHub (Jan 13, 2012):

Comment created by @guilhermeblanco:

How can this be returning you incorrect values for count?

The second query retrieves based on the join table, which is supposed to have only the linking between Container and News.
It is expected that given a Container ID, all you want to know is the actual number of associated News, which a simple check to join table is enough, no matter which inheritance strategy you are looking into.

Marking ticket as invalid.
Provide better information and explain how it is affecting you if my explanation is not enough.

@doctrinebot commented on GitHub (Jan 13, 2012): Comment created by @guilhermeblanco: How can this be returning you incorrect values for count? The second query retrieves based on the join table, which is supposed to have only the linking between Container and News. It is expected that given a Container ID, all you want to know is the actual number of associated News, which a simple check to join table is enough, no matter which inheritance strategy you are looking into. Marking ticket as invalid. Provide better information and explain how it is affecting you if my explanation is not enough.
Author
Owner

@doctrinebot commented on GitHub (Jan 13, 2012):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Jan 13, 2012): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Jan 13, 2012):

Comment created by ghennady:

Thank you for your comprehensive answer. My fault, I may not be correctly described the problem.

As I understood from you answer, the following code is not correct

/****
 * @Orm\Entity
 */
class Container extends Resource
{
    /****
     * @Orm\ManyToMany(targetEntity="News", fetch="EXTRA_LAZY")
     * @Orm\JoinTable(name="resource_path",
     *      joinColumns={@Orm\JoinColumn(name="parent", referencedColumnName="id")},
     *      inverseJoinColumns={@Orm\JoinColumn(name="item", referencedColumnName="id")}
     *      )
     */
    protected $news;

    public function getNews()
    {
        return $this->news;
    }

    /****
     * @Orm\ManyToMany(targetEntity="Container", fetch="EXTRA_LAZY")
     * @Orm\JoinTable(name="resource_path",
     *      joinColumns={@Orm\JoinColumn(name="parent", referencedColumnName="id")},
     *      inverseJoinColumns={@Orm\JoinColumn(name="item", referencedColumnName="id")}
     *      )
     */
    protected $containers;

    public function getContainers()
    {
        return $this->containers;
    }

}

in other words in the table resource_path can not be at the same time records linking Container with Container and Container with News.

@doctrinebot commented on GitHub (Jan 13, 2012): Comment created by ghennady: Thank you for your comprehensive answer. My fault, I may not be correctly described the problem. As I understood from you answer, the following code is not correct ``` /**** * @Orm\Entity */ class Container extends Resource { /**** * @Orm\ManyToMany(targetEntity="News", fetch="EXTRA_LAZY") * @Orm\JoinTable(name="resource_path", * joinColumns={@Orm\JoinColumn(name="parent", referencedColumnName="id")}, * inverseJoinColumns={@Orm\JoinColumn(name="item", referencedColumnName="id")} * ) */ protected $news; public function getNews() { return $this->news; } /**** * @Orm\ManyToMany(targetEntity="Container", fetch="EXTRA_LAZY") * @Orm\JoinTable(name="resource_path", * joinColumns={@Orm\JoinColumn(name="parent", referencedColumnName="id")}, * inverseJoinColumns={@Orm\JoinColumn(name="item", referencedColumnName="id")} * ) */ protected $containers; public function getContainers() { return $this->containers; } } ``` in other words in the table resource_path can not be at the same time records linking Container with Container and Container with News.
Author
Owner

@doctrinebot commented on GitHub (Jan 13, 2012):

Comment created by alex_pogodin:

The problem here is that resource_path knows nothing about type of the resource. At the same time, news collection contains only objects of resource.type == 'news'. That means, that if there' not only news inside container, but, probably another container(s), the $news->count() will return incorrect value (the sum of news and containers counts). That's the point.

@doctrinebot commented on GitHub (Jan 13, 2012): Comment created by alex_pogodin: The problem here is that `resource_path` knows nothing about type of the resource. At the same time, news collection contains only objects of `resource.type == 'news'`. That means, that if there' not only news inside container, but, probably another container(s), the `$news->count()` will return incorrect value (the sum of news and containers counts). That's the point.
Author
Owner

@doctrinebot commented on GitHub (Sep 4, 2012):

Comment created by i_yuki_i@hotmail.com:

Should this:

generates SQL like this: 
SELECT COUNT(*) FROM resource_path t WHERE parent = ? 

be:

generates SQL like this: 
SELECT COUNT(*) FROM resource_path t WHERE t.parent = ? 

Looks like parent columns is missing alias. Is that correct?

@doctrinebot commented on GitHub (Sep 4, 2012): Comment created by i_yuki_i@hotmail.com: Should this: ``` generates SQL like this: SELECT COUNT(*) FROM resource_path t WHERE parent = ? ``` be: ``` generates SQL like this: SELECT COUNT(*) FROM resource_path t WHERE t.parent = ? ``` Looks like parent columns is missing alias. Is that correct?
Author
Owner

@doctrinebot commented on GitHub (Apr 29, 2014):

Comment created by deatheriam:

Similar issue is described in http://www.doctrine-project.org/jira/browse/DDC-3104

@doctrinebot commented on GitHub (Apr 29, 2014): Comment created by deatheriam: Similar issue is described in http://www.doctrine-project.org/jira/browse/[DDC-3104](http://www.doctrine-project.org/jira/browse/DDC-3104)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1998