Doctrine2 Paginator getting total results - php

Right away I would say, that I read this question Doctrine2 Paginator, but it doesn't give me enough information regarding the title of my question.
When I used Doctrine1 I was getting the result with such code for example:
$list = $this->query
->addSelect( 'SQL_CALC_FOUND_ROWS *' )
->offset( ( $this->currentPage - 1 ) * $this->perPage )
->limit( $this->perPage )
->execute( array(), \Doctrine_Core::HYDRATE_ARRAY_SHALLOW );
$totalRecords = SqlCalcFoundRowsEventListener::getFoundRowsCount();
$this->totalPages = ceil( $totalRecords / $this->perPage );
and it was great.
Now, when using Doctrine2 I'm confused on how should I get total amount of records with same query as limited result for current page.
Any help/advice would be greatly appreciated.

The paginator usage is as following:
$paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
$totalItems = count($paginator);
$pagesCount = ceil($totalItems / $pageSize);
// now get one page's items:
$paginator
->getQuery()
->setFirstResult($pageSize * ($currentPage-1)) // set the offset
->setMaxResults($pageSize); // set the limit
foreach ($paginator as $pageItem) {
echo "<li>" . $pageItem->getName() . "</li>";
}

Or some other loop:
for ($i=0; $i < $pagesCount; $i++) {
if ($currentPage == ($i+1)) {
$pagination1 .= '<li class="active">'.($i+1).'</li>';
}else{
$pagination1 .= '<li>'.($i+1).'</li>';
}
}

Related

how to pagify genre search result on the movie database

