Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DDC-3346] #1277 find one with eager loads is failing #1280

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e36c7b0
DDC-3346 failing test example
scaytrase Jan 22, 2015
981cebb
Update test according to @Ocramius notes
scaytrase Jan 22, 2015
4c62d3b
#1277 DDC-3346 DDC-3531 - moved resultsetmapping into the newly creat…
Ocramius Jan 22, 2015
55930a3
#1277 DDC-3346 DDC-3531 - moved `selectColumnListSql` into the newly …
Ocramius Jan 22, 2015
b9f698c
#1277 DDC-3346 DDC-3531 - moved `selectJoinSql` into the newly create…
Ocramius Jan 22, 2015
2a7f149
#1277 DDC-3346 DDC-3531 - moved `sqlAliasCounter` into the newly crea…
Ocramius Jan 22, 2015
ebdfab8
#1277 DDC-3346 DDC-3531 - moved `sqlTableAliases` into the newly crea…
Ocramius Jan 22, 2015
8b9171c
#1277 DDC-3346 DDC-3531 - caching the currently in use persister context
Ocramius Jan 22, 2015
23a0d9a
#1277 DDC-3346 DDC-3531 - switch persister context at runtime
Ocramius Jan 22, 2015
67f60f2
#1277 DDC-3346 DDC-3531 - switching persister context at runtime, dep…
Ocramius Jan 22, 2015
a37fa97
#1277 DDC-3346 DDC-3531 - skipping joining of associations when limit…
Ocramius Jan 23, 2015
1672448
#1277 DDC-3346 DDC-3531 - renaming persister context switch for clarity
Ocramius Jan 23, 2015
157bf20
#1277 DDC-3346 DDC-3531 - additional tests for LIMIT and OFFSET repos…
Ocramius Jan 23, 2015
6e3ad49
#1277 DDC-3346 DDC-3531 - constants over string references
Ocramius Jan 23, 2015
16f447d
#1277 DDC-3346 DDC-3531 - correct usage of the model set (setUp/tearD…
Ocramius Jan 23, 2015
36bc448
#1277 DDC-3346 DDC-3531 - refactoring test code for simplicity/readab…
Ocramius Jan 23, 2015
04a271a
#1277 DDC-3346 DDC-3531 - refactoring test assets for readability
Ocramius Jan 23, 2015
39a8941
#1277 DDC-3346 DDC-3531 - minor CS fixes/cleanups: avoiding setters
Ocramius Jan 23, 2015
186c593
#1277 DDC-3346 DDC-3531 - proper bi-directional association setup
Ocramius Jan 23, 2015
dff3653
#1277 DDC-3346 DDC-3531 - enforcing `0` offset to avoid persisting mo…
Ocramius Jan 23, 2015
97ea6a7
#1277 DDC-3346 - removing array-based persister context handling (be…
Ocramius Jan 24, 2015
d4b278c
#1277 DDC-3346 - removing leftover comments
Ocramius Jan 25, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r'
$sql = $this->getSQLTableAlias($class->name, $tableAlias) . '.'
. $this->quoteStrategy->getColumnName($field, $class, $this->platform);

$this->rsm->addFieldResult($alias, $columnAlias, $field, $class->name);
$this->currentPersisterContext->rsm->addFieldResult($alias, $columnAlias, $field, $class->name);

