douyuan1049 2017-02-02 20:53
浏览 75
已采纳

为什么createNativeQuery不会返回包含空值的记录?

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?

  • 写回答

1条回答 默认 最新

  • dtzd65908 2017-08-17 20:29
    关注

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

    $rsm = new ResultSetMapping();
    $rsm->addScalarResult('u', 'newPK', 'newPK');
    $rsm->addScalarResult('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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流