简体   繁体   中英

Symfony 1.4 sfDoctrinePager: How to set custom SQL?

There is an old project written in symfony 1.4

And there is SQL that it is not possible to create using Doctrine. For an example, consider this test SQL:

SELECT id,name FROM users
UNION ALL
SELECT id*10 as id,name FROM users

puvlic function getPager(){
$sql = "SELECT id,name FROM users
      UNION ALL
    SELECT id*10 as id,name FROM users";

$pager = new sfDoctrinePager('Users', 10);
    $pager->setQuery($sql);
    $pager->setPage(2);
    $pager->init();
    return $pager;
}

It gives the following error:

Fatal error: __clone method called on non-object in ...

Help please solve the problem.

With stock sfDoctrinePager class it's not possible to use a custom SQL string. It only accepts an instance of Doctrine_Query (see sfDoctrinePlugin sources ).

You should construct a Doctring_Query object that does exactly what you need (which is likely not possible because of UNION). It requires a query object because pager needs to modify query before executing it (add LIMIT and OFFSET) in a platform-independent way (for example, it's different on MySQL vs. MS-SQL).

If you really need to use custom SQL with UNION , the only way to achieve it is to implement your custom sfDoctrinePager implementation that accepts SQL and assumes your target DB platform, certain SQL query structure, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM