【问题标题】:doctrine query returning array, not object as expected学说查询返回数组,而不是预期的对象
【发布时间】:2015-04-09 15:59:12
【问题描述】:

我正在使用 Symfony 2.6 构建一个小项目。它非常简单,只有一个实体类映射到使用在 Raspberry Pi 上运行的 python 插入 MySQL 数据库的数据。

问题

当我运行这个简单的存储库查询时,结果集似乎被转换为导致错误的数组(如下)。我不确定为什么会发生这种转换。

回购方式

public function getDataInRange( \DateTime $from, \DateTime $until)
{
    $query = $this->getEntityManager()->createQuery('
        select d
        from AppBundle:weatherdata d
        where d.idx > :f
        and d.idx < :u ')->setParameters(array( 'f' => $from, 'u' => $until      ));

    return $query->getResult();
}

错误

Catchable Fatal Error: Object of class DateTime could not be converted to string

数据库结构

主键,'idx' 是 datetime 对象,其他都是 double

试过了..

我尝试将 repo 方法更改为返回 getArrayResult(),这会停止错误,但我需要将结果集设为对象格式,因为我需要对数据进行其他处理。

提前致谢。

【问题讨论】:

    标签: php symfony doctrine-orm


    【解决方案1】:
    1. 你最好使用 BETWEEN
    2. 将日期时间转换为字符串

      $qb->where('e.fecha BETWEEN :monday AND :sunday') ->setParameter('monday', $monday->format('Y-m-d')) ->setParameter('sunday', $sunday->format('Y-m-d'));

    please use the search, already answered here

    【讨论】:

    • 这不是我要问的。查询工作正常,只返回一个数组集,而不是一个对象集。是的,我确实使用了搜索,但没有发布完全正确的解决方案。
    猜你喜欢
    • 2013-12-01
    • 2021-01-30
    • 2018-08-12
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    相关资源
    最近更新 更多