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 not found

Hello! phalcon resumed his study. please help to understand:

File config:

...
'modelsDir'      => __DIR__ . '/../models/', 
'viewsDir'       => __DIR__ . '/../views/',
'componentsDir'  => __DIR__ . '/../components/',
'servicesDir'    => __DIR__ . '/../services/',
...

File loader:

...
$loader->registerDirs(
    [
        $config->phalcon->controllersDir,
        $config->phalcon->modelsDir,
    ]
);

$loader->registerNamespaces(
    [
        'App\Components' => $config->phalcon->componentsDir,
        'App\Services' => $config->phalcon->servicesDir,
        'App\Cli' => $config->phalcon->cliDir
    ]
);
...

file services\Contacts.php :

namespace App\Services;

use App\Components\SalesapCrm;

class Contacts
{
    protected $contact;

    public function __construct(array $contact){

        $this->contact = $contact;
        return $this->checkContact();
    }

    private function checkContact(){

        $ContactsModel = \Phalcon\Mvc\Model\Contacts::findFirst('email = "[email protected]"');

            return $ContactsModel->getId();
    }

}

I get the error:

Uncaught Error: Class 'Phalcon\Mvc\Model\Contacts' not found

I do not understand why can not find a model?

This line is referencing the wrong namespace:

$ContactsModel = \Phalcon\Mvc\Model\Contacts::findFirst('email = "[email protected]"');

You've defined the namespace as App\Services, so it should read:

$ContactsModel = \App\Services\Contacts::findFirst('email = "[email protected]"');

Actually, since you're referencing the class from within itself, you could bypass classes & namespaces altogether:

$ContactsModel = self::findFirst('email = "[email protected]"');


4.3k

Your option is causing the error: Uncaught Error: Call to undefined method App\Services\Contacts::findFirst()

This line is referencing the wrong namespace:

$ContactsModel = \Phalcon\Mvc\Model\Contacts::findFirst('email = "[email protected]"');

You've defined the namespace as App\Services, so it should read:

$ContactsModel = \App\Services\Contacts::findFirst('email = "[email protected]"');

Actually, since you're referencing the class from within itself, you could bypass classes & namespaces altogether:

$ContactsModel = self::findFirst('email = "[email protected]"');


4.3k
edited Jun '20

For example, changed the code

File config:

$loader->registerDirs(
    [
        $config->phalcon->controllersDir,
        // $config->phalcon->modelsDir,
    ]
);

$loader->registerNamespaces(
    [
        'App\Components' => $config->phalcon->componentsDir,
        'App\Services' => $config->phalcon->servicesDir,
        'App\Cli' => $config->phalcon->cliDir,
        'App\Models' => $config->phalcon->modelsDir
    ]
);

File: services/Contacts.php

namespace App\Services;

use App\Models\Orders AS ordersModel;
use App\Components\SalesapCrm;

class Contacts
{
    protected $contact;

    public function __construct(array $contact){

        $this->contact = $contact;
        return $this->checkContact();
    }

    private function checkContact(){

        $OrdersModel = ordersModel::findFirst('id = 1');

            return $OrdersModel->getId();
    }

}

File models/Orders.php:

use Phalcon\Mvc\Model;

class Orders extends Model
{
    public
        $id,
        $contact_id,
        $form_id,
        $product_name,
        $product_id,
        $form_name,
        $order_id;

    public function getId(){
        return $this->id;
    }

I get the error: Uncaught Error: Class 'App\Models\Orders' not found

I do not understand why can not find a model class?



125.7k
Accepted
answer

Your Orders model is not in the correct namespace. In contacts.php you're referencing \App\Models\Orders, but you haven't declared that namespace in Orders.php

As a sidenote, you should probably register everything with registerNamespaces() as that method is much better in terms of performance that registerDirs()