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]: undefined column with columnMap and model caching #16420

Closed
Deathamns opened this issue Sep 1, 2023 · 4 comments
Closed

[BUG]: undefined column with columnMap and model caching #16420

Deathamns opened this issue Sep 1, 2023 · 4 comments
Assignees
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: medium Medium

Comments

@Deathamns
Copy link
Contributor

Deathamns commented Sep 1, 2023

Describe the bug
modelsMetadata and columnMap don't work well together since v5.3.0. In v5.2.3 everything was fine.
The following script runs at first time, and its output should be 1.
After creating the models' cache, every following execution will result in an error:
Column 'user_id' doesn't belong to any of the selected models (1), when preparing: SELECT user_id FROM Users
Deleting the cache directory allows the script to run correctly once more.

Removing the $di->setShared('modelsMetadata', ...); part or not using columnMap "solves" the problem.
Possible culprit #16393.
Maybe related #16411, but I'm not sure.

To Reproduce

<?php

class Users extends \Phalcon\Mvc\Model {
	public $user_id;
	public $user_name;

	public function columnMap() {
		return [
			'acc_id' => 'user_id',
			'user_name' => 'user_name'
		];
	}
}

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

$di->setShared('db', function() {
	return new \Phalcon\Db\Adapter\Pdo\Mysql([
		'dbname'   => 'test',
		'username' => 'test',
		'password' => 'test'
	]);
});

$di->setShared('modelsMetadata', function() {
	is_dir('cache/models') || mkdir('cache/models', 0777, true);

	return new \Phalcon\Mvc\Model\Metadata\Stream([
		'metaDataDir' => './cache/models/'
	]);
});

try {
	$app = new \Phalcon\Mvc\Application($di);
	echo $app->modelsManager->executeQuery(
		'SELECT user_id FROM Users'
	)->getFirst()->user_id;
} catch ( Exception $ex ) {
	print_r($ex->getMessage());
}

Expected behavior
Print 1 every time the script is executed.

Details

  • Phalcon v5.3.0
  • PHP 8.1.22 (cli) (built: Aug 1 2023 17:14:28) (NTS Visual C++ 2019 x64)
  • MySQL 8.1.0 schema:
CREATE DATABASE IF NOT EXISTS `test`;
USE `test`;

CREATE TABLE IF NOT EXISTS `users` (
  `acc_id` int unsigned NOT NULL AUTO_INCREMENT,
  `user_name` varchar(15) COLLATE utf8mb4_0900_bin NOT NULL,
  PRIMARY KEY (`acc_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;

INSERT INTO `users` (`acc_id`, `user_name`) VALUES (1, 'test');
@Deathamns Deathamns added bug A bug report status: unverified Unverified labels Sep 1, 2023
@rudiservo
Copy link
Contributor

hi @Deathamns, I think I have detected the bug, trying to replicate it in the tests, a fix will be issued soon.

@rudiservo
Copy link
Contributor

@Deathamns just to confirm, are you using APCU or any other cache metadata?

@Deathamns
Copy link
Contributor Author

I had the apcu extension enabled in PHP, but the issue persists after I disable it.

rudiservo added a commit to rudiservo/cphalcon that referenced this issue Sep 1, 2023
niden added a commit that referenced this issue Sep 5, 2023
Fix #16420, fixed metadata get column from cache adapters, added tests
@niden
Copy link
Member

niden commented Sep 5, 2023

Resolved in #16421

Thank you @Deathamns and @rudiservo

@niden niden closed this as completed Sep 5, 2023
@niden niden added status: medium Medium 5.0 The issues we want to solve in the 5.0 release and removed status: unverified Unverified labels Sep 5, 2023
@niden niden added this to Phalcon v5 Sep 5, 2023
@niden niden moved this to Implemented in Phalcon v5 Sep 5, 2023
@niden niden moved this from Implemented to Released in Phalcon v5 Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: medium Medium
Projects
Status: Released
Development

No branches or pull requests

3 participants