V
V
Vanik Khachatryan2018-05-16 17:34:38
Phalcon
Vanik Khachatryan, 2018-05-16 17:34:38

Why can't Micro see the models?

There is an action in which I am trying to pull all the records from the table:

public function getAccounts() {
        $accounts = Accounts::find();

        return $this->response->setJsonContent(['accounts' => $accounts])->send();
    }

The PostMan issues
<br />
<b>Warning</b>:  class 'AdminController' does not have a method 'Accounts' in
<b>C:\OSPanel\domains\fisher.loc\public\index.php</b> on line
<b>20</b>
<br />

Although the directories are specified in the loader.
This is what the model looks like
<?
use Phalcon\Mvc\Model;

class Accounts extends Model {
    public $id;

    public $login;

    public $password;

}

What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Terekhin, 2018-05-17
@VaniXac

In the loader, you need to register the namespace and the directory of the models

$loader = new \Phalcon\Loader();

$loader->registerNamespaces(
    [
        'App\\Models' => __DIR__ . '/models/',
    ]
);

$loader->registerDirs(
    [
        ...
         __DIR__ . '/models/',
        ...
    ]
)->register();

namespace App\Models;

class Accounts extends \Phalcon\Mvc\Model {
    public $id;

    public $login;

    public $password;

    public $steam_guard;

    public $email;

    public $worker_id;
}

use App\Models\Accounts;

class AdminController extends \Phalcon\Mvc\Controller {
    public function getAccounts() {
        $accounts = Accounts::find();
        
        return $this->response->setJsonContent(['accounts' => $accounts])->send();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question