Advertisement

Am I doing something stupid? Topic is solved

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, 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.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
david0058
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 131
Joined: Wed Jan 20, 2016 10:58 pm

Am I doing something stupid?

Post by david0058 » Tue Dec 31, 2024 5:16 pm

I thought I'd figured out how this namespacing works, but apparently not.

I have a component with this model:

administrator/components/com_example/src/Model/FlowsModel.php

Code: Select all

namespace Acme\Component\Example\Administrator\Model;

use Joomla\CMS\MVC\Model\ListModel;

class FlowsModel extends ListModel
{   
    public function copyflows($ids)
    {   
        return 'success';
    }
}
My Controller looks like this:

administrator/components/com_example/src/Controller/FlowsController.php

Code: Select all

namespace Acme\Component\Example\Administrator\Controller;

use Joomla\CMS\MVC\Controller\AdminController;

class FlowsController extends AdminController
{
    public function copyflows()
    {   
        // Get the input
        $pks = $this->input->post->get('cid', array(), 'array');
        $pks = ArrayHelper::toInteger($pks);

        // Get the model
        $model = $this->getModel('Flows', 'Administrator');

        // Check if the model exists and call the copyflows method
        if ($model)
            $msg = $model->copyflows($pks);
         else
            $msg = 'Model not found';		// <<<<< THIS MESSAGE IS DISPLAYED

        Factory::getApplication()->enqueueMessage($msg, 'notice');

        $this->setRedirect(Route::_('index.php?option=com_example&view=flows', false));
    }
}
When I run the code I get "Model not found" message, so $this->getModel('Flows', 'Administrator'); is not finding the model. I don't understand why, maybe I'm fundamentally misunderstanding how this is supposed to work.

Any ideas very welcome :)

Advertisement
david0058
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 131
Joined: Wed Jan 20, 2016 10:58 pm

Re: Am I doing something stupid?

Post by david0058 » Wed Jan 01, 2025 12:41 pm

Yep, I was doing comething wrong ... of course :-[

The code above works fine, as expected: the problem was in the code I chopped out to simplify the post.

The FlowsController class contained this old code from J3.10:

Code: Select all

 public function __construct($config = array())
 {   
        parent::__construct($config);
 }
Without delving into the details to find out exactly why, I can imagine a host of reasons why this causes the MVC setup to go haywire. Deleted it, everything fine now.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17988
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Am I doing something stupid?

Post by toivo » Wed Jan 01, 2025 9:39 pm

david0058 wrote: Tue Dec 31, 2024 5:16 pmI thought I'd figured out how this namespacing works, but apparently not.
Nick Dionysopoulos, the developer of Akeeba Backup, published the following chapter about PHP namespaces in Joomla:
Basic concepts - Namespaces
Toivo Talikka, Global Moderator

david0058
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 131
Joined: Wed Jan 20, 2016 10:58 pm

Re: Am I doing something stupid?

Post by david0058 » Thu Jan 02, 2025 11:50 am

Thanks toivo, I had found Nick's fantastic book, which has saved me many hours of trying to work out how the new Joomla works. I'd even read the namespaces chapter a couple of times, but I think it's still sinking in ... not 100% confident that I get it all but I'm getting there slowly :D

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 25355
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Am I doing something stupid?

Post by pe7er » Thu Jan 02, 2025 11:54 am

david0058 wrote: Thu Jan 02, 2025 11:50 amI'd even read the namespaces chapter a couple of times, but I think it's still sinking in ... not 100% confident that I get it all but I'm getting there slowly :D
In case of namespace issues (like Class not found errors), it's best to delete the autoload file /administrator/cache/autoload_psr4.php
It will be recreated again with the next page refresh.
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

david0058
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 131
Joined: Wed Jan 20, 2016 10:58 pm

Re: Am I doing something stupid?

Post by david0058 » Sun Jan 05, 2025 10:45 am

Thanks Peter, good tip!

Advertisement

Post Reply

Return to “Joomla! 4.x Coding”