Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 and Doctrine 2 result data incorrectly returns an array of nulls

I have a query I run in Doctrine; it now is failing, but in a weird way. This query has worked for a year, I just messed with my composer.json a bit by adding a couple new libraries, and changing some versions. I also updated my Linux box (maybe MySQL or MariaDB has been updated, not sure).

Whatever has changed (doubtful it's my bundle's Doctrine model code, unless it wasn't kosher in the first place and a core update now reveals it as broken), the query is now returning results, with an array of the correct amount of keys, however the values are getting NULLed somehow. The array looks exactly like this, a direct copy and paste from the var_dump, e.g.

var_dump($query->getSql());
var_dump($results);
exit;

select GoalLabel from opt_goals where scale = 'mathematics'

array(17) { [0]=> NULL [1]=> NULL [2]=> NULL [3]=> NULL [4]=> NULL [5]=> NULL [6]=> NULL [7]=> NULL [8]=> NULL [9]=> NULL [10]=> NULL [11]=> NULL [12]=> NULL [13]=> NULL [14]=> NULL [15]=> NULL [16]=> NULL } 

But when I run this same query in MySQL command line, I get the actual results, which is the same amount of keys, but the values are there.

Here's the non default portion of my composer.json require section:

    "components/jquery": "~1.11,<2.0",
    "twbs/bootstrap": "3.3.*",
    "braincrafted/bootstrap-bundle": "~2.1",
    "knplabs/knp-menu": "~1.1",
    "knplabs/knp-menu-bundle": "~1.1",
    "knplabs/knp-paginator-bundle": "dev-master",
    "symfony/icu": "1.1.*",
    "mnot/hinclude": "dev-master",
    "rhumsaa/array_column": "~1.1",
    "webfactory/exceptions-bundle": "@stable",
    "friendsofsymfony/jsrouting-bundle": "~1.5",
    "hearsay/require-js-bundle": "~1.0",
    "browserstate/history.js": "dev-master"

Pay close attention to any version adjustments, because I did mess with this. However I don't know at which point Doctrine went off the deep end.

Edit: per the latest comments, and now seeing this, is this what is going on? Actually this URL is more appropriate because I am not using doctrine/dbal, I'm using doctrine/orm.

like image 655
blamb Avatar asked Apr 25 '15 04:04

blamb


2 Answers

This problem was caused by the updating of doctrine/orm to version 2.5 from 2.4.x. There is a lot of BC breaks.

Downgrade your doctrine/orm to https://packagist.org/packages/doctrine/orm#v2.4.7 and the problem will go away. After knowing this is your issue, you need to rewrite your queries to satisfy any new requirements. I did see a note in there about MariaDB problem as well.

In fact, since I haven't tackled this fully yet myself, it might be as easy as regenerating the entities. I'm always hesitant when I do that....

like image 179
blamb Avatar answered Oct 03 '22 13:10

blamb


Today I had to face this "issue". In my case, all was about null values in the column marked as @id. Let me show you.

This is invoices's table:

mysql> select * from payments;
+----------------------------------+---------------------+-------+---------+
| transaction                      | date                | total | invoice |
+----------------------------------+---------------------+-------+---------+
| JB2BJ43dqhI7xiVxP1j1ExlIbraplHiS | 2015-04-24 21:23:08 |   320 |       3 |
| DIavWPjtVf9ZJ0m4GBnP2ZDCBzV4PBmQ | 2015-04-24 21:49:39 |   320 |       4 |
| XporZAkdIFDbdq9Uzvw1rp2GD4W98esy | 2015-04-24 22:18:10 |   320 |       5 |
| OFgUCb5kdjftqiNUmU6BCok47EZ6Sw2X | 2015-04-27 01:10:01 |    25 |      10 |
+----------------------------------+---------------------+-------+---------+

I added a new column in order to marked as a primary key:

mysql>alter table invoices add column `id_payment` int auto_increment;

mysql> select id_payment, transaction, date, total, invoice from paymen
s limit 4;
+------------+----------------------------------+---------------------+-------+--------+
| id_payment | transaction                      | date                | total | invoice|
+------------+----------------------------------+---------------------+-------+--------+
|       NULL | JB2BJ43dqhI7xiVxP1j1ExlIbraplHiS | 2015-04-24 21:23:08 |   320 |       3|
|       NULL | DIavWPjtVf9ZJ0m4GBnP2ZDCBzV4PBmQ | 2015-04-24 21:49:39 |   320 |       4|
|       NULL | XporZAkdIFDbdq9Uzvw1rp2GD4W98esy | 2015-04-24 22:18:10 |   320 |       5|
|       NULL | OFgUCb5kdjftqiNUmU6BCok47EZ6Sw2X | 2015-04-27 01:10:01 |    25 |      10|
+------------+----------------------------------+---------------------+-------+--------+

Now, in the Invoice model I set the new column as my primary key:

/**
 * @entity
 * @table(name="invoices")
 */
class Invoice
{
    /**
     * @id
     * @column(type="int", name="id_payment")
     */
    protected $id;

    // ... 

When I altered the table, all values in id_payment column is null. This will return null values even all other fields contains information:

$qb = $em->createQueryBuilder();
$qb->select(['a']);
$qb->from(Invoice::class, 'a');
$invoices = $qb->getQuery()->getArrayResult();
dd($invoices);

Result:

[
    null,
    null,
    null,
    null,
]
like image 42
manix Avatar answered Oct 02 '22 13:10

manix