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

Help my config database - Webserver Nginx + mariaDB

I get errors in the log file, I try to fix but no results. You see the details file log and help me solution Thanks

Phalcon\Mvc\Model\Exception: Table 'phalcon'.'users' doesn't exist in database when dumping meta-data for Server\Models\users File=/var/www/domain.com/public_html/app/library/Auth/Auth.php<br>Line=24 #0 [internal function]: Phalcon\Mvc\Model\MetaData\Strategy\Introspection->getMetaData(Object(Server\Models\users), Object(Phalcon\Di\FactoryDefault))
#1 [internal function]: Phalcon\Mvc\Model\MetaData->_initialize(Object(Server\Models\users), 'Server\\models\\aus...', 'users', 'phalcon')
#2 [internal function]: Phalcon\Mvc\Model\MetaData->readMetaDataIndex(Object(Server\Models\users), 4)
#3 [internal function]: Phalcon\Mvc\Model\MetaData->getDataTypes(Object(Server\Models\users))
#4 [internal function]: Phalcon\Mvc\Model::_invokeFinder('findFirstByEmai...', Array)
#5 /var/www/domain.com/public_html/app/library/Auth/Auth.php(24): Phalcon\Mvc\Model::__callStatic('findFirstByEmai...', Array)
#6 /var/www/domain.com/public_html/app/library/Auth/Auth.php(24): Server\Models\users::findFirstByEmail('[email protected]')
#7 /var/www/domain.com/public_html/app/controllers/LoginController.php(76): Server\Auth\Auth->check(Array, '')
#8 [internal function]: Server\Controllers\LoginController->indexAction()
#9 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(Server\Controllers\LoginController), 'indexAction', Array)
#10 [internal function]: Phalcon\Dispatcher->_dispatch()
#11 [internal function]: Phalcon\Dispatcher->dispatch()
#12 /var/www/domain.com/public_html/public/index.php(42): Phalcon\Mvc\Application->handle()
#13 {main}

What does your config and services file look like?

Your code is looking for a table users in a database named phalcon, which doesnt exist. Your connection should establish, since that would produce a different error message.



3.0k

file config:

  use Phalcon\Config;
  use Phalcon\Logger;

  defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
  defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');
  $hostname = $_SERVER['HTTP_HOST'];
  @date_default_timezone_set('Asia/Ho_Chi_Minh');
  return new Config([
      'database' => [
          'adapter'     => 'Mysql',
          'host'        => 'localhost',
          'username'    => 'username',
          'password'    => 'parssword',
          'dbname'      => 'dataname',
          'charset'     => 'utf8',
      ],
      'application' => [
          'appDir'         => APP_PATH . '/',
          'controllersDir' => APP_PATH . '/controllers/',
          'modelsDir'      => APP_PATH . '/models/',
          'formsDir'       => APP_PATH . '/forms/',
          'viewsDir'       => APP_PATH . '/views/',        
          'migrationsDir'  => APP_PATH . '/migrations/',
          'pluginsDir'     => APP_PATH . '/plugins/',
          'libraryDir'     => APP_PATH . '/library/',
          'cacheDir'       => BASE_PATH . '/cache/',
          'baseUri'        => 'https://'.$hostname.'/',
          'publicUrl'      => 'https://'.$hostname.'',
          'cryptSalt'      => 'eEAfR|_&G&f,+vU]:jFr!!A&+71w1Ms9~8_4L!<@[[email protected]_2My|:+.u>/6m,$D'
          // This allows the baseUri to be understand project paths that are not in the root directory
          // of the webpspace.  This will break if the public/index.php entry point is moved or
          // possibly if the web server rewrite rules are changed. This can also be set to a static path.
         // 'baseUri'        => preg_replace('/public([\/\\\\])index.php$/', '', $_SERVER["PHP_SELF"]),
      ],
      'logger' => [
          'path'     => APP_PATH . '/logs/',
          'format'   => '%date% [%type%] %message%',
          'date'     => 'D j H:i:s',
          'logLevel' => Logger::DEBUG,
          'filename' => 'application.log',
      ],
      'mail' => [       
          'fromName' => 'name',
          'fromEmail' => '[email protected]',
          'smtp' =>   [
                      'server' => 'smtp.gmail.com',
                      'port' => 587,
                      'security' => 'tls',
                      'username' => 'name',
                      'password' => 'pass'
                      ]
      ],
      // Set to false to disable sending emails (for use in test environment)
      'useMail' => true
  ]);

