Joomla Database - How to use LIMIT in getQuery?

前端 未结 4 1451
别跟我提以往
别跟我提以往 2021-02-04 02:42

I want to build the below query using joomla inbuilt database class.

SELECT * 
FROM table_name
ORDER BY id DESC
LIMIT 1

This is the query I hav

4条回答
  •  [愿得一人]
    2021-02-04 02:58

    SetLimit doesn't work for me in Joomla 3.4.x, so try:

    Within the model:

    protected function getListQuery()
    {
        // Create a new query object.
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
    
        // Select some fields
        $query->select('*');
        $query->from('#__your_table');
    
        $this->setState('list.limit', 0); // 0 = unlimited
    
        return $query;
    }
    

    Davids answer: https://joomla.stackexchange.com/questions/4249/model-getlistquery-fetch-all-rows-with-using-jpagination

    Run that before the model calls getItems and it will load all the items for you.

    A few caveats with this.

    You can also do this outside the model, so if for instance you were in your view. You could do the following:

    $model = $this->getModel(); $model->setState('list.limit', 0);

    Sometimes you can do this too early, before the model's state has been populated, which will cause the model to get rebuilt from the user state after you have set the limit, basically overriding the limit.

    To fix this, you can force the model to populate its state first:

    $model = $this->getModel(); $model->getState(); $model->setState('list.limit', 0); The actual populateState method is protected, so outside the model you can't call it directly, but any call to getState will make sure that the populateState is called before returning the current settings in the state.

提交回复
热议问题