Welcome, Guest
Username: Password: Remember me

TOPIC: Context state overrides

Context state overrides 30 Oct 2016 11:14 #14666

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
I ask this question once per year, as every year it gives me great grief

I have two views, same table, each will have different limits and default ordering.. here is what I have so far
public function populateState($ordering = null, $direction = null)
    {
        // Initialise variables.
        $app = JFactory::getApplication();
        $session = JFactory::getSession();
        $acl = MyComponentHelper::getActions();

        parent::populateState('a.film_title', 'asc');
        $this->setState('list.limit', 15); 

        if($this->context == "films.latestfilms")
        {
            parent::populateState('a.creation_date', 'desc');
            $this->setState('list.limit', 10);  
        }
    }

The overall state works (film title, 15)

but the contextual one (lates films) doesnt

How can I correctly code this populate state to work

Thanks
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.

Context state overrides 30 Oct 2016 13:47 #14667

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
As odering was not really a necessary feature for user selection in my app, I just wrote the ordering into the SQL
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.

Context state overrides 30 Oct 2016 19:42 #14668

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Does the if statement: if($this->context == "films.latestfilms") work?

I always fork the model function prepareQuery for this. Then use $this->addOrder to it.
The administrator has disabled public write access.

Context state overrides 31 Oct 2016 11:16 #14670

  • liubov
  • liubov's Avatar
  • Offline
  • Elite Member
  • (=) 10 mn and it's ready!
  • Posts: 279
  • Thank you received: 35
  • Karma: 22
If you have 2 views from a Table you should find the 2 'case' treatment in the model.
case 'view 1':
....
break;

case 'films.latestfilms':
....
//LIMIT
$this->setState('list.limit', 10);
//ORDER
$query->order('a.creation_date desc');	

break;

I think it's the right place to do that and It works fine ...
The administrator has disabled public write access.

Context state overrides 01 Nov 2016 11:04 #14672

  • admin
  • admin's Avatar
  • Online
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 985
  • Karma: 140
Missing the component alias : com_xxxxx

There are 2 ways to get the model context :

1.
$this->context; (from the model itself) OR $model->get('context');
It contains the full namespaced context : com_xxxxxx.films.latestfilms

2.
$model->getState('context')
It only contains the relative context : films.latestfilms

- If you call getState() from the populateSate() function, you will end with a endless recursive loop. So you need to use the first method.
if ($this->context == 'com_xxxxxx.films.latestfilms')
{
	$this->setState('list.limit', 10);
}
Coding is now a piece of cake
Last Edit: 01 Nov 2016 11:04 by admin.
The administrator has disabled public write access.
The following user(s) said Thank You: organicwebs
Time to create page: 0.193 seconds

Get Started