Joomla 3.6 Changes around LegacyController ?

243 views
Skip to first unread message

Honkoman

unread,
Jul 7, 2016, 11:33:05 AM7/7/16
to Joomla! CMS Development
Hi there,
i just got a list of errors when testing my component with J3.6.

I'm using backend models in the frontend by doing something like this in a controller:

$this->addModelPath( JPATH_ADMINISTRATOR .'/components/com_compo/models/' );
$model = $this->getModel('Objects', 'MyModel'); // get backend model
$model->doSomething();

That worked since Joomla 2.5. Not anymore. Now i found i have to write the prefix

$this->addModelPath( JPATH_ADMINISTRATOR .'/components/com_compo/models/, 'MyModel'' );
$model = $this->getModel('Objects', 'MyModel'); // get backend model
$model->doSomething();

I don't get it because both models in the front- and backend have had the same Prefix 'MyModel' all the time.

Can someone explain what happened here?

Michael Babker

unread,
Jul 7, 2016, 11:38:45 AM7/7/16
to joomla-...@googlegroups.com
Try reverting https://github.com/joomla/joomla-cms/commit/52bcb7dc4641249b6565bf8d999623ba938d70fd and see what happens?  That's the only change in the controller classes.


--
You received this message because you are subscribed to the Google Groups "Joomla! CMS Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-cm...@googlegroups.com.
To post to this group, send an email to joomla-...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-cms.
For more options, visit https://groups.google.com/d/optout.

Allon Moritz

unread,
Jul 7, 2016, 11:40:45 AM7/7/16
to joomla-...@googlegroups.com
It is advisable to use them with a prefix anyway, now is the chance to do it ;-)

To post to this group, send email to joomla-...@googlegroups.com.

Izhar Aazmi

unread,
Jul 7, 2016, 12:07:25 PM7/7/16
to joomla-...@googlegroups.com

That PR fixes another issue that needs to be considered when trying to revert it.

I'd rather suggest fixing the current issue instead of reverting. Well I can't say anything for sure right now as I'm out on vacation. I'll see to it likely this Sunday.

Izhar Aazmi

unread,
Jul 13, 2016, 8:38:12 AM7/13/16
to Joomla! CMS Development
Today I had time to look into the matter and I added below code to the com_contact's contact controller 

public function test()
{
    $this
->addModelPath(JPATH_ADMINISTRATOR . '/components/com_content/models/');
    $model
= $this->getModel('Article', 'ContentModel');  // 'contentModel' also worked (lowercase first character)

    printr
($model);
}

and followed the url

http://localhost/~izhar/staging/index.php?option=com_contact&task=contact.test

And it dumped the following (trimmed for brevity):

ContentModelArticle Object
(
    [text_prefix:protected] => COM_CONTENT
    [typeAlias] => com_content.article
    [associationsContext:protected] => com_content.item
...


I intentionally used contact controller and content model to avoid any implicit assumption between them. Still that was loaded fine. I then uses this with content controller and content model too and it again worked.

On further investigation, I see the JModelLegacy::getInstance code that clearly attempts to find the model path first with prefix and then without prefix.

...

$path
= JPath::find(self::addIncludePath(null, $prefix), self::_createFileName('model', array('name' => $type)));

if (!$path)
{
    $path
= JPath::find(self::addIncludePath(null, ''), self::_createFileName('model', array('name' => $type)));
}
...

The change in the said PR does not affect any model loaded this way. 

So as of now I wasn't able to reproduce this issue. If I am offtrack here then please let me know. I'd be able to better investigate if I had access to your component for testing.

Thanks.

--

Honkoman

unread,
Jul 13, 2016, 9:48:13 AM7/13/16
to Joomla! CMS Development
There is definitely a change that affects the way the models gets loaded. I tried today upgrading to J3.6 with a untouched version of my component and the same effect occurred. 

I had to switch:
$this->addModelPath( JPATH_ADMINISTRATOR .'/components/com_compo/models/');     
to
$this->addModelPath( JPATH_ADMINISTRATOR .'/components/com_compo/models/, 'MyModel'' );  

If i don't do that the frontend model gets loaded first.

Izhar Aazmi

unread,
Jul 13, 2016, 10:38:05 AM7/13/16
to joomla-...@googlegroups.com
Okay now I probably get it. You have model with name in the front-end as well as back-end. 

As you must already know you therefore can't load both instances (normally) in one session. Whichever loads first will take priority, second will never be seen. You should never rely on this as you don't know for sure which instance you get. You may always end up getting frontend model in case some other 3pd plugin loads it for you already. In that case your addModelPath will be useless.

If it worked previously, I am not sure how, probably luck. Joomla does not allow this IMO.

Refer to same code in my previous message. I have highlighted the difference below. 

1. When you add model path (include path) it is mapped to given prefix and the global prefix both.
2. When you load a model from listed paths most recent valid path is used. First this tries to load with paths assigned with 'MyModel' prefix. if not found it fallbacks to blank (global) prefix

...

$path 
= JPath::find(self::addIncludePath(null, $prefix), self::_createFileName('model', array('name' => $type)));

if (!$path)
{
    $path 
= JPath::find(self::addIncludePath(null, ''), self::_createFileName('model', array('name' => $type)));
}
...

Hope this helps. If you find anything relevant, please write back here. Pardon me for any error or misinterpretation. 


PS: You should consider using autoloadable paths if at all possible. This will avoid such pitfalls too.

Honkoman

unread,
Jul 13, 2016, 1:15:54 PM7/13/16
to Joomla! CMS Development
Yeah i know i can't load both (frontend and backend) instances of a model at a time. I'm not trying to do that. I just need to load the backend model in a frontend controller. The frontend model is not of interest at that point.
Looks like without using the Prefix as the third parameter it loads the frontend model under J3.6, which it did not before, because i added the backend model path on top via:

Izhar Aazmi

unread,
Jul 14, 2016, 3:01:50 AM7/14/16
to joomla-...@googlegroups.com

Can you give output of the following code exactly before addModelPath, and before and after getModel calls.


$all = JModelLegacy::addIncludePath(null, '');

And

$my = JModelLegacy::addIncludePath(null, 'MyModel');


Honkoman

unread,
Sep 23, 2016, 1:27:12 PM9/23/16
to Joomla! CMS Development
Sorry i did not answer, but i also could avoid the error by doing this:

jimport( 'joomla.application.component.model' );
JLoader::import( 'objects', JPATH_SITE . '/components/com_mycomp/models' );
$model = JModelLegacy::getInstance( 'Objects', 'MyModel' );
$model->doSomething(); 
Reply all
Reply to author
Forward
0 new messages