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 'o_obj_id' doesn't make part of the column map #14488

Closed
Deathamns opened this issue Oct 26, 2019 · 4 comments
Closed

[BUG]: Column 'o_obj_id' doesn't make part of the column map #14488

Deathamns opened this issue Oct 26, 2019 · 4 comments
Assignees
Labels
bug A bug report status: low Low

Comments

@Deathamns
Copy link
Contributor

Describe the bug
The mentioned error from the title occurs when selecting a column that also has the table/model name before it (an alias or just the full model name).
This is a problem since Phalcon v4.0.0-alpha1, v3.4.4 works fine.
The value of phalcon.orm.column_renaming global doesn't seem to have any effect.
Maybe related: issue / change.

To Reproduce

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

$di = new \Phalcon\Di\FactoryDefault;

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

try {
    /*\Phalcon\Mvc\Model::setup([
        'columnRenaming' => false,
        'castOnHydrate' => false
    ]);*/

    $app = new \Phalcon\Mvc\Application($di);
    echo $app->modelsManager->executeQuery(
        'SELECT o.obj_id FROM Objects AS o LIMIT 1' // err
        // Objects.obj_id // err
        // o.obj_id AS obj_id // ok
        // obj_id // ok
        // o.* // ok
    )->getFirst()->obj_id;
} catch ( Exception $ex ) {
    print_r($ex->getMessage());
}

Expected behavior
To be able to run the query without errors (and access the columns by their own names preferably).

Details

  • Phalcon version: 4.0.0-rc.1, Sep 23 2019 05:35:18, Zephir Version 0.12.4-b386980
  • PHP Version: PHP 7.3.11 (cli) (built: Oct 22 2019 11:20:10) ( NTS MSVC15 (Visual C++ 2017) x64 )
  • Operating System: Win 10 x64
  • MySQL: 5.7.28
  • Server: Nginx
  • Other related info (Database, table schema):
CREATE DATABASE IF NOT EXISTS `test`;
USE `test`;

CREATE TABLE IF NOT EXISTS `objects` (
 `obj_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `obj_name` varchar(20) NOT NULL,
 `obj_type` tinyint(3) unsigned NOT NULL,
 PRIMARY KEY (`obj_id`)
) ENGINE=InnoDB;

INSERT INTO `objects` (`obj_id`, `obj_name`, `obj_type`) VALUES (1, 'test1', 1);
@niden
Copy link
Member

niden commented Nov 21, 2019

I have verified this with a test.

@niden
Copy link
Member

niden commented Dec 1, 2019

Side effect from fixing #13552 .

@niden
Copy link
Member

niden commented Dec 1, 2019

Resolved in #14566

Thank you @Deathamns

@Deathamns
Copy link
Contributor Author

These column aliasing bugs are haunting me...

Phalcon v4.0.3.

<?php
class Cities extends \Phalcon\Mvc\Model
{
	public $city_id;
	public $municipality;
	public $city_name;
}

$di = new \Phalcon\Di\FactoryDefault;

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

(new \Phalcon\Mvc\Model\Query\Builder([
	'models' => ['c' => Cities::class],
	'columns' => ['c.city_id', 'c.city_name', 'd.city_name'],
	'order' => 'IFNULL(d.city_name, c.city_name), c.city_name',
	'joins' => [[Cities::class, 'c.municipality = d.city_id', 'd', 'LEFT']]
]))->getQuery()->execute()->getFirst();
CREATE DATABASE IF NOT EXISTS `test`;
USE `test`;
CREATE TABLE `cities` (
	`city_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`municipality` INT(10) UNSIGNED NULL,
	`city_name` VARCHAR(50) NOT NULL COLLATE 'utf8mb4_unicode_ci',
	PRIMARY KEY (`city_id`) USING BTREE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;

Giving an alias to d.city_name, like d.city_name AS mun, silences the error. But I already faced this issue, so I knew where to look.
This might be very well related to #14657.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: low Low
Projects
None yet
Development

No branches or pull requests

2 participants