1
0
mirror of synced 2025-01-30 20:11:49 +03:00

Moved new functions to ResultSetMappingBuilder class

This commit is contained in:
Michael Ridgway 2011-03-30 10:27:31 -04:00
parent 20dc72ef9a
commit c46d835146
3 changed files with 90 additions and 48 deletions

View File

@ -392,39 +392,4 @@ class ResultSetMapping
$this->metaMappings[$columnName] = $fieldName; $this->metaMappings[$columnName] = $fieldName;
$this->columnOwnerMap[$columnName] = $alias; $this->columnOwnerMap[$columnName] = $alias;
} }
}
/**
* Adds a root entity and all of its fields to the result set.
*
* @param ClassMetadata $classMetadata
* @param string $alias The unique alias to use for the root entity.
* @param string $prefix Prefix for columns
*/
public function addRootEntityFromClassMetadata(ClassMetadata $classMetadata, $alias, $prefix = '')
{
$this->addEntityResult($classMetadata->getReflectionClass()->getName(), $alias);
foreach ($classMetadata->getColumnNames() AS $columnName) {
// $columnName should use DBAL\Platforms\AbstractPlatform::getSQLResultCasing() but we don't have the EM
$this->addFieldResult($alias, $prefix . $columnName, $classMetadata->getFieldName($columnName));
}
}
/**
* Adds a joined entity and all of its fields to the result set.
*
* @param ClassMetadata $classMetadata
* @param string $alias The unique alias to use for the joined entity.
* @param string $parentAlias The alias of the entity result that is the parent of this joined result.
* @param object $relation The association field that connects the parent entity result with the joined entity result.
* @param string $prefix Prefix for columns
*/
public function addJoinedEntityFromClassMetadata(ClassMetadata $classMetadata, $alias, $parentAlias, $relation, $prefix = '')
{
$this->addJoinedEntityResult($classMetadata->getReflectionClass()->getName(), $alias, $parentAlias, $relation);
foreach ($classMetadata->getColumnNames() AS $columnName) {
// $columnName should use DBAL\Platforms\AbstractPlatform::getSQLResultCasing() but we don't have the EM
$this->addFieldResult($alias, $prefix . $columnName, $classMetadata->getFieldName($columnName));
}
}
}

View File

@ -0,0 +1,80 @@
<?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 LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Query;
use Doctrine\ORM\EntityManager;
/**
* A ResultSetMappingBuilder uses the EntityManager to automatically populate entity fields
*
* @author Michael Ridgway <mcridgway@gmail.com>
* @since 2.1
*/
class ResultSetMappingBuilder extends ResultSetMapping
{
/**
* @var EntityManager
*/
private $em;
/**
* @param EntityManager
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* Adds a root entity and all of its fields to the result set.
*
* @param string $class The class name of the root entity.
* @param string $alias The unique alias to use for the root entity.
* @param string $prefix Prefix for columns
*/
public function addRootEntityFromClassMetadata($class, $alias, $prefix = '')
{
$this->addEntityResult($class, $alias);
$classMetadata = $this->em->getClassMetadata($class);
$platform = $this->em->getConnection()->getDatabasePlatform();
foreach ($classMetadata->getColumnNames() AS $columnName) {
$this->addFieldResult($alias, $platform->getSQLResultCasing($prefix . $columnName), $classMetadata->getFieldName($columnName));
}
}
/**
* Adds a joined entity and all of its fields to the result set.
*
* @param string $class The class name of the joined entity.
* @param string $alias The unique alias to use for the joined entity.
* @param string $parentAlias The alias of the entity result that is the parent of this joined result.
* @param object $relation The association field that connects the parent entity result with the joined entity result.
* @param string $prefix Prefix for columns
*/
public function addJoinedEntityFromClassMetadata($class, $alias, $parentAlias, $relation, $prefix = '')
{
$this->addJoinedEntityResult($class, $alias, $parentAlias, $relation);
$classMetadata = $this->em->getClassMetadata($class);
$platform = $this->em->getConnection()->getDatabasePlatform();
foreach ($classMetadata->getColumnNames() AS $columnName) {
$this->addFieldResult($alias, $platform->getSQLResultCasing($prefix . $columnName), $classMetadata->getFieldName($columnName));
}
}
}

