MVC for Joomla tutorial and array_merge

This forum is for general questions about extensions for Joomla! 2.5.

Moderators: pe7er, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
neo314
Joomla! Apprentice
Joomla! Apprentice
Posts: 36
Joined: Sat Mar 13, 2010 4:22 am

MVC for Joomla tutorial and array_merge

Post by neo314 » Mon Dec 24, 2012 6:15 am

Hi,

I'm running through the tutorial for Developing a Model-View-Controller Component for Joomla 2.5 on the 7th step.

In the /admin/models/fields/helloworld.php file is the following code:

Code: Select all

protected function getOptions() 
        {
                $db = JFactory::getDBO();
                $query = $db->getQuery(true);
                $query->select('id,greeting');
                $query->from('#__helloworld');
                $db->setQuery((string)$query);
                $messages = $db->loadObjectList();
                $options = array();
                if ($messages)
                {
                        foreach($messages as $message) 
                        {
                                $options[] = JHtml::_('select.option', $message->id, $message->greeting);
                        }
                }
                $options = array_merge(parent::getOptions(), $options);
                return $options;
        }
Can anyone explain the use of array_merge here? What is it being merged with? Does it contain the whole select element rather than just the options and it is being merged with any other select elements in BE of the component.

Also, is there a recommended way to answer a question like this such as turning on debugging or something rather than trying to put my own dumps into the output to see what is happening?

Thanks.

User avatar
stutteringp0et
Joomla! Ace
Joomla! Ace
Posts: 1389
Joined: Sat Oct 28, 2006 11:16 pm
Location: Texas
Contact:

Re: MVC for Joomla tutorial and array_merge

Post by stutteringp0et » Mon Dec 24, 2012 8:50 am

What you're looking at is the options of the parent class being merged with the options of this extended class. This is useful in the event that the parent class has separate options from this child class. Duplicate option items will be replaced with the new values from this child class.

If your extension isn't going to do anything with the options from the parent class, I don't see any reason you would need to merge them in. It is a tutorial after all, just giving an example.
My extensions: http://extensions.joomla.org/profile/pr ... ails/18398
Honk if this signature offends you.

neo314
Joomla! Apprentice
Joomla! Apprentice
Posts: 36
Joined: Sat Mar 13, 2010 4:22 am

Re: MVC for Joomla tutorial and array_merge

Post by neo314 » Wed Dec 26, 2012 6:03 am

stutteringp0et wrote:[snip] It is a tutorial after all, just giving an example.
Thanks for the clarification. I didn't see anything to merge with in the parent class unless other processes were going on I was unaware of. Examples have to be understood to be useful. :geek:

User avatar
stutteringp0et
Joomla! Ace
Joomla! Ace
Posts: 1389
Joined: Sat Oct 28, 2006 11:16 pm
Location: Texas
Contact:

Re: MVC for Joomla tutorial and array_merge

Post by stutteringp0et » Wed Dec 26, 2012 5:13 pm

What you're seeing is method inheritance. Although you might not see that method in the parent class, the parent class inherits it from something up the chain. When you follow the classes, you'll find where it exists.
My extensions: http://extensions.joomla.org/profile/pr ... ails/18398
Honk if this signature offends you.

neo314
Joomla! Apprentice
Joomla! Apprentice
Posts: 36
Joined: Sat Mar 13, 2010 4:22 am

Re: MVC for Joomla tutorial and array_merge

Post by neo314 » Wed Dec 26, 2012 6:37 pm

Thanks. I understood that. Looking up the class chain, I couldn't see what it would be merged to unless it had to be merged with other processes using the same parent class.

User avatar
stutteringp0et
Joomla! Ace
Joomla! Ace
Posts: 1389
Joined: Sat Oct 28, 2006 11:16 pm
Location: Texas
Contact:

Re: MVC for Joomla tutorial and array_merge

Post by stutteringp0et » Wed Dec 26, 2012 6:50 pm

OK, so I'll go into great detail.

/admin/models/fields/helloworld.php is an input field that extends JFormFieldList

Looking at the source for JFormFieldList, options provide a method for individual options to be defined within the JForm XML definition.

So, when using the JFormFieldHelloWorld class, the options retrieved from the database are merged with the options defined within XML.

<field type="helloworld" name="helloworld" default="xyzpdq">
<option value="xyzpdq">This is the default option defined with XML</option>
</field>
My extensions: http://extensions.joomla.org/profile/pr ... ails/18398
Honk if this signature offends you.

User avatar
stutteringp0et
Joomla! Ace
Joomla! Ace
Posts: 1389
Joined: Sat Oct 28, 2006 11:16 pm
Location: Texas
Contact:

Re: MVC for Joomla tutorial and array_merge

Post by stutteringp0et » Wed Dec 26, 2012 6:54 pm

Oh, and the method getOptions is a method of the immediate parent class of JFormFieldHelloWorld - If you'd looked for it, it was easy to find. There was no complicated inheritance to follow.
My extensions: http://extensions.joomla.org/profile/pr ... ails/18398
Honk if this signature offends you.

neo314
Joomla! Apprentice
Joomla! Apprentice
Posts: 36
Joined: Sat Mar 13, 2010 4:22 am

Re: MVC for Joomla tutorial and array_merge

Post by neo314 » Thu Dec 27, 2012 1:16 am

Thanks. That was very helpful. I did find getOptions, but still had some difficulty figuring out what was being merged with what. I think I understand how JHtml works a bit better now which helps me understand what is being created in the array.

Is it possible to easily display the XML that is being generated?


Locked

Return to “Extensions for Joomla! 2.5”