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

Models cant loaded in Multi Module

Hi,

Im using this sample: https://github.com/phalcon/mvc/tree/master/multiple

and i make a model file (Users.php) in models foldet in frontend.

Users.php:

<?php

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Email as EmailValidator;
use Phalcon\Mvc\Model\Validator\Uniqueness as UniquenessValidator;

class Users extends Model
{   
    public function getSource() {
        return "eb_users";
    }
}

and in the IndexController.php (in frontend):

<?php

namespace Multiple\Frontend\Controllers;

use Phalcon\Mvc\Controller;

class IndexController extends Controller
{

    public function indexAction()
    {
        $user   = Users::find();
    }
}

but i have a this error:

Fatal error: Class 'Multiple\Frontend\Controllers\Users' not found in /var/www/html/xxxx/multi/apps/frontend/controllers/IndexController.php on line 12

why ?

sry for bad type english.

you forgot to define the namespace of the model. Also is good practice to keep the model in a separate directory 'Models'



7.0k

you forgot to define the namespace of the model. Also is good practice to keep the model in a separate directory 'Models'

Thanks for reply

i add this namespace in Users.php:

namespace Multiple\Frontend\Models;

But it still does not work.

full code (Users.php) :

<?php

namespace Multiple\Frontend\Models;

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Email as EmailValidator;
use Phalcon\Mvc\Model\Validator\Uniqueness as UniquenessValidator;

class Users extends Model
{   
    public function getSource() {
        return "eb_users";
    }
}

you also moved your 'Users.php' file to 'models' directory? and then in controller you must call '$user = \Multiple\Frontend\Modules\Users()::find()'



7.0k
edited Jun '15

you also moved your 'Users.php' file to 'models' directory? and then in controller you must call '$user = \Multiple\Frontend\Modules\Users()::find()'

Yes, 'Users.php' is in the apps/frontend/models directory.

and i used below code, but doesn't work:


$user = \Multiple\Frontend\Modules\Users()::find();

and show thie error:

Parse error: syntax error, unexpected T_STRING, expecting T_NS_SEPARATOR in /var/www/html/xxx/multi/apps/frontend/controllers/IndexController.php on line 12

Full code of indexController.php :


<?php

namespace Multiple\Frontend\Controllers;

use Phalcon\Mvc\Controller;

class IndexController extends Controller
{

    public function indexAction()
    {
        $user = \Multiple\Frontend\Modules\Users()::find();
    }
}


4.6k
Accepted
answer
  • I use phalcon dev tools to generate model in own directory, Database cms in directory ../app/models/cms
  • In model files add namespace Cms; and
    public function initialize()
    {
        // By default "db" is choose for phalcon, you may specify other connection in your
        $this->setConnectionService("dbCms");
    }
  • index.php add
    $config = new Phalcon\Config\Adapter\Ini(__DIR__ . "/../app/config/config.ini");

    $di = new Phalcon\DI\FactoryDefault;
    $di->set('dbCms', function () use ($config) {
        return new Phalcon\Db\Adapter\Pdo\Mysql(
            [
                "host" => $config->dbCms->host,
                "username" => $config->dbCms->username,
                "password" => $config->dbCms->password,
                "dbname" => $config->dbCms->dbname,
                "options" => [
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
                    PDO::ATTR_CASE => PDO::CASE_LOWER,
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
                ],
            ]);
    });

    $loader = new Phalcon\Loader;
    $loader->registerNamespaces([
        'Cms' => $config->phalcon->modelsCmsDir,
    ]);

now your code is something like $myModell = new Cms\ExampleModell(); Hope this help.

edited Jun '15

I think you must add namespce to loader.

    $loader->registerNamespaces([
        'Multiple\Frontend\Controllers' => $config->phalcon->modelsFrontendControllersDir,
    ]);

you also moved your 'Users.php' file to 'models' directory? and then in controller you must call '$user = \Multiple\Frontend\Modules\Users()::find()'

Yes, 'Users.php' is in the apps/frontend/models directory.

and i used below code, but doesn't work:


$user = \Multiple\Frontend\Modules\Users()::find();

and show thie error:

Parse error: syntax error, unexpected T_STRING, expecting T_NS_SEPARATOR in /var/www/html/xxx/multi/apps/frontend/controllers/IndexController.php on line 12

Full code of indexController.php :


<?php

namespace Multiple\Frontend\Controllers;

use Phalcon\Mvc\Controller;

class IndexController extends Controller
{

  public function indexAction()
  {
      $user = \Multiple\Frontend\Modules\Users()::find();
  }
}

I also had a similar error !