Skip to content

[BUG] PHP Error after \Phalcon\Mvc\Model\MetaData::reset() #1801

Closed
@fangface

Description

@fangface

Using Phalcon 1.2.4 I am getting PHP errors after making use of the MetaData::reset() call to force metadata to be reloaded.

$connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
    'host' => 'localhost',
    'username' => 'user',
    'password' => 'password',
    'dbname' => 'test_company_1',
));

$di->set('companyDb', $connection, true);

$robot = \Models\Robots::findFirst(1);
print_r($robot->toArray());

The above works fine and the current robot is displayed

Now if I need to connect to another database (for the sake of argument lets say there are thousands of databases to iterate through, so setting up a seperate $di service and seperate models for each is not practical so I want to reuse the the same models and so the same connection service and table names

$connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
    'host' => 'localhost',
    'username' => 'user',
    'password' => 'password',
    'dbname' => 'test_company_2',
));

$di->set('companyDb', $connection, true);

The table structures could be slightly out of sync with new fields etc so I need to ensure the corrent metadata is loaded again

$di->get('modelsMetadata')->reset();

Now when I run the following it works and the new structure is displayed.

$robot = \Models\Robots::findFirst(1);
print_r($robot->toArray());

But both the findFirst() and toArray() methods now generate a PHP error;
Notice: Undefined index: models\robots in ...

Please note that within the Robots.php class file I make use of the following to ensure that any changes to the connection service are picked up

public function getReadConnection()
{
    return $this->getDI()->get($this->getReadConnectionService());
}

I can also generate the same PHP error without changing database connections if the Metadata reset option is used between method calls.

$robot = \Models\Robots::findFirst(1);
print_r($robot->toArray());

$di->get('modelsMetadata')->reset();

$robot = \Models\Robots::findFirst(1);
print_r($robot->toArray());

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions