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 MetaData not refresh

I can't refresh the columns metadata into my model?


CREATE TABLE IF NOT EXISTS users' (
  'id' INT NOT NULL AUTO_INCREMENT,
  'username' VARCHAR(32) NOT NULL,
  'password' VARCHAR(128) NOT NULL,
  'email' VARCHAR(255) NOT NULL,
  'first_name' VARCHAR(128) NULL,
  'last_name' VARCHAR(128) NULL,
  'users_type_user_type_id' INT NOT NULL,
  'stores_store_id' INT NOT NULL,
  PRIMARY KEY ('user_id', 'users_type_user_type_id', 'stores_store_id'),
  UNIQUE INDEX 'username_UNIQUE' ('username' ASC),
  UNIQUE INDEX 'email_UNIQUE' ('email' ASC),
  INDEX 'fk_users_users_type1_idx' ('users_type_user_type_id' ASC),
  INDEX 'fk_users_stores1_idx' ('stores_store_id' ASC))
ENGINE = InnoDB

Log...

[Tue, 18 Feb 14 16:43:21 -0600][INFO] SELECT 'users'.'username', ..., 'users'.'company', ..., 'users'.'pre_register' FROM 'users' WHERE ... LIMIT 1

The fields 'company' and 'pre_register', not exist into my model now and I tryed with this:

# services.php
$di->set('modelsMetadata', function () {
    $adapter = new MetaDataAdapter();
    $adapter->reset();
    return $adapter;
});

or:

# my_model.php
class MyModel extends Model {
    public function initialize(){
        $this->useDynamicUpdate(true);
    }
}

and the result is:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.company' in 'field list'
#0 [internal function]: PDO->query('SELECT 'users'....')
#1 [internal function]: Phalcon\Db\Adapter\Pdo->query('SELECT 'users'....', NULL, NULL)
#2 [internal function]: Phalcon\Mvc\Model\Query->_executeSelect(Array, NULL, NULL)
#3 [internal function]: Phalcon\Mvc\Model\Query->execute(NULL, NULL)
#4 /var/www/html/mx_com_addicted/verify/app/library/Auth/Auth.php(37): Phalcon\Mvc\Model::findFirst('username = 'sys...')
#5 /var/www/html/mx_com_addicted/verify/app/controllers/AuthController.php(45): Addicted\Auth\Auth->signin(Array)
#6 [internal function]: Addicted\Controllers\AuthController->signinAction()
#7 [internal function]: Phalcon\Dispatcher->dispatch()
#8 /var/www/html/mx_com_addicted/verify/public/index.php(18): Phalcon\Mvc\Application->handle()
#9 {main}

Can you help me? Regards,

A!~

I not defined any adapter type, only use the following code and mysql engine:

config.php

return new Phalcon\Config(
    array(
        'database' => array(
            'adapter' => 'Mysql',
            'host' => 'localhost',
            'username' => 'root',
            'password' => 'root',
            'dbname' => 'db_name',
        ),
    )
);

services.php

$di->set('db', function () use ($config) {
    $adapter = new DbAdapter(
        array(
            'host' => $config->database->host,
            'username' => $config->database->username,
            'password' => $config->database->password,
            'dbname' => $config->database->dbname,
        )
    );
    return $adapter;
});

$di->set('modelsMetadata', function () {
    $adapter = new MetaDataAdapter();
    $adapter->reset();
    return $adapter;
});

Thanks for your answer. Regards, A!~



293

I have the same problem when change table field.

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'live.lpt_id' in 'field list'

finally I override base model find method to refresh cache like this

Mvc/Model.php


public static function find($parameters = null):ResultsetInterface
{
        try {
            return parent::find($parameters);
        } catch (\PDOException $e) {
            if ('42S22' ==$e->getCode()) {
                $di = Di::getDefault();
                $modelsMetadata = $di->getShared('modelsMetadata');
                $modelsMetadata->reset();

                return parent::find($parameters);
            } else {
                throw $e;
            }
        }
}