I have a website that I'm building on my localhost using the MVC design pattern. I have button on click that will display all the genres in TheMovieDatabase which I got using the TheMovieDatabaseAPI. On click of a genre I get data for Page 1 with 20 results. But, I couldn't figure out how to request the results for page 2. These requests are made using PHP.
I am assuming you are taking about pagination using, I am also assuming you are using mySQL as database, I am not sure which framework you are using,
Here is a function i use, you can build around this
function get_movie($page = 1){
global $database_connection;
$start = 0; //Don't modify this
$limit = 10; //set this to 20 to display 20 movies per page
$start = ($page - 1)* $limit;
//get the movie from the table
$sql = "SELECT genre FROM movie_table LIMIT $start, $limit";
$result = mysqli_query($database_connection, $sql);
while ($row = mysqli_fetch_array($result)){
echo "<div>$row['genre']</div>";
}
$movie_rows = mysqli_num_rows (mysqli_query($database_connection ,"SELECT * FROM movie_table"));
$total = ceil($testimony_rows / $limit);
if (isset($page)){
echo "<div>";
echo "<ul class='pager'>";
if($page > 1) {
echo "<a href='?page=".($page - 1)."' style='float: left;' class=' w3-sand w3-btn w3-margin w3-round'>Previous</a>";
}
for($i = 1; $i <= $total; $i++) {
if($i == $page) { echo "<li class='active current'>".$i."</li>"; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>"; }
}
if($page != $total) {
echo "<a href='?page=".($page + 1)."' class='w3-btn w3-margin w3-sand w3-round'>Next</a>";
}
echo "</ul></div>";
}
}
you can use limit and offset to get the result for page 2.
Example :- if you are using query builder to get the results
$limit = 20;
$offset = 21;
$queryBuilder->select('d')
->from(<table name>, 'd')
->setMaxResults($limit)
->setFirstResult($offset)
->setParameter('limit', $limit)
->setParameter('offset', $offset);
If you are using raw query :
SELECT * FROM table LIMIT 20 OFFSET 21
Else you can use paginator too
$paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
$totalItems = count($paginator);
$pagesCount = ceil($totalItems / $pageSize);
// now get one page's items:
$paginator
->getQuery()
->setFirstResult($pageSize * ($currentPage-1)) // set the offset
->setMaxResults($pageSize); // set the limit
foreach ($paginator as $pageItem) {
echo "<li>" . $pageItem->getName() . "</li>";
}

Doctrine query builder offset changeable

I'm a Symfony beginner & I'm trying to load my contact from the database using the Doctrine Query Builder as follows:
ContactRepository.php:
public function getcontactByLimit($offset, $limit)
{
$queryBuilder = $this->_em->createQueryBuilder()
->select('contact')
->from($this->_entityName, 'contact')
->setFirstResult( $offset )
->setMaxResults( $limit )
$query = $queryBuilder->getQuery();
$results = $query->getResult();
return $results;
}
DefaultController.php:
$contacts = $repository->getContactByLimit(0, 3);
Now I want to get all contacts I have in the database but just 3 by 3, what means I'm supposed to change the offset value at every loop (to 3 then 6 then 9...)
Have you any idea how to do it ?
It seems for me, that you want to load objects in parts to save memory.
For that, you can use an Iterator:
$query = $queryBuilder->getQuery();
$iterator = $query->iterate();
return $iterator;
And iterate in your Controller
foreach ($iterator as $row) {
}
What will happen, is that doctrine uses PDO and select your result but not read it out directly. There is a cursor on the ResultSet of the Database, which is moving on each iteration. So your PHP-Application will get each Row from Database one by one.
To get 3 by 3 packages, you could use the internal position:
$currentPackage = array();
foreach ($iterator as $position => $row) {
if($position % 3){
// do here something with your 3er package before unset
$currentPackage = array();
$entityManager->clear(); // clear doctrine, could free memory
}
$currentPackage[] = $row;
}
That what I did, & it works fine !
DefaultController.php:
$offset = 0;
$limit = 2;
$sizeData /= $limit;
for( $i = 0; $i < $sizeData; $i++)
{
$contacts = $repository->getListByLimit($offset, $limit);
$sender->setContacts($contacts);
$offset += $limit;
}
ContactRepository.php:
public function getListByLimit($offset, $limit)
{
$queryBuilder = $this->_em->createQueryBuilder()
->select('contact')
->from($this->_entityName, 'contact')
->setFirstResult( $offset )
->setMaxResults( $limit );
$query = $queryBuilder->getQuery();
$results = $query->getResult();
return $results;

Pagination for foreach loop

I currently have a method inside my "car class" that display the car:
static function getCars(){
$autos = DB::query("SELECT * FROM automoviles");
$retorno = array();
foreach($autos as $a){
$automovil = automovil::fromDB($a->marca, $a->modelo, $a->version, $a->year, $a->usuario_id, $a->kilometraje, $a->info,
$a->hits, $a->cilindrada, $a->estado, $a->color, $a->categoria, $a->precio, $a->idAutomovil);
array_push($retorno, $automovil);
}
return $retorno;
}
In my index.php I call that function
foreach(car::getCars() as $a){
That allows me to display the info this way ( of course inside the foreach I have a huge code with the details I'll display.
Is there a way to implement a pagination to that thing so I can handle 8 cars per page, instead of showing all of them at the same page?
You can add a $limit and $page parameter on your function so that it will only return a maximum of $limit number of items starting from $limit * $page(or will call it the $offset). You also need to add a function to get the total number of rows you have for automoviles table.
static function getCars($page = 0, $limit = 8){
$offset = $limit * max(0, $page - 1);
//replace this with prepared statement
$autos = DB::query("SELECT * FROM automoviles LIMIT $offset, $limit");
$retorno = array();
foreach($autos as $a){
$automovil = automovil::fromDB($a->marca, $a->modelo, $a->version, $a->year, $a->usuario_id, $a->kilometraje, $a->info,
$a->hits, $a->cilindrada, $a->estado, $a->color, $a->categoria, $a->precio, $a->idAutomovil);
array_push($retorno, $automovil);
}
return $retorno;
}
static function getTotal()
{
//query to get total number of rows in automoviles table
}
In your index.php do this:
foreach(car::getCars((isset($_GET['page']) ? $_GET['page'] : 1)) as $a){
...
}
and add the pagination links.
$total = car::getTotal();
if($total > 8) {
for($i = 1; $i <= intval(ceil(1.0 * $total / $limit)); $i++) {
echo '' . $i . ';
}
}

Pagination in MongoDB database for PHP language

I have the following code for the pagination(PHP) in MongoDB database.
<?php
$mongodb = new Mongo("vvv");
$database = fff
$collection = gggg
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$limit = 12;
$skip = ($page - 1) * $limit;
$next = ($page + 1);
$prev = ($page - 1);
$sort = array('createdAt' => -1);
$cursor = $collection->find()->skip($skip)->limit($limit)->sort($sort);
foreach ($cursor as $r) {
--------
}
$total= $cursor->count();
if($page > 1){
echo 'Previous';
if($page * $limit < $total) {
echo ' Next';
}
} else {
if($page * $limit < $total) {
echo ' Next';
}
}
$mongodb->close();
?>
BUT my database size is 30GB+, each search provides the results of 20,000 which takes HUGE TIME to count() //$total= $cursor->count();
Can any one provide any PHP pagination code for MongoDB which does not count the total number of results but do the pagination?
cursor.skip() requires the server to walk from the beginning of the collection or index to get the offset or skip position before beginning to return results. It is not recommended to use it in pagination against 30GB+ data in your case.
If you cannot narrow a little bit your condition in find(), you can consider to add a sequence number in your docs for pagination purpose, assuming that you will rarely remove/update those documents:
{
createdAt: "2015-01-01",
...
seq_no: 1
},
{
createdAt: "2015-01-02",
...
seq_no: 2
}
Then you can implement the pagination query like this (remember to create the index on seq_no, of course):
$cursor = $collection->find({"seq_no": {"$gt":$skip, "$lte":($skip+$limit)}})

Upgrading a PHP pagination function to run faster

I created a pagination for an website file last year, and I've never really had to use it again.
But now I'll need to use it in a bigger application, and I'm afraid it won't behave really good with a huge amount of mysql rows, I don't know.
I'll post an example bellow and I'll be greatful if someone could tell me if I really need to upgrade the code.
/* Connection file */
include( 'config.php' );
/* Get the amount of all rows */
$mqc = "SELECT COUNT(*) AS total FROM table";
$mqe = mysql_query( $mqc );
$mqn = mysql_fetch_array( $mqe );
$total = $mqn['total'];
/* Rows per page */
$perPage = 10;
/* Range for the pagination */
$range = 5;
/* Actual page */
$page = isset( $_GET['page'] ) &&
is_numeric( $_GET['page'] ) &&
!empty( $_GET['page'] ) ?
( (int)$_GET['page'] * $range ) : 0;
/* Query */
$mqc = "SELECT * FROM table
LIMIT " . $page . ", " . $perPage . " ";
$mqe = mysql_query( $mqc );
/* Show results */
while( $l = mysql_fetch_object( $mqe ) )
{
echo $l->id . ' - ' . $l->title . '<br />';
}
/* Function for pagination */
function pages( $total = false, $perPage = false, $page = false, $range = false )
{
/* Number of pages */
$totalPages = ( ceil( $total / $perPage ) ) - 1;
/* The range * 2 */
$range = $range;
/* The real param $_GET['page'] */
$realGetPage = !empty( $page ) ? ( $page / $range ) : 0;
/* Show the first page */
if($realGetPage >= 7)
{
echo ' 1 ... ';
}
/* Loop to create the range */
for($i = ( $realGetPage - $range ); $i < ( $range + $realGetPage ); $i++)
{
/* Check out if the value isn't outta range */
if( $i > 0 && $i < $totalPages )
{
/* Highlight the actual page */
if( ( $i - 1 ) == $realGetPage )
{
echo ' [ ' . $i . ' ] ';
}
else
{
echo ' ' . $i . ' ';
}
}
/* Store the number of the last page */
$lastPage = ( $i + 1 );
}
/* Hack to show 5678910 */
if( $lastPage < ( $range * 2 ) + 1 )
{
for( $j = $lastPage; $j <= ( $range * 2 ); $j++ )
{
if( ($j - 1) == $realGetPage )
{
echo ' [ ' . $j . ' ] ';
}
else
{
echo ' ' . $j . ' ';
}
}
}
/* Last page */
echo ' ... ' . $totalPages . ' ';
}
/* Show the pagination */
pages( $total, $perPage, $page, $range );
What do you think?
From the information you provided in your comment, the performance issues you might have will not come from your PHP script. If you do multiple INNER JOINS on huge tables, this is exactly what will affect your performance. Some important things to look for:
Tables indexes (if you use filters, for your joins if you don't join on primary keys)
Optimizing your table / queries to decrease the number of JOINs
If you expect a lot of traffic, consider using caching for your queries (memcached for instance)
As Phil mentionned, the ORDER BY must be used to render a predictable result and could slow down your queries if the tables are used. You could pre-render ordered table using a cron job
Consider using denormalization to reduce the number of JOINS
The main thing I see missing is an ORDER BY clause. Without this, your paged results are (potentially) unpredictable.
Add an ORDER BY clause and make sure you have appropriate database indexes in place on the sortable column(s).
I'd also recommend switching to a more up-to-date database connection library with prepared statements and bound parameters. Please see PDO.

Categories