cant call getModel from controller

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
froboz
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Mon Mar 21, 2011 12:30 am

cant call getModel from controller

Post by froboz » Tue Mar 10, 2015 11:39 am

I'm developing a component for Joomla 3.x and I'm having a problem with JControllerLegacy getModel. When I try to call it from within my controller.php in the dispaly function of the class CasehandlerController it breaks execution with no erorrs, just a blank page, as if the function was undefined. The very same code works fine on my local developement server but on the production server it fails when making this call. The function is as below:

Code: Select all

        function display($cachable = false, $urlparams = false) 
        {
            $this->configuration = getConfiguration();
            // set default view if not set
            $input = JFactory::getApplication()->input;
            $input->set('view', $input->getCmd('view', 'Casehandler'));
            $view = $this->getView( $input->get('view'), 'html' );
            echo 'model:';
            $model = $this->getModel('Casehandler);
            echo 'model done.';
            $view->setModel($model, true );
            
            $view->setLayout( 'default' );
            $view->display();
        } 
model: gets ecchoed and model done. does not. I've tried to debug this by echoing lines from inside the model class but with no response from that either. Calling getModel() without arguments also produces the same result. What could be the problem here when the exact same code runs fine on my local server?
Last edited by imanickam on Tue Mar 10, 2015 1:08 pm, edited 1 time in total.
Reason: Moved the topic from the forum Extensions for Joomla! 3.x to the forum Joomla! 3.x Coding

froboz
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Mon Mar 21, 2011 12:30 am

Re: cant call getModel from controller

Post by froboz » Tue Mar 10, 2015 1:44 pm

Having debugged this further by echoing lines from the JControllerLegacy class inside the getModel function I got a hint from the $prefix variable displaying the model name as casehandlerModel instead of CasehandlerModel, changed the filename of the model from casehandler.php to Casehandler.php and that fixed the problem...

kmchen
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Sat Oct 21, 2006 12:57 am
Location: marseille (13) france
Contact:

Re: cant call getModel from controller

Post by kmchen » Wed Aug 08, 2018 9:14 am

Unbelievable ! You're right. They changed the class loader on 3.6.0 so now you have to rename all your classes filenames with an uppercase in first position. Perhaps tomorrow the last letter should be uppercase and all 3rd part developpers will have to follow in silence !

I hold a project since 2009, hundreds of classes to rename !
Ithink I'd better write my own class loader. Here is one that works for me:

<?php

/**
* Lef Automatic class loader
* Searches and includes classes in admins and then in components
* @param string 'component.class' string
*/
spl_autoload_register(function ($class)
{
$types = array( 'model', 'helper' );
foreach ($types as $type)
{
if (stripos($class, $type) > 0){
list( $component, $model ) = explode($type, strtolower( $class ) );
$class_type = $type;
if (empty($model)) $model=$type;
}

}
if (isset($component)){
$filename = JPATH_ROOT . "/administrator/components/com_{$component}/{$class_type}s/{$model}.php";
if (file_exists($filename) ) require_once $filename;
else
{
$filename = JPATH_ROOT . "/components/com_{$component}/{$class_type}s/{$model}.php";
if (file_exists($filename) ) require_once $filename;
}
}
});


Now you just need to load that file in your first component file yourcomponent.php:

require_once JPATH_ROOT . "/administrator/components/com_yourcomponent/helpers/loader.php";

And call your models from where ever you want (views, controllers, models) as normal:
$model = new yourcomponentModelName()

instead of $model = $this->getModel('name') and all the addmodelpath staff


Post Reply

Return to “Joomla! 3.x Coding”