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

CLI task fails when calling model method from library

Hi all,

I have an issue that I am not sure how to solve. I have created a cli.php and a task that will use existing models to perform operations (import in this case). The model calls a library I made on the "afterUpdate" event. This works fine if for example I use the GUI to edit a record and save it again. However, I want to do the same thing (edit specific records) using the cli task I did. Here, although the model works fine, the library fails with the following error:

Fatal error: Uncaught Error: Class 'Importer' not found in <path here...>/app/cli.php:68

In my cli.php I am using this:

$loader->registerNamespaces([
    'WebErp\Models'      => __DIR__ . '/models',
    'WebErpCLI\Models'   => __DIR__ . '/climodels',
    'WebErpCLI\Tasks'       => __DIR__ . '/tasks',
]);
$loader->registerDirs([
    __DIR__ . "/tasks",
    __DIR__ . "/models",
    __DIR__ . "/climodels",
]);

But when I try to add the folder and namespace of the Libary (located under /app/library/Importer/Importer.php) I end up with similar errors, even when I add a shared DI component like so:

$di->set('importer', function () {
    return new Importer();
});

I am pretty sure I am doing somthing wrong, but I can't figure out what it is....

You need to register this namespacer/dir with loader obviously.

I tried doing tis like so, but still nothing....

$loader->registerNamespaces([
    'WebErp\Models'      => __DIR__ . '/models',
    'WebErpCLI\Models'   => __DIR__ . '/climodels',
    'WebErpCLI\Tasks'       => __DIR__ . '/tasks',
    'WebErp\Importer'       => __DIR__ . '/library/Importer',
]);
$loader->registerDirs([
    __DIR__ . "/tasks",
    __DIR__ . "/models",
    __DIR__ . "/climodels",
    __DIR__ . "/library/Importer",
]);

Try outputing __DIR__ . '/library/Importer' just to make sure it's pointing to the correct directory.

edited Dec '17

You need to decide really. you use registerNamespaces or registerDirs, don't use both for same path.

This simply means that this class doesn't exist, like it wasn't register by loader. Nothing more nothing less.