?
avatar LaurensGoedel
LaurensGoedel
14 Jul 2016

Steps to reproduce the issue

Try to access the administrator model in the frontend controller by

JModelLegacy::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.'/models/');

Expected result

$productModel = JModelLegacy::getInstance('Product', 'StockModel');
Should now be the administrator model

Actual result

It is actually the frontend model

System information (as much as possible)

Joomla 3.6.0
PHP 5.6.14
Webserver Apache/2.4.17 (FreeBSD) OpenSSL/1.0.1p-freebsd PHP/5.6.14 mpm-itk/2.4.7-03

Additional comments

When JModelLegacy::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.'/models/'); is replaced by require_once JPATH_COMPONENT_ADMINISTRATOR.'/models/product.php'; the administrator model is loaded

avatar LaurensGoedel LaurensGoedel - open - 14 Jul 2016
avatar ggppdk
ggppdk - comment - 14 Jul 2016

@LaurensGoedel

I think this is not a bug ...

proper usage would be to add a 2nd parameter (the 'prefix') to it, so this:

JModelLegacy::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.'/models/');
$productModel = JModelLegacy::getInstance('Product', 'StockModel');

should be:

$_prefix = 'StockModel';
JModelLegacy::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.'/models/', $_prefix);
$productModel = JModelLegacy::getInstance('Product', $_prefix);
avatar ggppdk
ggppdk - comment - 14 Jul 2016

Still the fact that it worked before is a B/C break ?

avatar LaurensGoedel
LaurensGoedel - comment - 14 Jul 2016

That seems to work indeed! But why did it work till 3.5.1 without the prefix?
The function inside JModelLegacy isnt' changed.

avatar brianteeman
brianteeman - comment - 14 Jul 2016

If something worked because of a bug and the bug is fixed it's unfortunate
but not a b/c issue

avatar ggppdk
ggppdk - comment - 14 Jul 2016

I confirm this behaved differently in J3.5.x,
i am afraid this is a B/C break,

  • some software maybe using this without the prefix ?

Almost all usage of it in core files, that i could find
is making use of the $prefix when calling

JModelLegacy::addIncludePath()

But i found 1 case not using it (also 1 in com_admin)
https://github.com/joomla/joomla-cms/blob/staging/administrator/components/com_categories/helpers/categories.php#L148-L151

now because there is not a frontend model for it, no bug will manifest, if someone tries to use the categories helper from frontend

avatar b2z
b2z - comment - 14 Jul 2016

public static function addIncludePath($path = '', $prefix = '')

$prefix is an optional parameter. I was never using $prefix in my extensions, because it is optional. And I am sure that many developers also. If behavior without it was changed then it is a B/C break.


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/11115.

avatar izharaazmi
izharaazmi - comment - 16 Jul 2016

I'd like to investigate on this.

For people who are having similar issue with:

JModelLegacy::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/models/');
$productModel = JModelLegacy::getInstance('Product', 'StockModel');

Can you provide attach the output of the following code (Change the Model Prefix StockModel to your components prefix):

$paths   = array();
$paths[] = JModelLegacy::addIncludePath(null, '');
$paths[] = JModelLegacy::addIncludePath(null, 'StockModel');

JModelLegacy::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/models/');

$paths[] = JModelLegacy::addIncludePath(null, '');
$paths[] = JModelLegacy::addIncludePath(null, 'StockModel');

$productModel = JModelLegacy::getInstance('Product', 'StockModel');

$paths[] = JModelLegacy::addIncludePath(null, '');
$paths[] = JModelLegacy::addIncludePath(null, 'StockModel');

echo '<pre>';
print_r($paths);
echo '</pre>';
avatar LaurensGoedel
LaurensGoedel - comment - 1 Aug 2016

output of the code above:

Array
(
    [0] => Array
        (
            [0] => -webdir-/components/com_stock/models
        )

    [1] => Array
        (
            [0] => -webdir-/components/com_stock/models
        )

    [2] => Array
        (
            [0] => -webdir-/administrator/components/com_stock/models/
            [1] => -webdir-/components/com_stock/models
        )

    [3] => Array
        (
            [0] => -webdir-/components/com_stock/models
        )

    [4] => Array
        (
            [0] => -webdir-/administrator/components/com_stock/models/
            [1] => -webdir-/components/com_stock/models
        )

    [5] => Array
        (
            [0] => -webdir-/components/com_stock/models
        )

)
avatar izharaazmi
izharaazmi - comment - 1 Aug 2016

Please also check for

print_r(class_exists('StockModelProduct', false));

This should return false, if your backend model is supposed to load. If it returns true that means your front-end model has already been loaded and there will be no effect of addModelPath or addIncludePath anymore.

avatar izharaazmi
izharaazmi - comment - 1 Aug 2016

Well, on little investigation I found it to be caused by the fix I did with #9409. However, that is a real fix, it has to be that way.

On further investigation, I found that earlier (before that fix) the model paths were stored as:

Array
(
    [stockModel] => Array
        (
            [0] => /-/components/com_stock/models
        )
    [] => Array
        (
            [0] => /-/components/com_stock/models
        )
)

