Wrong result with custom left join #5509

Closed
opened 2026-01-22 15:09:32 +01:00 by admin · 2 comments
Owner

Originally created by @Sohorev on GitHub (Apr 22, 2017).

namespace FrontfaceBundle\Model\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Review
 *
 * @ORM\Table(name="review")
 * @ORM\Entity
 */
class Review {
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="bigint")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="SEQUENCE")
   * @ORM\SequenceGenerator(sequenceName="review_id_seq", allocationSize=1, initialValue=1)
   */
  private $id;

  /**
   * Get id
   *
   * @return integer
   */
  public function getId() {
    return $this->id;
  }
}
namespace FrontfaceBundle\Model\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * ReviewShort
 *
 * @ORM\Table(name="review_short")
 * @ORM\Entity
 */
class ReviewShort {

  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="bigint")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="SEQUENCE")
   * @ORM\SequenceGenerator(sequenceName="review_short_id_seq", allocationSize=1, initialValue=1)
   */
  private $id;

  /**
   * Set review
   *
   * @param \FrontfaceBundle\Model\Entity\Review $review
   *
   * @return ReviewShort
   */
  public function setReview(\FrontfaceBundle\Model\Entity\Review $review = null) {
    $this->review = $review;

    return $this;
  }

  /**
   * Get review
   *
   * @return \FrontfaceBundle\Model\Entity\Review
   */
  public function getReview() {
    return $this->review;
  }
}
    $qb = $this->getEntityManager()->createQueryBuilder()
      ->select(['r, rshort'])
      ->from('FrontfaceBundle:Review', 'r')
      ->leftJoin('FrontfaceBundle:ReviewShort', 'rshort', Expr\Join::WITH, 'r.id = rshort.review')
    ;
    print_r(count($qb->getQuery()->getResult()));
    // will print 164
    print_r($qb->getQuery()->getSQL());
   // SELECT r0_.id AS id_0, r1_.id AS id_1, r1_.review_id AS review_id_2 FROM review r0_ LEFT JOIN review_short r1_ ON (r0_.id = r1_.review_id)

When I run the query SELECT r0_.id AS id_0, r1_.id AS id_1, r1_.review_id AS review_id_2 FROM review r0_ LEFT JOIN review_short r1_ ON (r0_.id = r1_.review_id) on the database, I get 82 rows (it is right). But $qb->getQuery()->getResult() return 164 rows - it is wrong

Originally created by @Sohorev on GitHub (Apr 22, 2017). ``` namespace FrontfaceBundle\Model\Entity; use Doctrine\ORM\Mapping as ORM; /** * Review * * @ORM\Table(name="review") * @ORM\Entity */ class Review { /** * @var integer * * @ORM\Column(name="id", type="bigint") * @ORM\Id * @ORM\GeneratedValue(strategy="SEQUENCE") * @ORM\SequenceGenerator(sequenceName="review_id_seq", allocationSize=1, initialValue=1) */ private $id; /** * Get id * * @return integer */ public function getId() { return $this->id; } } ``` ``` namespace FrontfaceBundle\Model\Entity; use Doctrine\ORM\Mapping as ORM; /** * ReviewShort * * @ORM\Table(name="review_short") * @ORM\Entity */ class ReviewShort { /** * @var integer * * @ORM\Column(name="id", type="bigint") * @ORM\Id * @ORM\GeneratedValue(strategy="SEQUENCE") * @ORM\SequenceGenerator(sequenceName="review_short_id_seq", allocationSize=1, initialValue=1) */ private $id; /** * Set review * * @param \FrontfaceBundle\Model\Entity\Review $review * * @return ReviewShort */ public function setReview(\FrontfaceBundle\Model\Entity\Review $review = null) { $this->review = $review; return $this; } /** * Get review * * @return \FrontfaceBundle\Model\Entity\Review */ public function getReview() { return $this->review; } } ``` ``` $qb = $this->getEntityManager()->createQueryBuilder() ->select(['r, rshort']) ->from('FrontfaceBundle:Review', 'r') ->leftJoin('FrontfaceBundle:ReviewShort', 'rshort', Expr\Join::WITH, 'r.id = rshort.review') ; print_r(count($qb->getQuery()->getResult())); // will print 164 print_r($qb->getQuery()->getSQL()); // SELECT r0_.id AS id_0, r1_.id AS id_1, r1_.review_id AS review_id_2 FROM review r0_ LEFT JOIN review_short r1_ ON (r0_.id = r1_.review_id) ``` When I run the query `SELECT r0_.id AS id_0, r1_.id AS id_1, r1_.review_id AS review_id_2 FROM review r0_ LEFT JOIN review_short r1_ ON (r0_.id = r1_.review_id)` on the database, I get 82 rows (it is right). But $qb->getQuery()->getResult() return 164 rows - it is wrong
admin added the BugMissing Tests labels 2026-01-22 15:09:33 +01:00
admin closed this issue 2026-01-22 15:09:33 +01:00
Author
Owner

@Ocramius commented on GitHub (Apr 22, 2017):

There's a lot of confusion with your intent in this query, since:

  • You are not using an association
  • Instead, you are using a custom WITH join condition

Could you print out the generated DQL string, and explain what you wanted
to achieve, and why no association is being used?

On 22 Apr 2017 7:20 a.m., "Sohorev Sergey" notifications@github.com wrote:

namespace FrontfaceBundle\Model\Entity;

use Doctrine\ORM\Mapping as ORM;

