zend framework paginator does not work? - php

I use ZendFramework Paginator and I have some code like this:
$defaultCount=1000;
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select();
$select->from(array('u' => 'core_users'));
$select->join(array('ur' => 'core_users_roles'), 'u.uid = ur.uid');
$select->join(array('r' => 'core_roles'), 'r.rid = ur.rid');
$adapter=new Zend_Paginator_Adapter_DbSelect($select);
$adapter->setRowCount($db->select()->from('core_users',array(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN =>'uid')));
$paginator= new Zend_Paginator($adapter);
$paginator->setItemCountPerPage($defaultCount);
$paginator->setCurrentPageNumber($page);
but ,i can not get all of my data from DbSelect Adapter.when i remove $defaultCount , it always give me 20 total data (default ,I guess). should i use single table ?

$db->select()->from('core_users',array(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN =>'uid'))
give me wrong number. I use hard code ,works on me.

Related

Symfony mongo-odm-aggregation-bundle sums

For start, I have to say I am new to mongo (3.2). I am using mongo-odm-aggregation-bundle for php framework Symfony (2.8.4). I want to get sums of some fields restricted by dates.
So far, I managed to get sums for all records:
$expr = new \Solution\MongoAggregation\Pipeline\Operators\Expr;
$aq = $this->manager->getCollection('AppBundle:UserDaySums')->createAggregateQuery()->group([
'_id' => 'client.$id',
'total' => $expr->sum('$aSum'),
])
Now, I'd like to restrict this query by dateFrom,dateTo and userId. I am not sure, how to do it. I know, I should probably use match function, but I don't know how. Or is there some better solution?
Thanks for replies!
KP
Yes, you can use the match function. For example, the following assumes you have the date variables for use in the query:
$expr = new \Solution\MongoAggregation\Pipeline\Operators\Expr;
$aq = $this->manager->getCollection('AppBundle:UserDaySums')->createAggregateQuery();
$dateFrom = new MongoDate(strtotime('-2 days'));
$dateTo = new MongoDate();
$userId = 'Xsgy62js0lb';
$result = $aq->match(['dateFrom'=>$dateFrom, 'dateTo'=>$dateTo, 'userId'=>$userId ])
->group(['_id'=>'client.$id', 'total'=>$expr->sum('$aSum') ])
->getQuery()->aggregate();
In my case, match() was somewhat tricky on a MongoId object.
This didn't work for me:
$userMongoId = "57321c7a2977f8de306ef648";
$aq ->match([ 'user.$id' => $expr->eq($userMongoId) ])
This worked:
$aq ->match([ 'user' => new \MongoId($userMongoId) ])
Tested on Symfony 3 with the SolutionMongoAggregationBundle, MongoDB version 3.2.6.

Issue with Query Doctrine and zend 2

I have doctrine and zend 2 I do this script
$dql = $entityManager->createQuery('SELECT g FROM \Synchro\Entity\Geographicalarea g WHERE (g.namegeographicalarea = :namegeographicalarea ) AND g.codegeo = :codegeo');
$dql->setParameters(array(
'namegeographicalarea' => '$this->GetSQLValueString($nameGeographicalArea,"text")',
'codegeo' => '$this->GetSQLValueString($nameGeographicalArea,"text")',
));
$checkgeographicalarea = $query->getResult();
I var_dumped ($checkgeographicalarea) It was empty. I don't know why because I have the data
how can I resolved that ?
As long as you use Doctrine 2 and since your query was very simple i advise you to use this
$entityManager->getRepository('Synchro\Entity\Geographicalarea')->findBy(array(
'namegeographicalarea' => $nameGeographicalArea,
'codegeo' => $nameGeographicalArea,
));
Plus you have declared '$this->GetSQLValueString($nameGeographicalArea,"text")', with simple quotes wich is not working in PHP. and your codego = $nameGeographicalAreais this what you want ?
You have many issues here.

Sorting a collection in doctrine2