Notice lowercase "s" in stockModel. When looking up for StockModel (uppercase "S") using:

$productModel = JModelLegacy::getInstance('Product', 'StockModel');

It was never found and hence fallback to "" prefix (i.e. path with no prefix) was used. That's why,

JModelLegacy::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/models/');

always worked. Now it doesn't because When looking up for StockModel using the same code above it finds the correct path array and uses it already.

Summary

The fix is correct and the behaviour is appropriate. However, if we think we should provide B/C support here too, its fine and simple too. We'd just ignore the case of the prefix in addIncludePath.

EDIT: Sorry, that case-insensitivity would be good, but still won't provide the B/C. Only removing prefix from the call at JControllerLegacy would do this.

But I won't do it unless advised. May be @brianteeman can decide the best here.

avatar brianteeman
brianteeman - comment - 3 Aug 2016

@wilsonge your advice please on moving forward with this


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/11115.

avatar brianteeman brianteeman - change - 7 Sep 2016
Status New Needs Review
avatar brianteeman
brianteeman - comment - 21 May 2017

@wilsonge @rdeutz it appears from the comments that there was a b/c break BUT its been almost a year now so can we just close this?

avatar rdeutz rdeutz - change - 21 May 2017
The description was changed
Status Needs Review Closed
Closed_Date 0000-00-00 00:00:00 2017-05-21 11:51:00
Closed_By rdeutz
avatar rdeutz rdeutz - close - 21 May 2017
avatar jcodewalker
jcodewalker - comment - 5 Sep 2019

Hello,
It seems it's not fixed permanently..
J 3.9.11
In site view I'm not able to load the admin model (I can load other views admin model instead, other component models too):

Array
(
[0] => Array
(
[0] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_fields/models
[1] => /Library/WebServer/Documents/web/testmodel/components/com_test/models
[2] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_actionlogs/models
)

[1] => Array
    (
        [0] => /Library/WebServer/Documents/web/testmodel/components/com_test/models
    )

[2] => Array
    (
        [0] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_test/models/
        [1] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_fields/models
        [2] => /Library/WebServer/Documents/web/testmodel/components/com_test/models
        [3] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_actionlogs/models
    )

[3] => Array
    (
        [0] => /Library/WebServer/Documents/web/testmodel/components/com_test/models
    )

[4] => Array
    (
        [0] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_test/models/
        [1] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_fields/models
        [2] => /Library/WebServer/Documents/web/testmodel/components/com_test/models
        [3] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_actionlogs/models
    )

[5] => Array
    (
        [0] => /Library/WebServer/Documents/web/testmodel/components/com_test/models
    )

)

With or without the famous prefix... no way.

avatar mbabker
mbabker - comment - 6 Sep 2019

Please provide a full reproduction scenario. Dumping a debug output here provides absolutely no information on how someone can recreate your issue (and write automated tests to prevent it from reoccurring if confirmed to be a bug). There is a reason it’s generally suggested to open new items instead of randomly commenting on closed issues, part of it being the issue template which asks important questions to help people triage the issue.

avatar jcodewalker
jcodewalker - comment - 6 Sep 2019

Hello,
I just try to use the admin model in site side, same view name, let's say 'order', so I have 'MycomponentModelOrder' in the backend and 'MycomponentModelOrder' in the frontend.
Now, in the view 'view.html.php' (frontend) I want to load the backend model.
I tried with (in different attempt):
$test = JModelLegacy::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.'/models/', $_prefix);
var_dump($test) returns:
Array
(
[0] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_test/models
[1] => /Library/WebServer/Documents/web/testmodel/components/com_test/models
)
and JModelLegacy::getInstance('Order', $_prefix);
loads the frontend model

$test = JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_test/models/');
var_dump($test) returns:
Array
(
[0] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_test/models
[1] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_fields/models
[2] => /Library/WebServer/Documents/web/testmodel/components/com_test/models
[3] => /Library/WebServer/Documents/web/testmodel/administrator/components/com_actionlogs/models
)
and JModelLegacy::getInstance('Order', $_prefix); returns frontend model


$test = JModelLegacy::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.'/models, 'TestModel');
var_dump($test) returns:
Array
(
[0] => /Library/WebServer/Documents/web/bpplan/administrator/components/com_bpplan/models
[1] => /Library/WebServer/Documents/web/bpplan/administrator/components/com_fields/models
[2] => /Library/WebServer/Documents/web/bpplan/components/com_bpplan/models
[3] => /Library/WebServer/Documents/web/bpplan/administrator/components/com_actionlogs/models
)
and JModelLegacy::getInstance('Order', $_prefix); loads the frontend model.

All tested with and without $_Prefix = 'TestModel'; (which I confess I never used so far...)

At the same time, all the above work with different model name and even different components/models, so basically it seems in the front end I'm not able to load the admin model of the same component in same view/model name.

So, to reproduce the scenario, open the front view.html.php and try to load the relative model but from admin side.
I hope you've got the picture.

Add a Comment

Login with GitHub to post a comment