How can one retrieve the total number of items found with count() via the Zend Pagintor, NOT the page count?
Current tests come up with this approximation as the closest without manipulating the DBSelect count method:
$totalCount = $pagintor->count() * $paginator->getItemCountPerPage();
My question relates to the count() process used by the paginator to get the total number of records.
I've seen this: Zend Framework 2 - Pagination
and read these docs
http://framework.zend.com/manual/current/en/modules/zend.paginator.advanced.html
http://framework.zend.com/manual/current/en/modules/zend.paginator.usage.html
Are we to customise the count() method for the pagination object just to get the count as per last link?
class MyDbSelect extends Zend\Paginator\Adapter\DbSelect
{
public function count()
{
$select = new Zend\Db\Sql\Select();
$select->from('item_counts')->columns(array('c'=>'post_count'));
$statement = $this->sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
$row = $result->current();
$this->rowCount = $row['c'];
return $this->rowCount;
}
}
$adapter = new MyDbSelect($query, $adapter);
$paginator = new Zend\Paginator\Paginator($adapter);
Maybe I've missed something (probably true...) but since the pagination object has already gone to the trouble of compiling a 'count' why/how can we access this number without doing any other hurdles or obstacle courses...
Is there a $paginator->getTotalCount() method somewhere to access this variable...
The final result might be something like '20 records of 4536 total' where 4536 the total.
Many thanks in advance.
ENV: ZF 2.3.9 (not 2.4+)
Sounds like you want totalItemCount.
$paginator->getTotalItemCount();
Related
I've written a small extension in extbase/fluid and now I'm facing a little problem in the repository.
With my extension you can create item sthat are using system_categories for categorization (similiar to every news extension).
What I want to do is to show all items with category x on page X and all of category y on Page Y.
I know that I have to write the query in my itemRepository.php but I can't get it working, so here' my code of the repository:
public function findSearchForm($limit)
{
$query = $this->createQuery();
$query->getQuerySettings()->setLanguageUid($GLOBALS["TSFE"]->tmpl->setup['config.']['sys_language_uid']);
$query->matching(
$query->like('title','%'.$search.'%')
);
# $query->setOrderings(array('title' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING));
return $query->execute();
}
I tried to extend the query after the "query->like" even with a statement like this
$query->statement("SELECT * FROM sys_category_record_mm WHERE uid_local = '11'")
First of all: How did you enable categorization on your records? Does it correctly work in the backend?
When your TCA is correctly configured for category usage, you can use common repository methods to filter the result. But you have to combine the condition with your "title"-condition using AND.
Try something like this:
public function findSearchForm($limit)
{
$query = $this->createQuery();
$constraints = array();
$yourCategory = 11;
// Your $search-variable seems to be undefined?
//$constraints[] = $query->like('title','%'.$search.'%');
$constraints[] = $query->contains('categories', $yourCategory);
$query->matching(
$query->logicalAnd($constraints)
);
// I think you dont need this...
$query->getQuerySettings()->setLanguageUid($GLOBALS["TSFE"]->tmpl->setup['config.']['sys_language_uid']);
return $query->execute();
}
I am working on a web project in php that could potentially have a lot of users. From an administrative point of view, I am attempting to display a table containing information about each user, and seeing as the users table could grow extremely large, I would like to use pagination.
On the backend, I created a service that expects a limit and an offset parameter that will be used to query the database for records within the appropriate range. The service returns the total count of records in the table, along with the records matching the query
public static function getUsersInfo($limit = 50, $offset=1)
{
$users_count = Users::count(
array(
"column" => "user_id"
)
);
$users_info = array();
$users = Users::query()
->order('created_at')
->limit($limit, $offset)
->execute()
->toArray();
foreach ($users as $index => $user) {
$users_info[$index]['user_id'] = $user['user_id'];
$users_info[$index]['name'] = $user['first_name'] . " " . $user['last_name'];
$users_info[$index] ['phone'] = $user['phone'];
$users_info[$index] ['profile_image_url'] = $user['profile_image_url'];
}
$results = array(
'users_count' => $users_count,
'users_info' => $users_info
);
return !empty($results) ? $results : false;
}
On the frontend, what I would like to achieve ideally is, have the navigation displayed at the bottom of the table, with the typical previous, next buttons, and additionally a few numbers that allow the user to quickly navigate to a desired page if the page number displayed. This is what I have so far for the UsersController, with no pagination.
class UsersController extends ControllerBase
{
public function indexAction()
{
$usersObject = new Users();
$data = $usersObject->getUsers();
if ($data['status'] == Constants::SUCCESS) {
$users = $data['data']['users_info'];
$users_count = $data['data']['users_count'];
$this->view->setVar('users', $users);
}
echo $this->view->render('admin/users');
}
public function getUsersAction()
{
echo Pagination::create_links(15, 5, 1);
}
}
I don't have any working pagination yet, but I was thinking a good way to go would be to create a Pagination library with a create_links function that takes the
total_count of records in the database, so I know how many pages are expected
limit so I know how many records to collect
cur_page so I know where to start retrieving from
So when that function is called with the correct parameters, it would generate the html code to achieve the pagination, and that in turn can then be passed to the view and displayed.
I have never done this before, but from the research I have done so far, it seems like this might be a good way to approach it. Any guidance, suggestions, or anything at all really, regarding this would be greatly appreciated.
It looks like you are you using some bespoke MVC-ish framework. While it does not answer your question exactly I have a few points:
If you are looking at a lot of users, pagination is the least of your problems. You need to consider how the database is indexed, how the results are returned and much more.
Without understanding the underlying database abstract layer / driver you are using it is difficult to determine whether or not your ->limit($limit, $offset) line will work correctly. The offset should probably default to 0, but without knowing the code it is hard to say.
The ternary operator in your first method (return !empty($results) ? $results : false;) is currently valueless, because the statement before it will mean the variable will always be an array.
Avoid echo statements in controllers. They should return to a templating engine to output a view.
You Users class would be better named User, as the MVC framework implies that the 'Model' is a singular entity.
While it is not a general rule, most pagination systems I have used have worked on a zero-index system (Page 1 is Page 0), so calculating the limit range is simple:
$total_records = 1000;
$max_records = 20;
$current_page = 0;
$offset = $max_records * $current_page;
$sql = "SELECT * FROM foo LIMIT $offset, $max_records";
The Context
I'm using Laravel's Eloquent as my ORM. I am creating an API endpoint which provides access to Cars which have several attributes (color, make, status).
My endpoint allows clients to filter the return value by any subset of those attributes, if they provide no attributes then I will return everything.
The Question
I want to build a conditional query, which starts from "all" and narrows down based on which parameters have been specified. Here's what I've written:
public function getCars(Request $request)
{
$results = Cars::all();
if($request->has('color'))
$results = $results->where('color', $request->input('color'));
if($request->has('make'))
$results = $results->where('make', $request->input('make'));
if($request->has('status'))
$results = $results->where('status', $request->input('status'));
return $results->toJson();
}
If I call this with no parameters the API returns a list of all cars in the database.
If, however, I specify (for instance) status of 0 the API returns an empty set, despite the fact that some cars have status of 0.
Am I approaching this incorrectly? Is there something fundamental I'm missing?
Note that if instead I write:
$results = Cars::where('status', 0);
return $results->get();
The list of cars is properly generated
You should change your function like this:
public function getCars(Request $request)
{
$results = Cars::query();
if($request->has('color'))
$results = $results->where('color', $request->input('color'));
if($request->has('make'))
$results = $results->where('make', $request->input('make'));
if($request->has('status'))
$results = $results->where('status', $request->input('status'));
return $results->get()->toJson();
}
You could try this, for simplicity.
$query = Cars::query(); // no query executed, just give us a builder
$query->where(array_only($request->all(), ['color', 'make', 'status'])); // where can take a key value array to use
// update: only taking the vars you need, never trust incoming data
return $query->get(); // will be converted to Json for you
This only queries the DB for what you need. Yours is returning all results then filtering through them in a collection.
Update:
As Joseph stated, there is different functionality between $request->only() and array_only. The functionality of array_only is wanted here.
I have a query which is returning a sum, so naturally it returns one row.
I need to count the number of records in the DB which made that sum.
Here's a sample of the type of query I am talking about (MySQL):
SELECT
i.id,
i.vendor_quote_id,
i.product_id_requested,
SUM(i.quantity_on_hand) AS qty,
COUNT(i.quantity_on_hand) AS count
FROM vendor_quote_item AS i
JOIN vendor_quote_container AS c
ON i.vendor_quote_id = c.id
LEFT JOIN company_types ON company_types.company_id = c.company_id
WHERE company_types.company_type = 'f'
AND i.product_id_requested = 12345678
I have found and am now using the select_min(), select_max(), and select_sum() functions, but my COUNT() is still hard-coded in.
The main problem is that I am having to specify the table name in a tightly coupled manner with something like $this->$db->select( 'COUNT(myDbPrefix_vendor_quote_item.quantity_on_hand) AS count' ) which kills portability and makes switching environments a PIA.
How can/should I get my the count values I am after with CI in an uncoupled way??
If you want a completely decoupled way of dealing with this, just run the query to get all the rows you'd add with SUM() and then add them together in PHP.
$sum = 0;
foreach($query->result() as $row)
{
$sum += $row->quantity_on_hand;
}
Or something like that.
What about defining your table in a var or const and then doing the query like so:
define('VENDOR_QUOTE_ITEM', 'vendor_quote_item');
$this->$db->select( 'COUNT(' . VENDOR_QUOTE_ITEM . '.quantity_on_hand) AS count' );
This should be faster than $query->num_rows() as that would retrieve results and have PHP count them. The above code cuts to the chase and just asks the DB for the count without returning anything else (because it uses mysql's COUNT())
As for why $query->num_rows(); isn't working.. Make sure that var you call num_rows on a CI query result object. You should have something like this:
$your_query = $this->db->query("YOUR QUERY");
$your_query->num_rows()
if you would like to use any MySQL function inside $this->db->select() function pass the second parameter as FALSE.
So it should be $this->$db->select( 'COUNT(myDbPrefix_vendor_quote_item.quantity_on_hand) AS count' , FALSE)
Well ... while it's a different direction than I initially envisioned, I ended up simply extending CI via the directions found HERE.
I added a select_count() method to match the existing select_min(), select_max(), and select_sum() methods.
This addition only applies to MySQL at this time, but it's a solid solution.
In case someone encounters a similar problem in the future, here's what I did:
I dropped Simons "MY_Loader" directly into my "application/core"
directory (didn't need to change a thing).
Then I created a "MY_DB_mysql_driver" in the "application/core" directory,
as per his instructions ... and made it looke like this: (sans comments for brevity)
.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_DB_mysql_driver extends CI_DB_mysql_driver {
final public function __construct($params) {
parent::__construct($params);
log_message('debug', 'Extended DB driver class instantiated!');
} /* method: __CONSTRUCT */
final public function select_count($select = '', $alias = ''){
if ( !is_string($select) OR $select == ''){
$this->display_error('db_invalid_query');
}
if ($alias == ''){
$alias = $this->_create_alias_from_table(trim($select));
}
$sql = 'COUNT('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
$this->ar_select[] = $sql;
if ($this->ar_caching === TRUE){
$this->ar_cache_select[] = $sql;
$this->ar_cache_exists[] = 'select';
}
return $this;
} /* method: SELECT_COUNT */
}
Hope it helps.
Using Zend Paginator and the paginator cache works fine, but the same cached pages are returned for everything. Ie. I first look at a list of articles, when i go to view the categories the articles list is returned. How can I tell the paginator which result set i am looking for?
Also, how can I clear the paginated results without re-querying the paginator. Ie. I am updated a news article therefore the pagination needs to be cleared.
Thanks
Zend_Paginator uses two methods to define cache ID: _getCacheId and _getCacheInternalId. Second function is calculating cache ID based on two parameters: the number of items per page and special hash of the adapter object. The first function (_getCacheId) is calculating cache ID using result from _getCacheInternalId and current page.
So, if you are using two different paginator objects with 3 same internal parameters: adapter, current page number and the number of items per page, then your cache ID will be the same for these two objects.
So the only way I see is to define you own paginator class inherited from Zend_Paginator and to re-define one of these two internal functions to add a salt to cache ID.
Something like this:
class My_Paginator extends Zend_Paginator {
protected $_cacheSalt = '';
public static function factory($data, $adapter = self::INTERNAL_ADAPTER, array $prefixPaths = null) {
$paginator = parent::factory($data, $adapter, $prefixPaths);
return new self($paginator->getAdapter());
}
public function setCacheSalt($salt) {
$this->_cacheSalt = $salt;
return $this;
}
public function getCacheSalt() {
return $this->_cacheSalt;
}
protected function _getCacheId($page = null) {
$cacheSalt = $this->getCacheSalt();
if ($cacheSalt != '') {
$cacheSalt = '_' . $cacheSalt;
}
return parent::_getCacheId($page) . $cacheSalt;
}
}
$articlesPaginator = My_Paginator::factory($articlesSelect, 'DbSelect');
$articlesPaginator->setCacheSalt('articles');
$categoriesSelect = My_Paginator::factory($categoriesSelect, 'DbSelect');
$articlesPaginator->setCacheSalt('categories');