9

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.

7
  • 1
    instead var_dump() try dump() if you use symfony +2.6 and lets see what you get. Commented Apr 25, 2015 at 10:44
  • 1
    Did you check the EXPLAIN in your web profiler? Commented Apr 28, 2015 at 20:35
  • no but thats a great idea, i dont have a profiler, this is a json response. how would i do that? i found this stackoverflow.com/questions/21555697/… and trying the solution to add the service now Commented Apr 28, 2015 at 20:41
  • 1
    You can make the call to your JSON response with app_dev.php/ prepended to the URI and then go through the history of your requests (see the "view last 10" at the top of every full-page profiler?) Commented Apr 28, 2015 at 20:54
  • 1
    Do You use Doctrine? It was update to 2.5 version. Try to use 2.4. Anyway I had some problems after doctrine update. Commented Apr 28, 2015 at 23:30

4 Answers 4

5

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....

Sign up to request clarification or add additional context in comments.

Comments

2

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,
]

1 Comment

Thank you for this answer! I had the same problem and it saved me a lot of time.
0

it's because you did a var_dump.

var_dump only displays the items in their current state as the objects themselves have not been populated yet. they get pulled on their first call if you lazy loaded them.

2 Comments

i dont think so, beacuse its a json app, if i simply pull the url, i get [null,null,null,null], this is how i noticed teh error in the first place. then i put the debug stuff on there, and its showing me the same.. var_dump will show the correct result, however, because im var dumping the results ` $results = $query->getArrayResult();` so im not sure what you mean. getResult() yields same result. so what you see in the snippet in the OP, is exactly whats returning, the first line is the $query->getSql(), and the 2nd line is the $results dump
lazy loading has nothing to do with "getting the results", but "using the results", for me the hint is still valid without a negative proof and btw the abstractquery execute method caches queries, the best thing you can do is xdebug the code behaviour
0

Check if you have @ORM\Id annotation in your entity!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.