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]: Column names starting with "not" #14641

Closed
Deathamns opened this issue Dec 22, 2019 · 9 comments
Closed

[BUG]: Column names starting with "not" #14641

Deathamns opened this issue Dec 22, 2019 · 9 comments
Assignees
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report external dependency This issue depends on external issue to be resolved. status: medium Medium

Comments

@Deathamns
Copy link
Contributor

Describe the bug
When a column name starts with the string "not", then Phalcon throws en error like this one:

Column 'ifications' doesn't belong to any of the selected models (1), when preparing: SELECT notifications FROM Objects

To Reproduce

<?php
class Objects extends \Phalcon\Mvc\Model
{
	public $obj_id;
	public $notifications;

	/*public function columnMap()	{
		return [
			'notifications' => 'x',
			// 'notifications' => 'notx',
		];
	}*/
}

$di = new \Phalcon\Di\FactoryDefault;

$di['db'] = new \Phalcon\Db\Adapter\Pdo\Mysql([
	'dbname'   => 'test',
	'username' => 'test',
	'password' => 'test',
]);

try {
	$app = new \Phalcon\Mvc\Application($di);
	echo $app->modelsManager->executeQuery(
		'SELECT notifications FROM Objects' // err
		// 'SELECT [notifications] FROM Objects' // ok with escaping
		// 'SELECT x FROM Objects' // ok with columnMap
		// 'SELECT x AS [notifications] FROM Objects' // ok with columnMap
		// 'SELECT x AS notifications FROM Objects' // err with columnMap
		// 'SELECT notx FROM Objects' // err with columnMap + "not" alias
		// 'SELECT [notx] FROM Objects' // ok with columnMap + "not" alias
	)->getFirst()->notifications;
} catch ( Exception $ex ) {
	print_r($ex->getMessage());
}

Expected behavior
To be able to access the column with its original name, without any workarounds (listed above).

Details

  • Phalcon version: 4.0.0, Dec 21 2019 18:42:18, Zephir Version 0.12.15-814db50
  • PHP Version: PHP 7.4.1 (cli) (built: Dec 17 2019 19:23:59) ( NTS Visual C++ 2017 x64 )
  • Database, table schema:
CREATE DATABASE IF NOT EXISTS `test`;
USE `test`;

CREATE TABLE `objects` (
	`obj_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`obj_name` VARCHAR(20) NOT NULL,
	`obj_type` TINYINT(3) UNSIGNED NOT NULL,
	`notifications` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
	PRIMARY KEY (`obj_id`)
) ENGINE=InnoDB ;

INSERT INTO `objects` (`obj_id`, `obj_name`, `obj_type`) VALUES (1, 'test1', 1);
@niden niden added bug A bug report status: unverified Unverified and removed Bug - Unverified labels Dec 23, 2019
@ruudboon
Copy link
Member

ruudboon commented Jan 2, 2020

Let's investigate this bug in the current sprint. If we can fix it easily in Zephir we do it in 4.0.x if work is required in the PHQL parser we move this to 4.1.x

@ruudboon ruudboon added the 4.0.1 label Jan 2, 2020
@ruudboon ruudboon removed the status: unverified Unverified label Jan 7, 2020
@ruudboon
Copy link
Member

ruudboon commented Jan 7, 2020

I can confirm that this issue is related to the PHQL parser.

$query = new \Phalcon\Mvc\Model\Query('SELECT notifications FROM Objects', $di);
$query->parse(); //error

Gonna move this to 4.1.

@catalinsim
Copy link

in given example, modifying to this should work: SELECT [notifications] FROM Objects. Just add square brackets around column name starting with 'not'

@SamAntUA
Copy link

SamAntUA commented Nov 15, 2021

The same problem with 'end' in a column name:

  • columns('end') leads to (surrounded with grave accents)
    Fatal error: Uncaught Phalcon\Mvc\Model\Exception: Scanning error before 'end` FROM...
  • columns('end') leads to
    Fatal error: Uncaught Phalcon\Mvc\Model\Exception: Syntax error, unexpected token END, near to ' FROM...

@yesworld
Copy link

Phalcon 5, the same problem. Any news?

@dfernandesbsolus
Copy link

Guys, any news about this problem?

@niden
Copy link
Sponsor Member

niden commented Jul 25, 2023

Can you try enclosing your field in [] i.e. [notifications].

I remember we talked about this years ago and the decision was made not to change the parser because it has to identify the not keyword so as to parse correct syntax. Changing that at the time was not only very time consuming but could prove impossible.

I believe there is a note in the docs about reserved words and fields and that one should enclose the field in [ ].

Can you try and let us know please?

@alex-bunt
Copy link

Hi @niden I can confirm tableName.[Notes] fixes our issue.

@niden
Copy link
Sponsor Member

niden commented Dec 27, 2023

I am going to close this issue. This is part of the parser and it is near impossible to fix because we cannot predict the names of fields in the database and distinguish those with the NOT keyword.

As per the docs, one can enclose their field in [] i.e. [notifications].

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 external dependency This issue depends on external issue to be resolved. status: medium Medium
Projects
Status: Unverified - Wont Fix - Duplicates
Development

No branches or pull requests