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

test scalar result

This commit is contained in:
Fabio B. Silva 2012-02-28 22:18:25 -03:00
parent f813223036
commit 0cc2583a02
3 changed files with 87 additions and 11 deletions

View File

@ -170,6 +170,9 @@ class ResultSetMappingBuilder extends ResultSetMapping
$this->addFieldResult($alias, $field['column'], $fieldName, $classMetadata->name);
}
} else {
if(!isset($classMetadata->fieldMappings[$fieldName])) {
throw new \InvalidArgumentException("Entity '".$classMetadata->name."' has no field '".$fieldName."'. ");
}
$this->addFieldResult($alias, $field['column'], $fieldName, $classMetadata->name);
}
}

View File

@ -18,20 +18,25 @@ use Doctrine\Common\Collections\ArrayCollection;
* query = "SELECT id, username FROM cms_users WHERE username = ?"
* ),
* @NamedNativeQuery(
* name = "fetchAllColumnsWithResultClass",
* name = "fetchAllColumns",
* resultClass = "CmsUser",
* query = "SELECT * FROM cms_users WHERE username = ?"
* ),
* @NamedNativeQuery(
* name = "fetchJoinedAddressWithResultSetMapping",
* name = "fetchJoinedAddress",
* resultSetMapping= "mappingJoinedAddress",
* query = "SELECT u.id, u.name, u.status, a.id AS a_id, a.country, a.zip, a.city FROM cms_users u INNER JOIN cms_addresses a ON u.id = a.user_id WHERE u.username = ?"
* ),
* @NamedNativeQuery(
* name = "fetchJoinedPhonenumberWithResultSetMapping",
* name = "fetchJoinedPhonenumber",
* resultSetMapping= "mappingJoinedPhonenumber",
* query = "SELECT id, name, status, phonenumber AS number FROM cms_users INNER JOIN cms_phonenumbers ON id = user_id WHERE username = ?"
* ),
* @NamedNativeQuery(
* name = "fetchUserPhonenumberCount",
* resultSetMapping= "mappingUserPhonenumberCount",
* query = "SELECT id, name, status, COUNT(phonenumber) AS numPhones FROM cms_users INNER JOIN cms_phonenumbers ON id = user_id WHERE username IN (?) GROUP BY status, user_id ORDER BY username"
* ),
* })
*
* @SqlResultSetMappings({
@ -58,12 +63,28 @@ use Doctrine\Common\Collections\ArrayCollection;
* @EntityResult(
* entityClass = "CmsUser",
* fields = {
* @FieldResult("id"),
* @FieldResult("name"),
* @FieldResult("status"),
* @FieldResult("phonenumbers.phonenumber" , column = "number"),
* }
* )
* }
* ),
* @SqlResultSetMapping(
* name = "mappingUserPhonenumberCount",
* entities= {
* @EntityResult(
* entityClass = "CmsUser",
* fields = {
* @FieldResult(name = "id"),
* @FieldResult(name = "name"),
* @FieldResult(name = "status"),
* @FieldResult(name = "phonenumbers.phonenumber" , column = "number"),
* }
* )
* },
* columns = {
* @ColumnResult("numPhones")
* }
* )
* })

View File

@ -372,8 +372,6 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
*/
public function testBasicNativeNamedQueryWithResultClass()
{
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
$user = new CmsUser;
$user->name = 'Fabio B. Silva';
$user->username = 'FabioBatSilva';
@ -406,7 +404,7 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_em->clear();
$result = $repository->createNativeNamedQuery('fetchAllColumnsWithResultClass')
$result = $repository->createNativeNamedQuery('fetchAllColumns')
->setParameter(1, 'FabioBatSilva')->getResult();
$this->assertEquals(1, count($result));
@ -419,6 +417,9 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
}
/**
* @group DDC-1663
*/
public function testJoinedOneToOneNativeNamedQueryWithResultSetMapping()
{
$user = new CmsUser;
@ -442,7 +443,7 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
$result = $repository->createNativeNamedQuery('fetchJoinedAddressWithResultSetMapping')
$result = $repository->createNativeNamedQuery('fetchJoinedAddress')
->setParameter(1, 'FabioBatSilva')->getResult();
$this->assertEquals(1, count($result));
@ -457,7 +458,9 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertEquals('São Paulo', $result[0]->getAddress()->getCity());
}
/**
* @group DDC-1663
*/
public function testJoinedOneToManyNativeNamedQueryWithResultSetMapping()
{
$user = new CmsUser;
@ -477,7 +480,7 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
$result = $repository->createNativeNamedQuery('fetchJoinedPhonenumberWithResultSetMapping')
$result = $repository->createNativeNamedQuery('fetchJoinedPhonenumber')
->setParameter(1, 'FabioBatSilva')->getResult();
$this->assertEquals(1, count($result));
@ -491,7 +494,56 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertTrue($phones[0]->getUser() === $result[0]);
}
/**
* @group DDC-1663
*/
public function testMixedNativeNamedQueryNormalJoin()
{
$user1 = new CmsUser;
$user1->name = 'Fabio B. Silva';
$user1->username = 'FabioBatSilva';
$user1->status = 'dev';
$user2 = new CmsUser;
$user2->name = 'test tester';
$user2->username = 'test';
$user2->status = 'tester';
$phone1 = new CmsPhonenumber;
$phone2 = new CmsPhonenumber;
$phone3 = new CmsPhonenumber;
$phone1->phonenumber = 11111111;
$phone2->phonenumber = 22222222;
$phone3->phonenumber = 33333333;
$user1->addPhonenumber($phone1);
$user1->addPhonenumber($phone2);
$user2->addPhonenumber($phone3);
$this->_em->persist($user1);
$this->_em->persist($user2);
$this->_em->flush();
$this->_em->clear();
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
$result = $repository->createNativeNamedQuery('fetchUserPhonenumberCount')
->setParameter(1, array('test','FabioBatSilva'))->getResult();
$this->assertEquals(2, count($result));
$this->assertTrue(is_array($result[0]));
$this->assertTrue(is_array($result[1]));
// first user => 2 phonenumbers
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[0][0]);
$this->assertEquals('Fabio B. Silva', $result[0][0]->name);
$this->assertEquals(2, $result[0]['numPhones']);
// second user => 1 phonenumbers
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[1][0]);
$this->assertEquals('test tester', $result[1][0]->name);
$this->assertEquals(1, $result[1]['numPhones']);
}
}