Ask your Symfony questions! Pay money and get answers fast! (more info)

A statement pager for the old versions of Creole Symfony

  • SOLVED

Hey, everyone, I am overwhelmed with work this weekend so I am going to outsource 2 small projects to the excellent programmers of Symfony Experts.

The first project is real simple. I need [[LINK href="http://arpeggios.wordpress.com/2009/02/02/enabling-pagination-with-raw-sql-queries/"]]the great statement pager that Nicolas Martin did[[/LINK]], redone to work with the old syntax of pre-PDO versions of Creole. In other words, this:

$this->statement->execute();

needs to become

$this->statement->executeQuery();

and

$this->statement->rowCount()

needs to become whatever the earlier version of that method used to be.

I do not have time to look up the old API, so I offer it here for someone else to do.

Change all the methods to their older versions.

Answers (1)

2010-05-16

Arturo Linares answers:


class statementPager extends sfPager
{
protected $resultsetArray = array();
protected $query = '';

public function __construct($class = null, $maxPerPage = 10)
{
parent::__construct($class, $maxPerPage);
}

public function setStatement($statement, $query)
{
$this->statement = $statement;
$this->query = $query;
}

public function init()
{
$rs = $this->statement->executeQuery($this->query);

$this->setNbResults($rs->getRecordCount());

if (($this->getPage() == 0 || $this->getMaxPerPage() == 0))
{
$this->setLastPage(0);
}
else
{
$this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
}
}

public function getResults()
{

if (($this->getPage() == 0 || $this->getMaxPerPage() == 0))
{
$this->setLastPage(0);
}
else
{
$this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
}

$row_num = 1;
while ($resultset = $this->statement->executeQuery())
{
if ($row_num > $this->getMaxPerPage()*($this->getPage()-1)
and $row_num <= ($this->getPage()*$this->getMaxPerPage() ))
{
$this->resultsetArray[] = $resultset;
}
$row_num++;
}

return $this->resultsetArray;
}

public function retrieveObject($offset)
{
return $this->resultsetArray[$offset];

}
}

// you can call it like this:

$connection = Propel::getConnection();
$query = 'SELECT * from account';

$statement = $connection->createStatement();

$pager = new statementPager(null, 10);
$pager->setStatement($statement, $query);
$pager->setPage(1);
$pager->init();


Lawrence Krubner comments:

Looks good, but I won't be able to test it till very late tonight. Thanks!


Lawrence Krubner comments:

The while loop needs to be

while ($resultset->next())

but otherwise this was perfect. thanks.