/**

  • Review
  • @ORM\Table(name="review")
  • @ORM\Entity
    /
    class Review {
    /
    *
    • @var integer
    • @ORM\Column(name="id", type="bigint")
    • @ORM\Id
    • @ORM\GeneratedValue(strategy="SEQUENCE")
    • @ORM\SequenceGenerator(sequenceName="review_id_seq",
      allocationSize=1, initialValue=1)
      */
      private $id;

/**

  • Get id
  • @return integer
    */
    public function getId() {
    return $this->id;
    }
    }

namespace FrontfaceBundle\Model\Entity;

use Doctrine\ORM\Mapping as ORM;

/**

  • ReviewShort
  • @ORM\Table(name="review_short")
  • @ORM\Entity
    */
    class ReviewShort {

/**

  • @var integer
  • @ORM\Column(name="id", type="bigint")
  • @ORM\Id
  • @ORM\GeneratedValue(strategy="SEQUENCE")
  • @ORM\SequenceGenerator(sequenceName="review_short_id_seq",
    allocationSize=1, initialValue=1)
    */
    private $id;

/**

  • Set review
  • @param \FrontfaceBundle\Model\Entity\Review $review
  • @return ReviewShort
    */
    public function setReview(\FrontfaceBundle\Model\Entity\Review
    $review = null) {
    $this->review = $review;
return $this;

}

/**

  • Get review
  • @return \FrontfaceBundle\Model\Entity\Review
    */
    public function getReview() {
    return $this->review;
    }
    }
$qb = $this->getEntityManager()->createQueryBuilder()
  ->select(['r, rshort'])
  ->from('FrontfaceBundle:Review', 'r')
  ->leftJoin('FrontfaceBundle:ReviewShort', 'rshort',

Expr\Join::WITH, 'r.id = rshort.review')
;
print_r(count($qb->getQuery()->getResult()));
// will print 164
print_r($qb->getQuery()->getSQL());
// SELECT r0_.id AS id_0, r1_.id AS id_1, r1_.review_id AS
review_id_2 FROM review r0_ LEFT JOIN review_short r1_ ON (r0_.id =
r1_.review_id)

When I run the query SELECT r0_.id AS id_0, r1_.id AS id_1, r1_.review_id
AS review_id_2 FROM review r0_ LEFT JOIN review_short r1_ ON (r0_.id =
r1_.review_id) on the database, I get 82 rows (it is right). But
$qb->getQuery()->getResult() return 164 rows - it is wrong


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/6411, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakHhuS8FegBg4EbqftxMXjaYRSpqgks5ryY4cgaJpZM4NE-oB
.

@Ocramius commented on GitHub (Apr 22, 2017): There's a lot of confusion with your intent in this query, since: * You are not using an association * Instead, you are using a custom `WITH` join condition Could you print out the generated DQL string, and explain what you wanted to achieve, and why no association is being used? On 22 Apr 2017 7:20 a.m., "Sohorev Sergey" <notifications@github.com> wrote: namespace FrontfaceBundle\Model\Entity; use Doctrine\ORM\Mapping as ORM; /** * Review * * @ORM\Table(name="review") * @ORM\Entity */ class Review { /** * @var integer * * @ORM\Column(name="id", type="bigint") * @ORM\Id * @ORM\GeneratedValue(strategy="SEQUENCE") * @ORM\SequenceGenerator(sequenceName="review_id_seq", allocationSize=1, initialValue=1) */ private $id; /** * Get id * * @return integer */ public function getId() { return $this->id; } } namespace FrontfaceBundle\Model\Entity; use Doctrine\ORM\Mapping as ORM; /** * ReviewShort * * @ORM\Table(name="review_short") * @ORM\Entity */ class ReviewShort { /** * @var integer * * @ORM\Column(name="id", type="bigint") * @ORM\Id * @ORM\GeneratedValue(strategy="SEQUENCE") * @ORM\SequenceGenerator(sequenceName="review_short_id_seq", allocationSize=1, initialValue=1) */ private $id; /** * Set review * * @param \FrontfaceBundle\Model\Entity\Review $review * * @return ReviewShort */ public function setReview(\FrontfaceBundle\Model\Entity\Review $review = null) { $this->review = $review; return $this; } /** * Get review * * @return \FrontfaceBundle\Model\Entity\Review */ public function getReview() { return $this->review; } } $qb = $this->getEntityManager()->createQueryBuilder() ->select(['r, rshort']) ->from('FrontfaceBundle:Review', 'r') ->leftJoin('FrontfaceBundle:ReviewShort', 'rshort', Expr\Join::WITH, 'r.id = rshort.review') ; print_r(count($qb->getQuery()->getResult())); // will print 164 print_r($qb->getQuery()->getSQL()); // SELECT r0_.id AS id_0, r1_.id AS id_1, r1_.review_id AS review_id_2 FROM review r0_ LEFT JOIN review_short r1_ ON (r0_.id = r1_.review_id) When I run the query SELECT r0_.id AS id_0, r1_.id AS id_1, r1_.review_id AS review_id_2 FROM review r0_ LEFT JOIN review_short r1_ ON (r0_.id = r1_.review_id) on the database, I get 82 rows (it is right). But $qb->getQuery()->getResult() return 164 rows - it is wrong — 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/6411>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAJakHhuS8FegBg4EbqftxMXjaYRSpqgks5ryY4cgaJpZM4NE-oB> .
Author
Owner

@beberlei commented on GitHub (Dec 8, 2020):

Closing for lack of feedback.

@beberlei commented on GitHub (Dec 8, 2020): Closing for lack of feedback.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5509