I've written the following query (that may or may not be efficient, I'm still a newbie):
$collection = $this->dm->getConnection()->selectCollection('db_name', 'collection_name');
$query = array('array_name' => new \MongoId(id));
$cursor = $collection->find($query)->limit(9)->sort('r', 'desc');
I'm trying to sort by an r value that looks like this in the document:
"r": 0.58325652219355106354
but it isn't actually sorting it by that r-value. What am I doing wrong?
Pretty sure sort takes an array argument. Try
->sort(['r' => 'desc]);
I looked it up...
http://apigen.juzna.cz/doc/doctrine/mongodb/source-class-Doctrine.MongoDB.Cursor.html#564-585

PHP + GridFS: Own id

I want save many files using MongoDB's GridFS but I ran into some trouble by using my own id. My simplified code is the following:
<?php
$mongo = new Mongo();
$db = $mongo->myFiles;
$grid = $db->getGridFS();
var_dump($grid->storeBytes("ForTestingPurposes", array("_id" => new MongoID("mySampleId"), array("safe" => true))));
?>
I assumed that storeBytes() returns my own id (in this case "mySampleId") but what I get is something like this:
object(MongoId)#5 (1) { ["$id"]=> string(24) "50ae7542a34156852300003d" }
.. the automatically generated ID from Mongo. Is there anything wrong with my code above? Thanks for any suggestions...
The PHP MongoId class is only for working with MongoDB ObjectIDs, which have a specific 12-byte format.
If you want to use a custom value for _id, just pass the string directly, eg:
$grid->storeBytes("ForTestingPurposes", array("_id" => 'mySampleId', array("safe" => true))));

zend framework paginator (Zend_Paginator) results too slow

I have a query that running way too slow. the page takes a few minutes to load.
I'm doing a table join on tables with over 100,000 records. In my query, is it grabbing all the records or is it getting only the amount I need for the page? Do I need to put a limit in the query? If I do, won't that give the paginator the wrong record count?
$paymentsTable = new Donations_Model_Payments();
$select = $paymentsTable->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->setIntegrityCheck(false)
->from(array('p' => 'tbl_payments'), array('clientid', 'contactid', 'amount'))
->where('p.clientid = ?', $_SESSION['clientinfo']['id'])
->where('p.dt_added BETWEEN \''.$this->datesArr['dateStartUnix'].'\' AND \''.$this->datesArr['dateEndUnix'].'\'')
->join(array('c' => 'contacts'), 'c.id = p.contactid', array('fname', 'mname', 'lname'))
->group('p.id')
->order($sortby.' '.$dir)
;
$payments=$paymentsTable->fetchAll($select);
// paginator
$paginator = Zend_Paginator::factory($payments);
$paginator->setCurrentPageNumber($this->_getParam('page'), 1);
$paginator->setItemCountPerPage('100'); // items pre page
$this->view->paginator = $paginator;
$payments=$payments->toArray();
$this->view->payments=$payments;
Please see revised code below. You need to pass the $select to Zend_Paginator via the correct adapter. Otherwise you won't see the performance benefits.
$paymentsTable = new Donations_Model_Payments();
$select = $paymentsTable->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->setIntegrityCheck(false)
->joinLeft('contacts', 'tbl_payments.contactid = contacts.id')
->where('tbl_payments.clientid = 39')
->where(new Zend_Db_Expr('tbl_payments.dt_added BETWEEN "1262500129" AND "1265579129"'))
->group('tbl_payments.id')
->order('tbl_payments.dt_added DESC');
// paginator
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($select));
$paginator->setCurrentPageNumber($this->_getParam('page', 1));
$paginator->setItemCountPerPage('100'); // items pre page
$this->view->paginator = $paginator;
Please see revised code above!
In your code, you are :
first, selecting and fetching all records that match your condition
see the select ... from... and all that
and the call to fetchAll on the line just after
and, only the, you are using the paginator,
on the results returned by the fetchAll call.
With that, I'd say that, yes, all your 100,000 records are fetched from the DB, manipulated by PHP, passed to Zend_Paginator which has to work with them... only to discard almost all of them.
Using Zend_Paginator, you should be able to pass it an instance of Zend_Db_Select, and let it execute the query, specifying the required limit.
Maybe the example about DbSelect and DbTableSelect adapter might help you understand how this can be achieved (sorry, I don't have any working example).
I personally count the results via COUNT(*) and pass that to zend_paginator. I never understood why you'd deep link zend_paginator right into the database results. I can see the pluses and minuses, but really, its to far imho.
Bearing in mind that you only want 100 results, you're fetching 100'000+ and then zend_paginator is throwing them away. Realistically you want to just give it a count.
$items = Eurocreme_Model::load_by_type(array('type' => 'list', 'from' => $from, 'to' => MODEL_PER_PAGE, 'order' => 'd.id ASC'));
$count = Eurocreme_Model::load_by_type(array('type' => 'list', 'from' => 0, 'to' => COUNT_HIGH, 'count' => 1));
$paginator = Zend_Paginator::factory($count);
$paginator->setItemCountPerPage(MODEL_PER_PAGE);
$paginator->setCurrentPageNumber($page);
$this->view->paginator = $paginator;
$this->view->items = $items;

Categories