Setting model state in the new MVC classes

395 views
Skip to first unread message

Rune V. Sjøen

unread,
Jul 25, 2012, 10:03:32 AM7/25/12
to joomla-de...@googlegroups.com
Hello all,

I was poking around the new MVC classes and apparently the setState() of JModel now only supports a JRegistry object as an argument. Not only that, it does not support "merging" another registry object to append keys to the state. 

In the old MVC the controller has been able to set/modify state values in the model by calling setState() with two parameters (key/value), this is no longer possible and would result in the controller having to 1. get the registry object, 2. set the value, 3. set the registry object back in the model to be able to do this.

So to sum it all up, I have two questions.

1. Is there a particular reason why the setState() function can't support both setting a new JRegistry object, and setting a new value in the registry object based on a key/value pair?

2. While I do get that type hinting is the new cool stuff in the platform, does it make sense to force the object type in this case , thus making the implementation less flexible for people that want to use the JModel interface ?

Cheers,
Rune

Louis Landry

unread,
Jul 25, 2012, 10:52:36 AM7/25/12
to joomla-de...@googlegroups.com
I've been struggling with the same things Rune. It may have simply been a case of over zealous simplification.

L

Sent from my iPad

Rune V. Sjøen

unread,
Jul 25, 2012, 12:04:21 PM7/25/12
to joomla-de...@googlegroups.com
Personally I would probably prefer something like this 

        public function setState($name, $value = null)
        {
                if($name instanceof JRegistry)
                {
                        $this->state->merge($name);
                }
                else
                {
                        $this->state->set($name, $value);
                }
        }

But that does create an ambiguous argument name since the first argument can both be a path and an object. With maybe an optional argument whether to merge or just replace the whole registry object. Quite possibly it would be better to implement two methods in the interface. And of course their getters respectively.

public function setState($name, $value);
public function setStore(JRegistry $object);

This would allow proper type hinting where needed, and make the behavior consistent to what a lot of software already out there expects, being able to set values in the state using setState()

- Rune

Louis Landry

unread,
Oct 9, 2012, 2:47:20 AM10/9/12
to joomla-de...@googlegroups.com
That's actually not a bad idea Rune.  I'd like to hear what others think on this.  It may be just as easy if you are willing to just put together a quick pull request for the basics here and we can discuss the details in the PR discussion thread.  Whatever works for you.

Cheers.

- Louis

Andrew Eddie

unread,
Oct 9, 2012, 3:28:06 AM10/9/12
to joomla-de...@googlegroups.com
Just thinking out loud.

On 26 July 2012 02:04, Rune V. Sjøen <rvs...@gmail.com> wrote:
> public function setState($name, $value);
> public function setStore(JRegistry $object);

public function setState($name, $value);

public function putState(JRegistry $object); // ??
// or
public function loadState(JRegistry $object); // ??

We use the "load*" in other places for dependancy injection in the
application object.

Regards,
Andrew Eddie
http://learn.theartofjoomla.com - training videos for Joomla developers

Emerson da Rocha Luiz

unread,
Oct 9, 2012, 7:42:12 AM10/9/12
to joomla-de...@googlegroups.com

Be able to merge states again, on the new MVC, is a good idea. I just do not have a strong opinion about names of new or old  methods to do it.

att. Emerson

/android

goldenmean

unread,
Oct 11, 2012, 11:56:54 AM10/11/12
to joomla-de...@googlegroups.com
I prefer Andrew's nomenclature. To me it is more readily apparent how the different methods are to be used, and I like that "State" is consistent across the method names.
Reply all
Reply to author
Forward
0 new messages