1
0
mirror of synced 2025-01-18 06:21:40 +03:00

test native query with result class

This commit is contained in:
Fabio B. Silva 2012-02-27 21:16:35 -03:00
parent 3b79951824
commit f018a56d6d
4 changed files with 79 additions and 1 deletions

View File

@ -129,6 +129,15 @@ class ResultSetMappingBuilder extends ResultSetMapping
$this->addFieldResult($alias, $columnName, $propertyName); $this->addFieldResult($alias, $columnName, $propertyName);
} }
foreach ($classMetadata->associationMappings as $associationMapping) {
if ($associationMapping['isOwningSide'] && $associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
foreach ($associationMapping['joinColumns'] as $joinColumn) {
$columnName = $joinColumn['name'];
$this->addMetaResult($alias, $columnName, $columnName, $classMetadata->isIdentifier($columnName));
}
}
}
return $this; return $this;
} }

View File

@ -10,6 +10,19 @@ use Doctrine\Common\Collections\ArrayCollection;
* @NamedQueries({ * @NamedQueries({
* @NamedQuery(name="all", query="SELECT u FROM __CLASS__ u") * @NamedQuery(name="all", query="SELECT u FROM __CLASS__ u")
* }) * })
*
* @NamedNativeQueries({
* @NamedNativeQuery(
* name = "fetchIdAndUsernameWithResultClass",
* resultClass = "CmsUser",
* query = "SELECT id, username FROM cms_users WHERE username = ?"
* ),
* @NamedNativeQuery(
* name = "fetchAllColumnsWithResultClass",
* resultClass = "CmsUser",
* query = "SELECT * FROM cms_users WHERE username = ?"
* ),
* })
*/ */
class CmsUser class CmsUser
{ {

View File

@ -7,6 +7,7 @@ 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;
use Doctrine\Tests\Models\CMS\CmsEmail;
use Doctrine\Tests\Models\Company\CompanyFixContract; use Doctrine\Tests\Models\Company\CompanyFixContract;
use Doctrine\Tests\Models\Company\CompanyEmployee; use Doctrine\Tests\Models\Company\CompanyEmployee;
@ -334,7 +335,7 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
/** /**
* @group DDC-1663 * @group DDC-1663
*/ */
public function testBasicNativeNamedQuery() public function testBasicNativeNamedQueryWithSqlResultSetMapping()
{ {
$user = new CmsUser; $user = new CmsUser;
$user->name = 'Fabio B. Silva'; $user->name = 'Fabio B. Silva';
@ -365,5 +366,59 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertEquals($addr->city, $result[0]->city); $this->assertEquals($addr->city, $result[0]->city);
$this->assertEquals($addr->country, $result[0]->country); $this->assertEquals($addr->country, $result[0]->country);
} }
/**
* @group DDC-1663
*/
public function testBasicNativeQueryWithResultClass()
{
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
$user = new CmsUser;
$user->name = 'Fabio B. Silva';
$user->username = 'FabioBatSilva';
$user->status = 'dev';
$email = new CmsEmail();
$email->email = 'fabio.bat.silva@gmail.com';
$user->setEmail($email);
$this->_em->clear();
$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
$result = $repository->createNativeNamedQuery('fetchIdAndUsernameWithResultClass')
->setParameter(1, 'FabioBatSilva')->getResult();
$this->assertEquals(1, count($result));
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[0]);
$this->assertNull($result[0]->name);
$this->assertNull($result[0]->email);
$this->assertEquals($user->id, $result[0]->id);
$this->assertEquals('FabioBatSilva', $result[0]->username);
$this->_em->clear();
$result = $repository->createNativeNamedQuery('fetchAllColumnsWithResultClass')
->setParameter(1, 'FabioBatSilva')->getResult();
$this->assertEquals(1, count($result));
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[0]);
$this->assertEquals($user->id, $result[0]->id);
$this->assertEquals('Fabio B. Silva', $result[0]->name);
$this->assertEquals('FabioBatSilva', $result[0]->username);
$this->assertEquals('dev', $result[0]->status);
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsEmail', $result[0]->email);
$this->_em->clear();
}
} }

View File

@ -147,6 +147,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
$conn->executeUpdate('DELETE FROM cms_comments'); $conn->executeUpdate('DELETE FROM cms_comments');
$conn->executeUpdate('DELETE FROM cms_articles'); $conn->executeUpdate('DELETE FROM cms_articles');
$conn->executeUpdate('DELETE FROM cms_users'); $conn->executeUpdate('DELETE FROM cms_users');
$conn->executeUpdate('DELETE FROM cms_emails');
} }
if (isset($this->_usedModelSets['ecommerce'])) { if (isset($this->_usedModelSets['ecommerce'])) {