Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fangface opened this issue Jan 13, 2014 · 5 comments
Closed

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

fangface opened this issue Jan 13, 2014 · 5 comments
Labels
bug A bug report status: medium Medium

Comments

@fangface
Copy link

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());
@ghost
Copy link

ghost commented Jan 13, 2014

Worked for me in 1.2.5:

Array
(
    [id] => 1
    [name] => Robotina
    [type] => mechanical
    [year] => 1972
)
Array
(
    [id] => 1
    [name] => Robotina
    [type] => mechanical
    [year] => 1972
)

However, could you please try this:

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

?

@fangface
Copy link
Author

I have tried with getShared() and that made no difference. I have also now tried both options using 1.2.5 and continue to get the same error.

Please note I am using the following to see the error;

error_reporting(E_ALL);
ini_set('display_errors', 1);

@ghost
Copy link

ghost commented Jan 27, 2014

Could you please show more code?

With this:

<?php

error_reporting(E_ALL | E_STRICT);

require __DIR__ . '/models/Robots.php';

$di = new Phalcon\DI();

$di->set('modelsManager', function(){
        return new Phalcon\Mvc\Model\Manager();
});

$di->set('modelsMetadata', function(){
        return new Phalcon\Mvc\Model\Metadata\Memory();
});

$di->set('db', function(){
        require __DIR__ . '/config.db.php';
        return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
});

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

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

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

I am unable to reproduce the issue.

@fangface
Copy link
Author

Ok that works by itself for me as well, so after digging deeper here is how to alter it slightly to trigger the notice.

<?php

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', '1');

Phalcon\Mvc\Model::setup(array(
    'columnRenaming' => false,
));

require __DIR__ . '/models/Robots.php';

$di = new Phalcon\DI\FactoryDefault();

$di->set('db', function(){
        require __DIR__ . '/config.db.php';
        return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
});

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

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

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

Please note the use of BOTH of the following is required to trigger the notice;

$di = new Phalcon\DI\FactoryDefault();

and;

Phalcon\Mvc\Model::setup(array(
    'columnRenaming' => false,
));

@ghost
Copy link

ghost commented Feb 1, 2014

Fix submitted (#1952)

@phalcon phalcon closed this as completed Mar 6, 2014
@niden niden added bug A bug report status: medium Medium and removed Bug - Medium labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
None yet
Development

No branches or pull requests

2 participants