[Bug] Error usage option `orm.cast_on_hydrate` and hydrate mode - ARRAY/OBJECTS #12191

Closed
andrew-demb opened this Issue Sep 1, 2016 · 1 comment

Projects

None yet

2 participants

@andrew-demb

Description

Error when Phalcon\Mvc\Model::cloneResultMapHydrate() create result array.

Option orm.castOnHydrate = true change generated column map structure in
Phalcon\Mvc\Model\Query::_executeSelect() - (https://github.com/phalcon/cphalcon/blob/v3.0.1/phalcon/mvc/model/query.zep#L2781), but Phalcon\Mvc\Model::cloneResultMapHydrate() expects to string values in column map (https://github.com/phalcon/cphalcon/blob/v3.0.1/phalcon/mvc/model.zep#L653)

Output

Actual:

PHP Warning:  Illegal offset type in /var/www/hydrate/test.php on line 34
PHP Stack trace:
PHP   1. {main}() /var/www/apiV2/test.php:0
PHP   2. Phalcon\Mvc\Model\Resultset->getFirst() /var/www/hydrate/test.php:34
PHP   3. Phalcon\Mvc\Model\Resultset\Simple->current() /var/www/hydrate/test.php:34
PHP   4. Phalcon\Mvc\Model::cloneResultMapHydrate() /var/www/hydrate/test.php:0
Array
(
)

Expected:

Array
(
      [id] => 1
)

Scripts

Robot.php

<?php

namespace Test;

use Phalcon\Mvc\Model;

/**
 * Class Robot
 *
 * @author  Andrey Dembitskiy
 * @package Test
 */
class Robot extends Model
{
    public $id;

    /**
     * @inheritDoc
     */
    public function getSource()
    {
        return 'Robot';
    }
}

test.php

<?php

require 'Robot.php';

\Phalcon\Mvc\Model::setup(['castOnHydrate' => true]);

$di = new \Phalcon\Di\FactoryDefault();
$di->set('db', function () {
    $descriptor = [
        'adapter'  => 'Mysql',
        'host'     => 'localhost',
        'port'     => 3306,
        'dbname'   => 'phalcon_hydrate',
        'username' => 'root',
        'password' => '',
        'charset'  => 'utf8',
        'options'  => [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']
    ];

    return new \Phalcon\Db\Adapter\Pdo\Mysql($descriptor);
});

\Phalcon\Di::setDefault($di);

/** @var \Phalcon\Mvc\Model\Query\BuilderInterface $builder */
$builder = $di->get('modelsManager')->createBuilder();

// The same problem with \Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS
$result  = \Test\Robot::find()
    ->setHydrateMode(\Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS);


// PHP Warning:  Illegal offset type in /var/www/hydrate/test.php on line 34
// PHP Stack trace:
// PHP   1. {main}() /var/www/apiV2/test.php:0
// PHP   2. Phalcon\Mvc\Model\Resultset->getFirst() /var/www/hydrate/test.php:34
// PHP   3. Phalcon\Mvc\Model\Resultset\Simple->current() /var/www/hydrate/test.php:34
// PHP   4. Phalcon\Mvc\Model::cloneResultMapHydrate() /var/www/hydrate/test.php:0
$row = $result->getFirst();

// Shows `[]`, expected ['id' => '1']
print_r($row);

SQL

CREATE TABLE `phalcon_hydrate`.`Robot` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `phalcon_hydrate`.`Robot` (`id`) VALUES ('1');
INSERT INTO `phalcon_hydrate`.`Robot` (`id`) VALUES ('2');
INSERT INTO `phalcon_hydrate`.`Robot` (`id`) VALUES ('3');
INSERT INTO `phalcon_hydrate`.`Robot` (`id`) VALUES ('4');

Details

  • Phalcon version: 3.0.1
  • PHP Version: PHP 5.6.17-0+deb8u1 (cli)
  • Operating System: Debian 3.16.7-ckt20-1+deb8u3 (2016-01-17)
  • Installation type: Compiling from source
  • Zephir version (if any): 0.9.3a-dev-5c0fb106a6
  • Server: Nginx
  • Other related info (Database, table schema): 10.0.22-MariaDB-0+deb8u1-log
@andrew-demb andrew-demb pushed a commit to andrew-demb/cphalcon that referenced this issue Sep 1, 2016
Andrey Dembitskiy Fixed #12191`Phalcon\Mvc\Model::cloneResultMapHydrate` to correct cre…
…ate array/objects from data by column map with types
129a2d4
@andrew-demb andrew-demb pushed a commit to andrew-demb/cphalcon that referenced this issue Sep 1, 2016
Andrey Dembitskiy Fixed #12191`Phalcon\Mvc\Model::cloneResultMapHydrate` to correct cre…
…ate array/objects from data by column map with types
999f450
@andrew-demb andrew-demb pushed a commit to andrew-demb/cphalcon that referenced this issue Sep 1, 2016
Andrey Dembitskiy Fixed #12191`Phalcon\Mvc\Model::cloneResultMapHydrate` to correct cre…
…ate array/objects from data by column map with types
207555d
@andrew-demb andrew-demb pushed a commit to andrew-demb/cphalcon that referenced this issue Sep 1, 2016
Andrey Dembitskiy Fixed #12191`Phalcon\Mvc\Model::cloneResultMapHydrate` to correct cre…
…ate array/objects from data by column map with types
0c60beb
@andrew-demb andrew-demb pushed a commit to andrew-demb/cphalcon that referenced this issue Sep 1, 2016
Andrey Dembitskiy Fixed #12191`Phalcon\Mvc\Model::cloneResultMapHydrate` to correct cre…
…ate array/objects from data by column map with types
c183b23
@sergeyklay
Collaborator

Fixed in the 3.0.x branch.

@sergeyklay sergeyklay closed this Sep 1, 2016
@sergeyklay sergeyklay added a commit to sergeyklay/cphalcon that referenced this issue Sep 27, 2016
@sergeyklay Andrey Dembitskiy + sergeyklay Fixed #12191`Phalcon\Mvc\Model::cloneResultMapHydrate` to correct cre…
…ate array/objects from data by column map with types
b70e6bc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment