2009-04-12 23:02:12 +04:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
use Doctrine\ORM\Query\ResultSetMapping ;
use Doctrine\Tests\Models\CMS\CmsUser ;
2009-08-15 22:11:51 +04:00
use Doctrine\Tests\Models\CMS\CmsPhonenumber ;
2009-10-22 13:10:59 +04:00
use Doctrine\Tests\Models\CMS\CmsAddress ;
2009-04-12 23:02:12 +04:00
require_once __DIR__ . '/../../TestInit.php' ;
/**
* NativeQueryTest
*
* @ author robo
*/
class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
2010-04-18 21:12:38 +04:00
private $platform = null ;
2009-04-12 23:02:12 +04:00
protected function setUp () {
$this -> useModelSet ( 'cms' );
parent :: setUp ();
2010-04-18 21:12:38 +04:00
$this -> platform = $this -> _em -> getConnection () -> getDatabasePlatform ();
2009-04-12 23:02:12 +04:00
}
public function testBasicNativeQuery ()
{
$user = new CmsUser ;
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'dev' ;
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $user );
2009-04-12 23:02:12 +04:00
$this -> _em -> flush ();
2009-08-15 22:11:51 +04:00
$this -> _em -> clear ();
2009-04-12 23:02:12 +04:00
$rsm = new ResultSetMapping ;
2009-05-21 23:18:14 +04:00
$rsm -> addEntityResult ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
2010-04-18 21:12:38 +04:00
$rsm -> addFieldResult ( 'u' , $this -> platform -> getSQLResultCasing ( 'id' ), 'id' );
$rsm -> addFieldResult ( 'u' , $this -> platform -> getSQLResultCasing ( 'name' ), 'name' );
2009-04-12 23:02:12 +04:00
$query = $this -> _em -> createNativeQuery ( 'SELECT id, name FROM cms_users WHERE username = ?' , $rsm );
$query -> setParameter ( 1 , 'romanb' );
2009-08-03 21:18:37 +04:00
$users = $query -> getResult ();
2009-04-12 23:02:12 +04:00
$this -> assertEquals ( 1 , count ( $users ));
2009-05-26 15:30:07 +04:00
$this -> assertTrue ( $users [ 0 ] instanceof CmsUser );
2009-04-12 23:02:12 +04:00
$this -> assertEquals ( 'Roman' , $users [ 0 ] -> name );
}
2009-08-15 22:11:51 +04:00
2009-10-22 13:10:59 +04:00
public function testJoinedOneToManyNativeQuery ()
2009-08-15 22:11:51 +04:00
{
$user = new CmsUser ;
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'dev' ;
$phone = new CmsPhonenumber ;
$phone -> phonenumber = 424242 ;
$user -> addPhonenumber ( $phone );
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> _em -> clear ();
$rsm = new ResultSetMapping ;
$rsm -> addEntityResult ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
2010-04-18 21:12:38 +04:00
$rsm -> addFieldResult ( 'u' , $this -> platform -> getSQLResultCasing ( 'id' ), 'id' );
$rsm -> addFieldResult ( 'u' , $this -> platform -> getSQLResultCasing ( 'name' ), 'name' );
$rsm -> addFieldResult ( 'u' , $this -> platform -> getSQLResultCasing ( 'status' ), 'status' );
2009-08-15 22:11:51 +04:00
$rsm -> addJoinedEntityResult ( 'Doctrine\Tests\Models\CMS\CmsPhonenumber' , 'p' , 'u' , 'phonenumbers' );
2010-04-18 21:12:38 +04:00
$rsm -> addFieldResult ( 'p' , $this -> platform -> getSQLResultCasing ( 'phonenumber' ), 'phonenumber' );
2009-08-15 22:11:51 +04:00
$query = $this -> _em -> createNativeQuery ( 'SELECT id, name, status, phonenumber FROM cms_users INNER JOIN cms_phonenumbers ON id = user_id WHERE username = ?' , $rsm );
$query -> setParameter ( 1 , 'romanb' );
$users = $query -> getResult ();
$this -> assertEquals ( 1 , count ( $users ));
$this -> assertTrue ( $users [ 0 ] instanceof CmsUser );
$this -> assertEquals ( 'Roman' , $users [ 0 ] -> name );
$this -> assertTrue ( $users [ 0 ] -> getPhonenumbers () instanceof \Doctrine\ORM\PersistentCollection );
2009-10-22 13:10:59 +04:00
$this -> assertTrue ( $users [ 0 ] -> getPhonenumbers () -> isInitialized ());
2009-08-15 22:11:51 +04:00
$this -> assertEquals ( 1 , count ( $users [ 0 ] -> getPhonenumbers ()));
$phones = $users [ 0 ] -> getPhonenumbers ();
$this -> assertEquals ( 424242 , $phones [ 0 ] -> phonenumber );
$this -> assertTrue ( $phones [ 0 ] -> getUser () === $users [ 0 ]);
}
2009-10-22 13:10:59 +04:00
public function testJoinedOneToOneNativeQuery ()
{
$user = new CmsUser ;
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'dev' ;
$addr = new CmsAddress ;
$addr -> country = 'germany' ;
$addr -> zip = 10827 ;
$addr -> city = 'Berlin' ;
$user -> setAddress ( $addr );
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> _em -> clear ();
$rsm = new ResultSetMapping ;
$rsm -> addEntityResult ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' );
2010-04-18 21:12:38 +04:00
$rsm -> addFieldResult ( 'u' , $this -> platform -> getSQLResultCasing ( 'id' ), 'id' );
$rsm -> addFieldResult ( 'u' , $this -> platform -> getSQLResultCasing ( 'name' ), 'name' );
$rsm -> addFieldResult ( 'u' , $this -> platform -> getSQLResultCasing ( 'status' ), 'status' );
2009-10-22 13:10:59 +04:00
$rsm -> addJoinedEntityResult ( 'Doctrine\Tests\Models\CMS\CmsAddress' , 'a' , 'u' , 'address' );
2010-04-18 21:12:38 +04:00
$rsm -> addFieldResult ( 'a' , $this -> platform -> getSQLResultCasing ( 'a_id' ), 'id' );
$rsm -> addFieldResult ( 'a' , $this -> platform -> getSQLResultCasing ( 'country' ), 'country' );
$rsm -> addFieldResult ( 'a' , $this -> platform -> getSQLResultCasing ( 'zip' ), 'zip' );
$rsm -> addFieldResult ( 'a' , $this -> platform -> getSQLResultCasing ( 'city' ), 'city' );
2009-10-22 13:10:59 +04:00
$query = $this -> _em -> createNativeQuery ( '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 = ?' , $rsm );
$query -> setParameter ( 1 , 'romanb' );
$users = $query -> getResult ();
$this -> assertEquals ( 1 , count ( $users ));
$this -> assertTrue ( $users [ 0 ] instanceof CmsUser );
$this -> assertEquals ( 'Roman' , $users [ 0 ] -> name );
$this -> assertTrue ( $users [ 0 ] -> getPhonenumbers () instanceof \Doctrine\ORM\PersistentCollection );
$this -> assertFalse ( $users [ 0 ] -> getPhonenumbers () -> isInitialized ());
$this -> assertTrue ( $users [ 0 ] -> getAddress () instanceof CmsAddress );
$this -> assertTrue ( $users [ 0 ] -> getAddress () -> getUser () == $users [ 0 ]);
$this -> assertEquals ( 'germany' , $users [ 0 ] -> getAddress () -> getCountry ());
$this -> assertEquals ( 10827 , $users [ 0 ] -> getAddress () -> getZipCode ());
$this -> assertEquals ( 'Berlin' , $users [ 0 ] -> getAddress () -> getCity ());
}
2009-11-21 16:13:19 +03:00
public function testFluentInterface ()
{
$rsm = new ResultSetMapping ;
$q = $this -> _em -> createNativeQuery ( 'SELECT id, name, status, phonenumber FROM cms_users INNER JOIN cms_phonenumbers ON id = user_id WHERE username = ?' , $rsm );
$q2 = $q -> setSql ( 'foo' , $rsm )
-> setResultSetMapping ( $rsm )
-> expireResultCache ( true )
-> setHint ( 'foo' , 'bar' )
-> setParameter ( 1 , 'foo' )
-> setParameters ( array ( 2 => 'bar' ))
-> setResultCacheDriver ( null )
-> setResultCacheLifetime ( 3500 );
$this -> assertSame ( $q , $q2 );
}
2009-04-12 23:02:12 +04:00
}