Welcome, Guest
Username: Password: Remember me

TOPIC: [FIXED] Model instance not working in Joomla module

Model instance not working in Joomla module 10 Feb 2016 20:18 #13809

  • vlemos
  • vlemos's Avatar
  • Online
  • Elite Member
  • Posts: 295
  • Thank you received: 41
  • Karma: 21
Hi All

Based on the module example "j-cook.pro/index.php/m/module" the below code should work without a problem. However, it crashes when trying to instantiate the model. Can anyone quickly tell me why?

Thanks in advance
vlemos

	@include_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_demor'.DS.'helpers'.DS.'loader.php');
	if (!class_exists('DemoCkHelper'))
	{
		JError::raiseWarning( 1000, JText::_('This module requires the cook com_demo component') );
		return;
	}
	$model = CkJModel::getInstance('Users', 'DemoModel');
The administrator has disabled public write access.

Model instance not working in Joomla module 10 Feb 2016 20:26 #13810

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Well, I see "com_demor" in you include_once. Is that the correct name?
Accoring to the rest of the text, it should be com_demo?
The administrator has disabled public write access.
The following user(s) said Thank You: vlemos

Model instance not working in Joomla module 10 Feb 2016 20:39 #13811

  • vlemos
  • vlemos's Avatar
  • Online
  • Elite Member
  • Posts: 295
  • Thank you received: 41
  • Karma: 21
Thanks for that observation Romkabouter, but the actual component is not demo so this is just a typo while posting the question. Something else is wrong, the below line fails and not because of a typo.
$model = CkJModel::getInstance('Users', 'DemoModel');
Last Edit: 10 Feb 2016 20:42 by vlemos.
The administrator has disabled public write access.

Model instance not working in Joomla module 10 Feb 2016 20:47 #13812

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
Ok, just checking ;)

Code looks fine, what error do you get? You should see an error 500?
The administrator has disabled public write access.

Model instance not working in Joomla module 10 Feb 2016 21:00 #13813

  • vlemos
  • vlemos's Avatar
  • Online
  • Elite Member
  • Posts: 295
  • Thank you received: 41
  • Karma: 21
No error code, just mangles my page :(
The administrator has disabled public write access.

Model instance not working in Joomla module 11 Feb 2016 00:42 #13814

  • vlemos
  • vlemos's Avatar
  • Online
  • Elite Member
  • Posts: 295
  • Thank you received: 41
  • Karma: 21
Hi Admin

Based on this thread, can you offer any reason why this line may fail in a Joomla module?

Thanks
vlemos

$model = CkJModel::getInstance('Users', 'DemoModel');

Not sure why but looks like it fails while trying to find and load "DemoClassModelList"
Last Edit: 11 Feb 2016 02:31 by vlemos.
The administrator has disabled public write access.

Model instance not working in Joomla module 11 Feb 2016 10:43 #13815

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
All this is wierd.

Actually there is a random (because not found yet) problem with the Joomla Loader I can't sort it out.

First, are you sure the component loader is correctly loaded ? (remove the @, and put a exit('OK'); in the top of the file)
Note that the DS const is deprecated. You can try without, but this will not fix your problem.
Is it working with another component ?

Wich component is it ?


Well, I am digging in.
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: vlemos

Model instance not working in Joomla module 11 Feb 2016 11:26 #13816

  • vlemos
  • vlemos's Avatar
  • Online
  • Elite Member
  • Posts: 295
  • Thank you received: 41
  • Karma: 21
Hi Admin

Yes, the component is correctly loaded, the line below executes and finds the class without an error.
if (!class_exists('CommuterCkHelper'))

I also removed the "@" but it did not help

After tracing the code, it seems that the class "CommuterClassModelList" cannot be found

I will try loading the demo module and its component and get back to you.

The component is "com_commuter"

Thanks
vlemos
The administrator has disabled public write access.

Model instance not working in Joomla module 11 Feb 2016 11:50 #13817

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
I tried, and it works like a charm for me.
Try on a fresh Joomla installation to see if you have something broken.

In case, add manually the loading of the missing files. > In the loader.
You can try :
CommuterClassLoader::register('CommuterClassModelList', JPATH_ADMIN_COMMUTER ."/classes/model/list.php", true);

Or, directly put a require_once(...), but this will always load the class, even if not used, but this would not be optimized for ressources. In your case it is not a big deal because this file is almost always loaded with the component.

This is very common in Joomla, the loader is unstable. I saw this many times, but always working for me.
It is not a matter of version of Joomla, it lacks since a long time and still.


For debugging, you can also try :
CommuterClassLoader::dump();
It will output the registered and recognized classes.

Hope it helps.
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: vlemos

Model instance not working in Joomla module 12 Feb 2016 02:46 #13818

  • vlemos
  • vlemos's Avatar
  • Online
  • Elite Member
  • Posts: 295
  • Thank you received: 41
  • Karma: 21
Thanks again Admin, k++

The issue was not within the module at all. It was the one thing I would have never suspected. Finally all the logic pointed to the component but I disregarded it for a while, because it was a "Cook", and Cooks don't fail catastrophically. :cheer:

The issue was simple in the end. The class load file:
. . .\classes\loader.php

uses "/" when creating the "namespace" and somehow my environment doesn't like that. Earlier versions of Cook used "DS" and this works just fine.
181	$namespace = explode('/', $relativePath);

Changed to: $namespace = explode(DS, $relativePath);

230	self::register($class, $file->getPath() .'/'. $fileName);

Changed to: self::register($class, $file->getPath() .DS. $fileName);
So if you ever have an issue loading classes, you may want to look at the loader.php file.

Warm regards
vlemos
The administrator has disabled public write access.
The following user(s) said Thank You: admin

Model instance not working in Joomla module 12 Feb 2016 11:36 #13819

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Oh no... the karma is for you !
Thank you so much !
K++
You have fixed a simple, but very ennoying bug that I couldn't find because I am not working with windows.
I also extended your account for one month.


DS ! Yes of course !!!
Excellent catch. It was a cook problem. How couldn't I see it ?

DS are deprecated in Joomla components because PHP handles it correctly, so I removed them all in the 'pure' version.

BUT, in case of windows, the backslashes are used so in case of string treatment of a path, DS must be used. That's logic.


Fixed.


In the 'pure' version (still not there yet), the loader superclass is limited to the discovery() function. Reduced and cleaned file, because the rest was B/C handling (legacy Joomla versions).
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: vlemos

Model instance not working in Joomla module 09 Jun 2016 17:18 #14032

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Another fix for this one.

Now it is MUCH Better.
I explain :

In your loader file, you can find this important line for the controller to find the models
CkJController::addModelPath(JPATH_XXXXXX . '/models');

Cook was before omissing the prefix name, including the component name, wich is fondamental. Actualy it should be an obligation to set it.

Now, the correct code is:
CkJController::addModelPath(JPATH_XXXXXX . '/models', 'XxxxModel');

Otherwise, you will get conflicts if you are loading 2 different components loader, containing same model name.
In my case, I had twice the model named "Session", and so when I was including the second component, big crash.

This was not happening only in modules.

So this one is definitively fixed.
Also, you will find some changes in the Loader superclass in order to load correctly the Table files from the modules.
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: vlemos
Time to create page: 0.117 seconds

Get Started