JModelList::getListQuery - How to set limit and offset for query

3,679 views
Skip to first unread message

Ron W

unread,
Nov 3, 2011, 3:52:23 PM11/3/11
to Joomla! General Development
When constructing a query by overriding JModelList::getListQuery, how
do you set the limit for the query? The limit seems to be set to 20 by
default.

Thanks,
Ron

Phill Brown

unread,
Nov 3, 2011, 6:52:46 PM11/3/11
to joomla-de...@googlegroups.com
Hi Ron,

Setting the limit in PopulateState() using JRequest will override the limit while still allowing you to use the parent::populateState()

eg.
JRequest::setVar('limit',JFactory::getApplication()->getCfg('list_limit'));

Regards,

Phill Brown
M  04 2481 9754
Bathurst Software Solutions
-------------------------------------------------------------------------------------------------------------------




--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.


Роман

unread,
Nov 4, 2011, 5:06:09 AM11/4/11
to joomla-de...@googlegroups.com
you can also rewrite
_getList() method in your model

protected function _getList($query, $limitstart=0, $limit=0)

2011/11/4 Phill Brown <pbr...@softwaresolutions.com.au>:

--
С Уважением,
Романов Р. В.
+7 (917) 256-61-03
mailto:rrv....@gmail.com

Ron W

unread,
Nov 4, 2011, 11:04:54 AM11/4/11
to joomla-de...@googlegroups.com
Thanks Phil, it seems I need to find out more about PopulateState() to truly understand your answer but the docs are skimpy.

Ron W

unread,
Nov 4, 2011, 11:13:03 AM11/4/11
to joomla-de...@googlegroups.com
Thanks Romeo, I read the _getList() function and it seems to do exactly what I want. I'm questioning the real value of many of these functions though - I think I'll just write the database connection and query in a plain JModel class and do away with all this unnecessary abstraction.

Alfred Vink

unread,
Dec 24, 2011, 10:47:33 AM12/24/11
to joomla-de...@googlegroups.com
Could anybody elaborate on the offered solutions, I have the same problem
and can't get past the 20 limit..
Maybe a pointer to an example in the core code.

Alfred

-----Oorspronkelijk bericht-----
Van: joomla-de...@googlegroups.com
[mailto:joomla-de...@googlegroups.com] Namens Ron W
Verzonden: donderdag 3 november 2011 20:52
Aan: Joomla! General Development
Onderwerp: [jgen] JModelList::getListQuery - How to set limit and offset for
query

Thanks,
Ron

--

Mark Dexter

unread,
Dec 24, 2011, 10:57:08 AM12/24/11
to joomla-de...@googlegroups.com
In JModel, there is a method called _getList() that takes 3 args:
protected function _getList($query, $limitstart = 0, $limit = 0)

This in turn calls the JDatabase setQuery() method, that takes the
same 3 args. The limitstart and limit values are set in the
populateState method in the model, based on which "page" the user is
in.

Hope that helps. Mark

Alfred Vink

unread,
Dec 24, 2011, 11:27:28 AM12/24/11
to joomla-de...@googlegroups.com
Hi Mark,

I got it working by adding a populateState method, but apparently I don't
get the big picture, I always associated limit and limitstart with the
pagination in the backend.
So am I making a fundamental mistake here by using it frontside?

I need a list of items in my frontend form, for this I have a query that
needs to return all items, not just the standard 20...

I now have in my site model:

protected function getListQuery()
{
$user = JFactory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
$db = JFactory::getDBO();
$query = $db->getQuery(true);

$query->select('id,name,email,prefix,extra,extra2,defsubject,published,acces
s');
$query->from('#__alfcontact');
$query->where('published > 0');
$query->where('access IN ('.$groups.')');
$query->order('`ordering` ASC');

return $query;
}

protected function populateState()
{
$this->setState('list.limit', 150);
}

It now returns all items (as long as its under 150), but is this the wrong
approach ?

Alfred

-----Oorspronkelijk bericht-----
Van: joomla-de...@googlegroups.com
[mailto:joomla-de...@googlegroups.com] Namens Mark Dexter
Verzonden: zaterdag 24 december 2011 16:57
Aan: joomla-de...@googlegroups.com
Onderwerp: Re: [jgen] JModelList::getListQuery - How to set limit and offset

Reply all
Reply to author
Forward
0 new messages