Skip to content

Loading…

DDC-1494: Query results are overwritten by previous query. #2123

Open
doctrinebot opened this Issue · 7 comments

2 participants

@doctrinebot

Jira issue originally created by user monk.e.boy:

I am running a query that JOINs three tables, with a simple WHERE:

$q = $em->createQuery("

SELECT cat, n, c
FROM Project*Model*NoticeCategory cat
JOIN cat.notices n
JOIN n.chapters c
WHERE
c.id = :chapter_id

");

When I do this:

  $q->setParameter('chapter_id', 1);
  $a = $q->getResult();

  $q->setParameter('chapter_id', 2);
  $b = $q->getResult();

$b always has the wrong results. Running the following code:

  $q->setParameter('chapter_id', 1);
  $a = $q->getResult();

  $q->setParameter('chapter_id', 2);
  $b = $q->getResult();
  $z = $q->getArrayResult();

BUG Results: $b != $z (getArrayResult IS CORRECT, it refreshes the results) Note: $a==$b (which is wrong)

Explanation:

There is a chapter table, this has a many-to-many join to notices (these are meta info
about the chapter -- a little like tagging a blog post) the notices are grouped into
categories.

Data model:

/****
 * @Entity
 * @Table(name="chapter")
 */
class Project*Model*Chapter
{
    /****
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /*** @Column(type="string") **/
    private $title;

    /****
     * @ManyToMany(targetEntity="Project*Model*Notice", mappedBy="chapters")
     */
    private $notices;

    .... /lots of code snipped/ ....

}


/****
 * @Entity
 * @Table(name="notice")
 */
class Project*Model*Notice
{
    /****
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /*** @Column(type="string") **/
    private $title;

    /****
     * @ManyToMany(targetEntity="Project*Model*Chapter", inversedBy="notices")
     * @JoinTable(name="chapter_notice")
     */
    private $chapters;

    /****
     * @ManyToOne(targetEntity="Project*Model*NoticeCategory", inversedBy="notices")
     */
    private $notice_category;

    .... /lots of code snipped/ ....

}

/****
 * @Entity
 * @Table(name="notice_category")
 */
class Project*Model*NoticeCategory
{
    /****
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;
    /*** @Column(type="string") **/
    private $title;

    /****
     * Bidirectional - One-To-Many (INVERSE SIDE)
     *
     * @OneToMany(targetEntity="Project*Model_Notice", mappedBy="notice*category", cascade={"persist", "remove"})
     */
    private $notices;

    .... /lots of code snipped/ ....

}

Data fixtures:

$tools = new \Project*Model*NoticeCategory;
$tools->setTitle('Tools');

$spanner = new \Project*Model*Notice;
$spanner->setTitle('spanner');
$tools->addNotice($spanner);

$drill = new \Project*Model*Notice;
$drill->setTitle('power drill');
$tools->addNotice($drill);

$this->em->persist($tools);
$this->em->flush();

$tools = new \Project*Model*NoticeCategory;
$tools->setTitle('Safety');

$gloves = new \Project*Model*Notice;
$gloves->setTitle('gloves');
$tools->addNotice($gloves);

$goggles = new \Project*Model*Notice;
$goggles->setTitle('goggles');
$tools->addNotice($goggles);

$this->em->persist($tools);
$this->em->flush();

$chapter1 = new \Project*Model*Chapter;
$chapter1->setTitle('Chapter 1');
$this->em->persist($chapter1);

$chapter2 = new \Project*Model*Chapter;
$chapter2->setTitle('Chapter 2');
$this->em->persist($chapter2);

$chapter1->addNotice($spanner);
$chapter1->addNotice($gloves);

$chapter2->addNotice($spanner);
$chapter2->addNotice($gloves);
$chapter2->addNotice($drill);
$chapter2->addNotice($goggles);

// now persist and flush everything

Initial investigation:

I think it has something to do with HINT_REFRESH ? Stepping through:

ObjectHydrator->_hydrateRow
ObjectHydrator->_getEntity

when it requests the ProjectModelCategory from the unit of work, it
seems that the second query is simply grabbing the cached results from
the first results. This MUST be wrong as the second query uses a
different query (the ID changes) and all the results are wrong.

@doctrinebot

Comment created by @beberlei:

Fixed formatting

@doctrinebot

Comment created by @beberlei:

are you using result caching?

@doctrinebot

Comment created by monk.e.boy:

This is part of my bootstrap
,
,

$config = new \Doctrine\ORM\Configuration();

$cache = new \Doctrine\Common\Cache\ArrayCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);

// driver: schema
$driver = $config->newDefaultAnnotationDriver(
    APPLICATION_PATH . '/models'
);
$config->setMetadataDriverImpl($driver);
@doctrinebot

Comment created by @beberlei:

Cannot reproduce it with the script attached. Can you try to modify this to fail or write your own testcase?

@doctrinebot

Comment created by @beberlei:

Downgraded

@doctrinebot

Comment created by @asm89:

Please provide extra feedback.

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.