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

Created on 26 Oct 2019  路  4Comments  路  Source: phalcon/cphalcon

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):

``sql CREATE DATABASE IF NOT EXISTStest; USEtest`;

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);
```

bug low

All 4 comments

I have verified this with a test.

Side effect from fixing #13552 .

Resolved in https://github.com/phalcon/cphalcon/pull/14566

Thank you @Deathamns

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.

Was this page helpful?
0 / 5 - 0 ratings