Why won't createNativeQuery return records containing null value? #5407

Closed
opened 2026-01-22 15:06:54 +01:00 by admin · 15 comments
Owner

Originally created by @dipens on GitHub (Feb 3, 2017).

I have two identical Symfony 2.8 projects. The only difference in these two projects is that one is running Doctrine ORM version v2.4.8 and the other is running v2.5.5

The problem

I have this following piece of code in both Symfony projects.

$rsm = new ResultSetMapping();
$rsm->addEntityResult('EntityBundle:New', 'u');
$rsm->addFieldResult('u', 'newPK', 'newPK');
$rsm->addFieldResult('u', 'msg', 'msg');
$query = $em->createNativeQuery('StoredProcedureName @a= :a, @b= :b, @c= :c', $rsm);
$query->setParameter('a', 'a');
$query->setParameter('b', 'b');
$query->setParameter('c', 'c');
$result = $query->getArrayResult();
var_dump($result);

My Entity looks like this:

use Doctrine\ORM\Mapping as ORM;

/**
 * New
 *
 * @ORM\Table(name="dbo.New")
 * @ORM\Entity()
 */
class New
{
    /**
     * @var string
     *
     * @ORM\Column(name="newPK", type="string", length=255, unique=false)
     */
    private $newPK;
    /**
     * @var string
     *
     * @ORM\Column(name="msg", type="string", length=255, unique=false)
     */
    private $msg;
    /**
     * @return string
     */
    public function getNewPK()
    {
        return $this->newPK;
    }

    /**
     * @param string $newPK
     */
    public function setNewPK($newPK)
    {
        $this->newPK = $newPK;
    }
    /**
     * @return string
     */
    public function getMsg()
    {
        return $this->msg;
    }

    /**
     * @param string $msg
     */
    public function setMsg($msg)
    {
        $this->msg = $msg;
    }
}

In my StoredProcedure all I have is this:

CREATE PROCEDURE [dbo].[StoredProcedureName] 
AS
SELECT null as newPK, 'aaa' As msg
GO

This code works perfectly fine on version v2.4.8 returning me null value perfectly. However this doesn't work on v2.5.5. All it returns is an array with NULL inside it and not the newPK AND msg value. If I remove the null and replace it with an empty string or anything else for that matter, it works. But for some god forsaken reason, it does not like the NULL value. All I want is to check if null is returned or not but I can't seem to check without doctrine returning me proper values.

Is there anything that changed between these versions that I am not aware of? What do I need to do in order to read NULL values from a native query?

Originally created by @dipens on GitHub (Feb 3, 2017). I have two identical Symfony 2.8 projects. The only difference in these two projects is that one is running Doctrine ORM version `v2.4.8` and the other is running `v2.5.5` **The problem** I have this following piece of code in both Symfony projects. $rsm = new ResultSetMapping(); $rsm->addEntityResult('EntityBundle:New', 'u'); $rsm->addFieldResult('u', 'newPK', 'newPK'); $rsm->addFieldResult('u', 'msg', 'msg'); $query = $em->createNativeQuery('StoredProcedureName @a= :a, @b= :b, @c= :c', $rsm); $query->setParameter('a', 'a'); $query->setParameter('b', 'b'); $query->setParameter('c', 'c'); $result = $query->getArrayResult(); var_dump($result); My Entity looks like this: use Doctrine\ORM\Mapping as ORM; /** * New * * @ORM\Table(name="dbo.New") * @ORM\Entity() */ class New { /** * @var string * * @ORM\Column(name="newPK", type="string", length=255, unique=false) */ private $newPK; /** * @var string * * @ORM\Column(name="msg", type="string", length=255, unique=false) */ private $msg; /** * @return string */ public function getNewPK() { return $this->newPK; } /** * @param string $newPK */ public function setNewPK($newPK) { $this->newPK = $newPK; } /** * @return string */ public function getMsg() { return $this->msg; } /** * @param string $msg */ public function setMsg($msg) { $this->msg = $msg; } } In my StoredProcedure all I have is this: CREATE PROCEDURE [dbo].[StoredProcedureName] AS SELECT null as newPK, 'aaa' As msg GO This code works perfectly fine on version `v2.4.8` returning me null value perfectly. However this doesn't work on `v2.5.5`. All it returns is an array with NULL inside it and not the `newPK` AND `msg` value. If I remove the null and replace it with an empty string or anything else for that matter, it works. But for some god forsaken reason, it does not like the `NULL` value. All I want is to check if null is returned or not but I can't seem to check without doctrine returning me proper values. Is there anything that changed between these versions that I am not aware of? What do I need to do in order to read `NULL` values from a native query?
admin added the BugMissing Tests labels 2026-01-22 15:06:54 +01:00
admin closed this issue 2026-01-22 15:06:55 +01:00
Author
Owner

@Ocramius commented on GitHub (Feb 3, 2017):

@dipens are the DBAL versions also different?

@Ocramius commented on GitHub (Feb 3, 2017): @dipens are the DBAL versions also different?
Author
Owner

@dipens commented on GitHub (Feb 3, 2017):

@Ocramius Yes sorry should have mentioned that. The one with v2.4.8 has v2.5.4 installed. The one with v2.5.5 has v2.5.5 installed.

@dipens commented on GitHub (Feb 3, 2017): @Ocramius Yes sorry should have mentioned that. The one with `v2.4.8` has `v2.5.4` installed. The one with `v2.5.5` has `v2.5.5` installed.
Author
Owner

@Ocramius commented on GitHub (Feb 4, 2017):

@dipens did you try running this with DBAL only? Did the results change? What DB/adapter are you using?

@Ocramius commented on GitHub (Feb 4, 2017): @dipens did you try running this with DBAL only? Did the results change? What DB/adapter are you using?
Author
Owner

@dipens commented on GitHub (Feb 7, 2017):

@Ocramius With DBAL I am getting the results. I am using freeTDS on Ubuntu 14.04.

@dipens commented on GitHub (Feb 7, 2017): @Ocramius With DBAL I am getting the results. I am using freeTDS on Ubuntu 14.04.
Author
Owner

@Ocramius commented on GitHub (Feb 7, 2017):

Ping @deeky665 could our changes in prepared statements in 2.5 affect this?

@Ocramius commented on GitHub (Feb 7, 2017): Ping @deeky665 could our changes in prepared statements in 2.5 affect this?
Author
Owner

@deeky666 commented on GitHub (Feb 7, 2017):

@Ocramius the prepared statement stuff was released with 2.5.11, not 2.5.5. We'll have to take a look at the differences between DBAL 2.5.4 and 2.5.5 if I understand correctly.

@deeky666 commented on GitHub (Feb 7, 2017): @Ocramius the prepared statement stuff was released with `2.5.11`, not `2.5.5`. We'll have to take a look at the differences between DBAL `2.5.4` and `2.5.5` if I understand correctly.
Author
Owner

@Ocramius commented on GitHub (Feb 7, 2017):

@dipens can you try adding a test case meanwhile?

@Ocramius commented on GitHub (Feb 7, 2017): @dipens can you try adding a test case meanwhile?
Author
Owner

@dipens commented on GitHub (Feb 9, 2017):

@Ocramius Sorry how do I do that?

@dipens commented on GitHub (Feb 9, 2017): @Ocramius Sorry how do I do that?
Author
Owner

@Ocramius commented on GitHub (Feb 9, 2017):

See https://github.com/doctrine/doctrine2/tree/master/tests/Doctrine/Tests/ORM/Functional/Ticket for examples

@Ocramius commented on GitHub (Feb 9, 2017): See https://github.com/doctrine/doctrine2/tree/master/tests/Doctrine/Tests/ORM/Functional/Ticket for examples
Author
Owner

@dipens commented on GitHub (Aug 17, 2017):

I did a work around with this one using addScalarResult instead of addFieldResult. The modified code looks like this:

$rsm = new ResultSetMapping();
$rsm->addScalarResult( 'newPK', 'newPK');
$rsm->addScalarResult( 'msg', 'msg');
$query = $em->createNativeQuery('StoredProcedureName @a= :a, @b= :b, @c= :c', $rsm);
$query->setParameter('a', 'a');
$query->setParameter('b', 'b');
$query->setParameter('c', 'c');
$result = $query->getArrayResult();
var_dump($result);
@dipens commented on GitHub (Aug 17, 2017): I did a work around with this one using `addScalarResult` instead of `addFieldResult`. The modified code looks like this: $rsm = new ResultSetMapping(); $rsm->addScalarResult( 'newPK', 'newPK'); $rsm->addScalarResult( 'msg', 'msg'); $query = $em->createNativeQuery('StoredProcedureName @a= :a, @b= :b, @c= :c', $rsm); $query->setParameter('a', 'a'); $query->setParameter('b', 'b'); $query->setParameter('c', 'c'); $result = $query->getArrayResult(); var_dump($result);
Author
Owner

@Ocramius commented on GitHub (Aug 17, 2017):

What is the resolution here?

@Ocramius commented on GitHub (Aug 17, 2017): What is the resolution here?
Author
Owner

@dipens commented on GitHub (Aug 18, 2017):

I just edited my close comment. Please have a look

@dipens commented on GitHub (Aug 18, 2017): I just edited my close comment. Please have a look
Author
Owner

@Ocramius commented on GitHub (Aug 18, 2017):

@dipens that still doesn't say much to me - is this therefore a bug? Can it be reproduced?

@Ocramius commented on GitHub (Aug 18, 2017): @dipens that still doesn't say much to me - is this therefore a bug? Can it be reproduced?
Author
Owner

@potievdev commented on GitHub (Dec 12, 2017):

@Ocramius After upgrading from 2.4 to 2.5 I have the same problem. Late I can send example how reproduce this bug.

@potievdev commented on GitHub (Dec 12, 2017): @Ocramius After upgrading from 2.4 to 2.5 I have the same problem. Late I can send example how reproduce this bug.
Author
Owner

@piedrucci commented on GitHub (Dec 25, 2019):

@dipens this one

I did a work around with this one using addScalarResult instead of addFieldResult. The modified code looks like this:

$rsm = new ResultSetMapping();
$rsm->addScalarResult( 'newPK', 'newPK');
$rsm->addScalarResult( 'msg', 'msg');
$query = $em->createNativeQuery('StoredProcedureName @a= :a, @b= :b, @c= :c', $rsm);
$query->setParameter('a', 'a');
$query->setParameter('b', 'b');
$query->setParameter('c', 'c');
$result = $query->getArrayResult();
var_dump($result);

this one work for me!! thankful....

@piedrucci commented on GitHub (Dec 25, 2019): @dipens this one > I did a work around with this one using `addScalarResult` instead of `addFieldResult`. The modified code looks like this: > > ``` > $rsm = new ResultSetMapping(); > $rsm->addScalarResult( 'newPK', 'newPK'); > $rsm->addScalarResult( 'msg', 'msg'); > $query = $em->createNativeQuery('StoredProcedureName @a= :a, @b= :b, @c= :c', $rsm); > $query->setParameter('a', 'a'); > $query->setParameter('b', 'b'); > $query->setParameter('c', 'c'); > $result = $query->getArrayResult(); > var_dump($result); > ``` this one work for me!! thankful....
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5407