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] Error usage option orm.cast_on_hydrate and hydrate mode - ARRAY/OBJECTS #12191

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

Comments

@andrew-demb
Copy link
Contributor

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
@sergeyklay
Copy link
Contributor

Fixed in the 3.0.x branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants