Model Auto-Loading Help.

Permalink
Howdy, again. Still on my quest of migrating from 5.6 to 5.7.

I've got my Controller loading, thanks to goldhat.
Now, I'm running into an issue with the auto-loading of my Model.

Model is located at:
/packages/PACKAGE_NAME/models/product_model.php

It's never being loaded. I've got the following at the top, which is never executed.
<?php 
namespace Concrete\Package\PACKAGE_NAME\Models;
echo "LOADING!!!"; die();


In my controller, I've got:
<?php
namespace Concrete\Package\PACKAGE_NAME\Controller\SinglePage\Dashboard;
use \Concrete\Core\Page\Controller\DashboardPageController;
use Loader;
use \Concrete\Package\PACKAGE_NAME\Models\ProductModel;
class ProductManager extends DashboardPageController {
    public function view() {
       $productManager = new ProductModel();


When executed, the following exception is thrown:
Class 'Concrete\Package\PACKAGE_NAME\Models\ProductModel' not found

in reference to:
$productManager = new ProductModel();


Where should this script be located / be named to get it to auto-load?

meandmybadself
 
goldhat replied on at Permalink Reply
The error message is correct - your model file is being autoloaded, and the class does not exist because there is raw output instead of a valid class definition.

A different way to test - create the class, put your test in constructor, then you should see the the constructor run proving the file is autoloaded, the class is found, and it's a valid class constructor.

<?php
namespace Concrete\Package\PACKAGE_NAME\Models;
class ProductModel {
  public function __construct() {
    echo "LOADING PRODUCT MODEL!!!"; 
    die();
  }
}


One part I don't know is why the die() statement wouldn't run anyway, but it seems like in the process of autoloading maybe PHP tests for errors first before actually running anything, so it ends up finding the missing class first before it runs the die() statement.
meandmybadself replied on at Permalink Reply
meandmybadself
I didn't have any luck w/ moving the bomb into the constructor.

Some help from Korvin on IRC yielded a solution that works, manually registering the load path for the class. Also, for some reason, this only works when I camelcase the name of my file (ProductModel.php v. product_model.php).

$loader = new Psr4ClassLoader();
$loader->addPrefix(NAMESPACE_SEGMENT_VENDOR . '\\Package\\PACKAGE_NAME\\Models\\', DIR_PACKAGES . '/PACKAGE_NAME/models/');
$loader->register();
$this->_model = new ProductModel();


Reference link:https://github.com/Buttress/addon_composer_sample/blob/master/packag...

Not the prettiest solution, but in the event that someone else runs into the same issue, I hope it's helpful.
I'll circle back if I can get it to work in the intended way.