We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model 'Menu' could not be loaded

I have this relationship in MySQL:

My problem is when i want to select menu items, from menu table, depends on user rules in rule table with specific user id, error comes: Model 'Menu' could not be loaded

This is my code:

class Users extends Model
{
    public $id,$name,$username,$password,$active;
    public function initialize()
    {
        $this->setSource('bee_sysuser');
        $this->hasMany('id','Rules','id_user',['alias'=>'user_rules']);
        $this->hasManyToMany(
            'id',
            'Rules',
            'id_user',
            'id_menu',
            'Menu',
            'id',
            array('alias' => 'menus')
        );
    }
}
class Menu extends Model
{
    public $id,$name,$parent,$lft,$rgt,$link,$sort,$level,$islink,$disabled,$id_admin,$admin_only;
    public function initialize()
    {
        $this->setSource('bee_menu');

        $this->hasMany('id','Rules','id_model',['alias'=>'user_rules']);
        $this->hasManyToMany(
            'id',
            'Rules',
            'id_menu',
            'id_user',
            'User',
            'id',
            array('alias' => 'users')
        );
    }
}
class Rules extends Model
{
    public $id_user,$id_menu,$type;
    public function initialize()
    {
        $this->setSource('bee_settings_rules');

        $this->belongsTo('id_user', 'Users', 'id',
            array('alias' => 'users'));
        $this->belongsTo('id_menu', 'Menu', 'id',
            array('alias' => 'menus'));
    }
}

There im trying to call it:

$user = Users::findFirst(
                [
                    'id = :id:', 'bind' => ['id' => 1]
                ]
            );
            \Tracy\Debugger::barDump($user->menus);

$user is exists with data from Users model, but im not able to access to Menu model, as i mentioned before.

Any help would be appreciate

Thanks, and sorry about my english.



85.5k

i do not see the error ? but if it says that a model cannot be loaded its a loder configuration problem.

This is my loader.php

use Phalcon\Loader;

$loader = new Loader();

/**
 * Register Namespaces
 */
$loader->registerNamespaces([
    'MyCMS\Models' => APP_PATH . '/common/models/',
    'MyCMS'        => APP_PATH . '/common/library/',
    'MyCMS\Library'        => APP_PATH . '/library/',
]);

/**
 * Register module classes
 */
$loader->registerClasses([
    'MyCMS\Modules\Frontend\Module' => APP_PATH . '/modules/frontend/Module.php',
    'MyCMS\Modules\Admin\Module' => APP_PATH . '/modules/backend/Module.php',
    'MyCMS\Modules\Cli\Module'      => APP_PATH . '/modules/cli/Module.php',
]);
$loader->registerDirs(array(
    __DIR__ . '/models/'
))->register();

$loader->register();

And my backend Module.php:

class Module implements ModuleDefinitionInterface
{
    /**
     * Registers an autoloader related to the module
     *
     * @param DiInterface $di
     */
    public function registerAutoloaders(DiInterface $di = null)
    {
        $loader = new Loader();

        $loader->registerNamespaces([
            'MyCMS\Modules\Admin\Controllers' => __DIR__ . '/controllers/',
            'MyCMS\Modules\Admin\Models'      => __DIR__ . '/models/',
            'MyCMS\Modules\Admin\Forms'      => __DIR__ . '/forms/'
        ]);

        $loader->register();
    }