View File

@ -3,6 +3,7 @@
namespace Doctrine\Tests\ORM\Functional; namespace Doctrine\Tests\ORM\Functional;
use Doctrine\ORM\Query\ResultSetMapping; use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
use Doctrine\Tests\Models\CMS\CmsUser; use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\Models\CMS\CmsPhonenumber; use Doctrine\Tests\Models\CMS\CmsPhonenumber;
use Doctrine\Tests\Models\CMS\CmsAddress; use Doctrine\Tests\Models\CMS\CmsAddress;
@ -157,7 +158,7 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertSame($q, $q2); $this->assertSame($q, $q2);
} }
public function testJoinedOneToManyNativeQueryWithRSMHelper() public function testJoinedOneToManyNativeQueryWithRSMBuilder()
{ {
$user = new CmsUser; $user = new CmsUser;
$user->name = 'Roman'; $user->name = 'Roman';
@ -174,11 +175,9 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_em->clear(); $this->_em->clear();
$rsm = new ResultSetMapping; $rsm = new ResultSetMappingBuilder($this->_em);
$userMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); $rsm->addRootEntityFromClassMetadata('Doctrine\Tests\Models\CMS\CmsUser', 'u');
$phoneMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'); $rsm->addJoinedEntityFromClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber', 'p', 'u', 'phonenumbers', 'p_');
$rsm->addRootEntityFromClassMetadata($userMetadata, 'u');
$rsm->addJoinedEntityFromClassMetadata($phoneMetadata, 'p', 'u', 'phonenumbers', 'p_');
$query = $this->_em->createNativeQuery('SELECT u.*, p.phonenumber AS p_phonenumber FROM cms_users u INNER JOIN cms_phonenumbers p ON u.id = p.user_id WHERE username = ?', $rsm); $query = $this->_em->createNativeQuery('SELECT u.*, p.phonenumber AS p_phonenumber FROM cms_users u INNER JOIN cms_phonenumbers p ON u.id = p.user_id WHERE username = ?', $rsm);
$query->setParameter(1, 'romanb'); $query->setParameter(1, 'romanb');
@ -194,7 +193,7 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertTrue($phones[0]->getUser() === $users[0]); $this->assertTrue($phones[0]->getUser() === $users[0]);
} }
public function testJoinedOneToOneNativeQueryWithRSMHelper() public function testJoinedOneToOneNativeQueryWithRSMBuilder()
{ {
$user = new CmsUser; $user = new CmsUser;
$user->name = 'Roman'; $user->name = 'Roman';
@ -215,11 +214,9 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_em->clear(); $this->_em->clear();
$rsm = new ResultSetMapping; $rsm = new ResultSetMappingBuilder($this->_em);
$userMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); $rsm->addRootEntityFromClassMetadata('Doctrine\Tests\Models\CMS\CmsUser', 'u');
$addressMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); $rsm->addJoinedEntityFromClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress', 'a', 'u', 'address', 'a_');
$rsm->addRootEntityFromClassMetadata($userMetadata, 'u');
$rsm->addJoinedEntityFromClassMetadata($addressMetadata, 'a', 'u', 'address', 'a_');
$query = $this->_em->createNativeQuery('SELECT u.id, u.name, u.status, a.id AS a_id, a.country AS a_country, a.zip AS a_zip, a.city AS a_city FROM cms_users u INNER JOIN cms_addresses a ON u.id = a.user_id WHERE u.username = ?', $rsm); $query = $this->_em->createNativeQuery('SELECT u.id, u.name, u.status, a.id AS a_id, a.country AS a_country, a.zip AS a_zip, a.city AS a_city FROM cms_users u INNER JOIN cms_addresses a ON u.id = a.user_id WHERE u.username = ?', $rsm);
$query->setParameter(1, 'romanb'); $query->setParameter(1, 'romanb');