if (isset($class->fieldMappings[$field]['requireSQLConversion'])) {
$type = Type::getType($class->getTypeOfField($field));
Expand All @@ -88,7 +88,7 @@ protected function getSelectJoinColumnSQL($tableAlias, $joinColumnName, $classNa
{
$columnAlias = $this->getSQLColumnAlias($joinColumnName);

$this->rsm->addMetaResult('r', $columnAlias, $joinColumnName, false, $type);
$this->currentPersisterContext->rsm->addMetaResult('r', $columnAlias, $joinColumnName, false, $type);

return $tableAlias . '.' . $joinColumnName . ' AS ' . $columnAlias;
}
Expand Down
172 changes: 97 additions & 75 deletions lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php

Large diffs are not rendered by default.

102 changes: 102 additions & 0 deletions lib/Doctrine/ORM/Persisters/Entity/CachedPersisterContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ORM\Persisters\Entity;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\Query\ResultSetMapping;

/**
* A swappable persister context to use as a container for the current
* generated query/resultSetMapping/type binding information.
*
* This class is a utility class to be used only by the persister API
*
* This object is highly mutable due to performance reasons. Same reasoning
* behind its properties being public.
*
* @author Marco Pivetta <ocramius@gmail.com>
*/
class CachedPersisterContext
{
/**
* Metadata object that describes the mapping of the mapped entity class.
*
* @var \Doctrine\ORM\Mapping\ClassMetadata
*/
public $class;

/**
* ResultSetMapping that is used for all queries. Is generated lazily once per request.
*
* @var \Doctrine\ORM\Query\ResultSetMapping
*/
public $rsm;

/**
* The SELECT column list SQL fragment used for querying entities by this persister.
* This SQL fragment is only generated once per request, if at all.
*
* @var string|null
*/
public $selectColumnListSql;

/**
* The JOIN SQL fragment used to eagerly load all many-to-one and one-to-one
* associations configured as FETCH_EAGER, as well as all inverse one-to-one associations.
*
* @var string
*/
public $selectJoinSql;

/**
* Counter for creating unique SQL table and column aliases.
*
* @var integer
*/
public $sqlAliasCounter = 0;

/**
* Map from class names (FQCN) to the corresponding generated SQL table aliases.
*
* @var array
*/
public $sqlTableAliases = array();

/**
* Whether this persistent context is considering limit operations applied to the selection queries
*
* @var bool
*/
public $handlesLimits;

/**
* @param ClassMetadata $class
* @param ResultSetMapping $rsm
* @param bool $handlesLimits
*/
public function __construct(
ClassMetadata $class,
ResultSetMapping $rsm,
$handlesLimits
) {
$this->class = $class;
$this->rsm = $rsm;
$this->handlesLimits = (bool) $handlesLimits;
}
}
17 changes: 9 additions & 8 deletions lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ public function delete($entity)
*/
public function getSelectSQL($criteria, $assoc = null, $lockMode = null, $limit = null, $offset = null, array $orderBy = null)
{
$this->switchPersisterContext($offset, $limit);

$baseTableAlias = $this->getSQLTableAlias($this->class->name);
$joinSql = $this->getJoinSql($baseTableAlias);

Expand Down Expand Up @@ -420,19 +422,18 @@ protected function getLockTablesSql($lockMode)
protected function getSelectColumnsSQL()
{
// Create the column list fragment only once
if ($this->selectColumnListSql !== null) {
return $this->selectColumnListSql;
if ($this->currentPersisterContext->selectColumnListSql !== null) {
return $this->currentPersisterContext->selectColumnListSql;
}

$columnList = array();
$this->rsm = new ResultSetMapping();
$discrColumn = $this->class->discriminatorColumn['name'];
$baseTableAlias = $this->getSQLTableAlias($this->class->name);
$resultColumnName = $this->platform->getSQLResultCasing($discrColumn);

$this->rsm->addEntityResult($this->class->name, 'r');
$this->rsm->setDiscriminatorColumn('r', $resultColumnName);
$this->rsm->addMetaResult('r', $resultColumnName, $discrColumn);
$this->currentPersisterContext->rsm->addEntityResult($this->class->name, 'r');
$this->currentPersisterContext->rsm->setDiscriminatorColumn('r', $resultColumnName);
$this->currentPersisterContext->rsm->addMetaResult('r', $resultColumnName, $discrColumn);

// Add regular columns
foreach ($this->class->fieldMappings as $fieldName => $mapping) {
Expand Down Expand Up @@ -523,9 +524,9 @@ protected function getSelectColumnsSQL()
}
}

$this->selectColumnListSql = implode(', ', $columnList);
$this->currentPersisterContext->selectColumnListSql = implode(', ', $columnList);

return $this->selectColumnListSql;
return $this->currentPersisterContext->selectColumnListSql;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions lib/Doctrine/ORM/Persisters/Entity/SingleTablePersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ protected function getDiscriminatorColumnTableName()
*/
protected function getSelectColumnsSQL()
{
if ($this->selectColumnListSql !== null) {
return $this->selectColumnListSql;
if ($this->currentPersisterContext->selectColumnListSql !== null) {
return $this->currentPersisterContext->selectColumnListSql;
}

$columnList[] = parent::getSelectColumnsSQL();
Expand All @@ -63,8 +63,8 @@ protected function getSelectColumnsSQL()

$resultColumnName = $this->platform->getSQLResultCasing($discrColumn);

$this->rsm->setDiscriminatorColumn('r', $resultColumnName);
$this->rsm->addMetaResult('r', $resultColumnName, $discrColumn);
$this->currentPersisterContext->rsm->setDiscriminatorColumn('r', $resultColumnName);
$this->currentPersisterContext->rsm->addMetaResult('r', $resultColumnName, $discrColumn);

// Append subclass columns
foreach ($this->class->subClasses as $subClassName) {
Expand Down Expand Up @@ -106,9 +106,9 @@ protected function getSelectColumnsSQL()
}
}

$this->selectColumnListSql = implode(', ', $columnList);
$this->currentPersisterContext->selectColumnListSql = implode(', ', $columnList);

return $this->selectColumnListSql;
return $this->currentPersisterContext->selectColumnListSql;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/Doctrine/Tests/Models/DDC3346/DDC3346Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Doctrine\Tests\Models\DDC3346;

/**
* @Entity
* @Table(name="ddc3346_articles")
*/
class DDC3346Article
{
const CLASSNAME = __CLASS__;

/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;

/**
* @var DDC3346Author
*
* @ManyToOne(targetEntity="DDC3346Author", inversedBy="articles")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
public $user;
}
28 changes: 28 additions & 0 deletions tests/Doctrine/Tests/Models/DDC3346/DDC3346Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Doctrine\Tests\Models\DDC3346;

/**
* @Entity
* @Table(name="ddc3346_users")
*/
class DDC3346Author
{
const CLASSNAME = __CLASS__;

/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
public $id;

/**
* @Column(type="string", length=255, unique=true)
*/
public $username;

/**
* @OneToMany(targetEntity="DDC3346Article", mappedBy="user", fetch="EAGER", cascade={"detach"})
*/
public $articles = array();
}
77 changes: 77 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3346Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\Models\DDC3346\DDC3346Article;
use Doctrine\Tests\Models\DDC3346\DDC3346Author;

/**
* @group DDC-3346
*/
class DDC3346Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function setUp()
{
$this->useModelSet('ddc3346');

parent::setUp();

$this->loadAuthorFixture();
}

public function testFindOneWithEagerFetchWillNotHydrateLimitedCollection()
{
/* @var DDC3346Author $author */
$author = $this->_em->getRepository(DDC3346Author::CLASSNAME)->findOneBy(
array('username' => 'bwoogy')
);

$this->assertCount(2, $author->articles);
}

public function testFindLimitedWithEagerFetchWillNotHydrateLimitedCollection()
{
/* @var DDC3346Author[] $authors */
$authors = $this->_em->getRepository(DDC3346Author::CLASSNAME)->findBy(
array('username' => 'bwoogy'),
null,
1
);

$this->assertCount(1, $authors);
$this->assertCount(2, $authors[0]->articles);
}

public function testFindWithEagerFetchAndOffsetWillNotHydrateLimitedCollection()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will fail due to a DBAL bug

{
/* @var DDC3346Author[] $authors */
$authors = $this->_em->getRepository(DDC3346Author::CLASSNAME)->findBy(
array('username' => 'bwoogy'),
null,
null,
0 // using an explicitly defined offset
);

$this->assertCount(1, $authors);
$this->assertCount(2, $authors[0]->articles);
}

private function loadAuthorFixture()
{
$user = new DDC3346Author();
$article1 = new DDC3346Article();
$article2 = new DDC3346Article();

$user->username = 'bwoogy';
$article1->user = $user;
$article2->user = $user;
$user->articles[] = $article1;
$user->articles[] = $article2;

$this->_em->persist($user);
$this->_em->persist($article1);
$this->_em->persist($article2);
$this->_em->flush();
$this->_em->clear();
}
}
9 changes: 9 additions & 0 deletions tests/Doctrine/Tests/OrmFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
'Doctrine\Tests\Models\DDC2504\DDC2504ChildClass',
'Doctrine\Tests\Models\DDC2504\DDC2504OtherClass',
),
'ddc3346' => array(
'Doctrine\Tests\Models\DDC3346\DDC3346Author',
'Doctrine\Tests\Models\DDC3346\DDC3346Article',
),
'quote' => array(
'Doctrine\Tests\Models\Quote\Address',
'Doctrine\Tests\Models\Quote\Group',
Expand Down Expand Up @@ -399,6 +403,11 @@ protected function tearDown()
$conn->executeUpdate('DELETE FROM cache_country');
}

if (isset($this->_usedModelSets['ddc3346'])) {
$conn->executeUpdate('DELETE FROM ddc3346_articles');
$conn->executeUpdate('DELETE FROM ddc3346_users');
}

if (isset($this->_usedModelSets['quote'])) {
$conn->executeUpdate('DELETE FROM ' . $platform->quoteIdentifier("quote-address"));
$conn->executeUpdate('DELETE FROM ' . $platform->quoteIdentifier("quote-group"));
Expand Down