    /**
     * Registers services related to the module
     *
     * @param DiInterface $di
     */
    public function registerServices(DiInterface $di)
    {
        /**
         * Setting up the view component
         */
        $di->set('view', function () {
            $view = new View();
            $view->setDI($this);
            $view->setViewsDir(__DIR__ . '/views/');

            $view->registerEngines([
                '.volt'  => 'voltShared',
                '.phtml' => PhpEngine::class
            ]);
            return $view;
        });
        $di->set(
            'dispatcher',
            function() use ($di) {

                $evManager = $di->getShared('eventsManager');

                $evManager->attach(
                    "dispatch:beforeException",
                    function($event, $dispatcher, $exception)
                    {
                        if ($dispatcher->getControllerName() != 'error') {
                            switch ($exception->getCode()) {
                                case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                                case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                                    $dispatcher->forward(
                                        array(
                                            'controller' => 'error',
                                            'action' => 'show404',
                                        )
                                    );
                                    return false;
                                    break;
                                default:
                                    $dispatcher->forward(
                                        array(
                                            'controller' => 'error',
                                            'action' => 'show500',
                                        )
                                    );
                                    return false;
                                    break;
                            }
                        }
                    }
                );
                $dispatcher = new Dispatcher();
                $dispatcher->setDefaultNamespace("MyCMS\Modules\Admin\Controllers");
//                $dispatcher->setEventsManager($evManager);
                return $dispatcher;
            },
            true
        );
    }
}

Maybe I missed something.



85.5k
class Menu extends Model

again i am not sure what error you are getting. But seems like this this "Model" class cannot be loaded

Ok, Apache gives me this error:

AH01071: Got error 'PHP message: PHP Fatal error: Uncaught Phalcon\Mvc\Model\Exception: Model 'Menu' could not be loaded in /var/www/mycms/app/modules/backend/controllers/ControllerBase.php:43\nStack trace:\n#0 [internal function]: Phalcon\Mvc\Model\Manager->load('Menu', true)\n#1 [internal function]: Phalcon\Mvc\Model\Query->_prepareSelect()\n#2 [internal function]: Phalcon\Mvc\Model\Query->parse()\n#3 [internal function]: Phalcon\Mvc\Model\Query->execute()\n#4 [internal function]: Phalcon\Mvc\Model\Manager->getRelationRecords(Object(Phalcon\Mvc\Model\Relation), NULL, Object(MyCMS\Modules\Admin\Models\Users), NULL)\n#5 /var/www/mycms/app/modules/backend/controllers/ControllerBase.php(43): Phalcon\Mvc\Model->__get('menus')\n#6 /var/www/mycms/app/modules/backend/controllers/UserController.php(14): MyCMS\Modules\Admin\Controllers\ControllerBase->initialize()\n#7 [internal function]: MyCMS\Modules\Admin\Controllers\UserController->initialize()\n#8 [internal function]: Phalcon\Dispatcher->dispatch()\n#9 /var/www/mycms...\n', referer: https://mysite.com/administrator/



10.1k
edited Jul '19

Is the model namespace included?

use Phalcon\Mvc\Model;

Sure

namespace MyCMS\Modules\Admin\Models;

use Phalcon\Mvc\Model;

class Menu extends Model
{
    public $id,$name,$parent,$lft,$rgt,$link,$sort,$level,$islink,$disabled,$id_admin,$admin_only;
    public function initialize()
    {
        $this->setSource('bee_menu');

        $this->hasMany('id','Rules','id_model',['alias'=>'user_rules']);
        $this->hasManyToMany(
            'id',
            'Rules',
            'id_menu',
            'id_user',
            'User',
            'id',
            array('alias' => 'users')
        );
    }
}


10.1k
Accepted
answer

I'm wondering if it goes wrong on the relation setup. Could you try to specify the full namespace MyCMS\Modules\Admin\Models\Menu instead of just Menu

Wonderfull!!

I change $this->hasMany('id','Rules','id_model',['alias'=>'user_rules']); to $this->hasMany('id',Rules::class,'id_model',['alias'=>'user_rules']); and

$this->hasManyToMany(
            'id',
            'Rules',
            'id_menu',
            'id_user',
            'User',
            'id',
            array('alias' => 'users')
        );

to

 $this->hasManyToMany(
            'id',
            Rules::class,
            'id_menu',
            'id_user',
            Users::class,
            'id',
            array('alias' => 'users')
        );

And in other models as well. (Users.php, Rules.php) Now it everything works.

Thanks a lot for help :)