Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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'



2.9k

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()'



2.9k
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();
    }
}


3.3k
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 !