file service:

    /**
     * The URL component is used to generate all kind of urls in the application
     */
    $di->setShared('url', function () {
        $config = $this->getConfig();

        $url = new UrlResolver();
        $url->setBaseUri($config->application->baseUri);

        return $url;
    });

    /**
     * Setting up the view component
     */
    $di->set('view', function () {
        $config = $this->getConfig();

        $view = new View();

        $view->setViewsDir($config->application->viewsDir);

        $view->registerEngines([
            '.volt' => function ($view) {
                $config = $this->getConfig();

                $volt = new VoltEngine($view, $this);

                $volt->setOptions([
                    'compiledPath' => $config->application->cacheDir . 'volt/',
                    'compiledSeparator' => '_'
                ]);

                $volt->getCompiler()->addFunction(
                    'keywrods',
                    function($key)
                    {
                        return str_replace(" ", ", ", $key);
                        //return preg_replace(" ", ",", $key); 
                    });
                $volt->getCompiler()->addFunction(
                    'Enjavascript',
                    function($key)
                    {
                     return '<script type="text/javascript"> /<![CDATA[/' . $key . '/]]>/</script>';
                    });

                return $volt;
            }
        ]);

        return $view;
    }, true);

    /*
    $di->setShared('view', function () {
        $config = $this->getConfig();

        $view = new View();
        $view->setDI($this);
        $view->setViewsDir($config->application->viewsDir);

        $view->registerEngines([
            '.volt' => function ($view) {
                $config = $this->getConfig();

                $volt = new VoltEngine($view, $this);

                $volt->setOptions([
                    'compiledPath' => $config->application->cacheDir,
                    'compiledSeparator' => '_'
                ]);

                return $volt;
            },
            '.phtml' => PhpEngine::class

        ]);

        return $view;
    });*/

    /**
     * Database connection is created based in the parameters defined in the configuration file

    $di->setShared('db', function () {
        $config = $this->getConfig();

        $class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
        $params = [
            'host'     => $config->database->host,
            'username' => $config->database->username,
            'password' => $config->database->password,
            'dbname'   => $config->database->dbname,
            'charset'  => $config->database->charset
        ];

        if ($config->database->adapter == 'Postgresql') {
            unset($params['charset']);
        }

        $connection = new $class($params);

        return $connection;
    });*/

    $di->set('db', function () {
        $config = $this->getConfig();
        return new DbAdapter([
            'host' => $config->database->host,
            'username' => $config->database->username,
            'password' => $config->database->password,
            'dbname' => $config->database->dbname,
            'charset'  => $config->database->charset
        ]);
    });

    $di->set('mail', function () {
        return new Mail();
    });

    $di->set('modelsMetadata', function ()
    {
        $config = $this->getConfig();
        return new MetaDataAdapter([
            'metaDataDir' => $config->application->cacheDir . 'metaData/',
            "lifetime" => 60,
        ]);
        if ($config->debug)
        {
            $cache->flush();
        }
    });

    $di->set('session', function () {
        session_save_path('/home/www/mxt.vn/session');
        $session = new SessionAdapter();
        $session->start();
        return $session;
    });

    /**
     * Crypt service
     */
    $di->set('crypt', function () {
        $config = $this->getConfig();

        $crypt = new Crypt();
        $crypt->setKey($config->application->cryptSalt);
        return $crypt;
    });

    /**
     * Dispatcher use a default namespace
     */
            $di->set('dispatcher', function () {
                //Create/Get an EventManager
                $eventsManager = new \Phalcon\Events\Manager();
                //Attach a listener
                $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) {
                    //controller or action doesn't exist
                    if ($event->getType() == 'beforeException') {
                        switch ($exception->getCode()) {
                            case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                            case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                                $dispatcher->forward(array(
                                    'controller' => 'index',
                                    'action' => 'notFound'
                                ));

                                return false;
                        }
                    }
                });

                $dispatcher = new \Phalcon\Mvc\Dispatcher();
                //Set default namespace to backend module
                $dispatcher->setDefaultNamespace("iChi\Controllers");
                //Bind the EventsManager to the dispatcher
                $dispatcher->setEventsManager($eventsManager);

                return $dispatcher;
            });

    $di->set('auth', function () {
        return new Auth();
    });

    /**
     * Register the session flash service with the Twitter Bootstrap classes
     */
    $di->set('flash', function () {
        return new Flash([
            'error'   => 'alert alert-danger',
            'success' => 'alert alert-success',
            'notice'  => 'alert alert-info',
            'warning' => 'alert alert-warning'
        ]);
    });

    $di->set("cookies", function ()
    {
            $cookies = new Cookies();
            $cookies->useEncryption(false);
            return $cookies;}
    );

    $di->setShared('cache', function ()
     {
        $config = $this->getConfig();   
        $frontCache = new \Phalcon\Cache\Frontend\Data(array('lifetime' => 60)); //900
        $cache = new Phalcon\Cache\Backend\File($frontCache, array("cacheDir" => $config->application->cacheDir."caches/"));
        return $cache;
    });

    $di->setShared('modelsCache', function () 
    {
        $config = $this->getConfig();    
        //Create a Data frontend and set a default lifetime to 1 hour
        $frontCache = new \Phalcon\Cache\Frontend\Data(array('lifetime' => 60)); //900
        //Create the cache passing the connection
        $cache = new Phalcon\Cache\Backend\File($frontCache, array("cacheDir" => $config->application->cacheDir."caches/"));
        return $cache;
    }); 

The table name is 'ausers', I do not know where the phalcon is

Help my thanks!

edited Aug '17

Your service file seems OK, I think its simply a misconfiguration on your part.

First you must understand the error message: its complaining that you are querying against a table users in a database phalcon, and it cannot find it.


  return new Config([
      'database' => [
          'adapter'     => 'Mysql',
          'host'        => 'localhost',
          'username'    => 'username',
          'password'    => 'parssword',
          'dbname'      => 'dataname', // phalcon in your error log
          'charset'     => 'utf8',
      ],
  }

dbname should match with the database you've created within MariaDB. In case you're in doubt, you should install phpmyadmin on your box, so you can view your db structure in a nice GUI



3.0k
edited Aug '17

It worked well!

in model:

      public function initialize()
      {
          $this->setSchema("phalcon"); /change my database name
      }

How can I acheive this with phalcon?

Im trying to acheive somelike how how Doctrine and Eloquent handle joins, Where the joined table is a child container (resultset in phalcons case) instead of multiple of the same user with diffrent joined servers. google street view maps