当我尝试在mongodb上获取数据时,我遇到了错误

时间:2015-07-28 17:30:39

标签: php mongodb symfony doctrine

我有很多这样的统计数据:

enter image description here

我在控制器中获取此数据时遇到此错误:

Could not convert array to a date value 

堆栈跟踪:

    Stack Trace
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Types/DateType.php at line 67   -
        }
        if ($datetime === false) {
            throw new \InvalidArgumentException(sprintf('Could not convert %s to a date value', is_scalar($value) ? '"'.$value.'"' : gettype($value)), 0, $exception);
        }
        return $datetime;
at DateType ::getDateTime (array('date' => '2014-07-16 13:39:01.000000', 'timezone_type' => '3', 'timezone' => 'Europe/Istanbul')) 
in app/cache/dev/doctrine/odm/mongodb/Hydrators/OjsAnalyticsBundleDocumentObjectViewsHydrator.php at line 81   +
at OjsAnalyticsBundleDocumentObjectViewsHydrator ->hydrate (object(ObjectViews), array('_id' => object(MongoId), 'entity' => 'article', 'objectId' => '116597', 'logDate' => array('date' => '2014-07-16 13:39:01.000000', 'timezone_type' => '3', 'timezone' => 'Europe/Istanbul')), array()) 
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php at line 453   +
at HydratorFactory ->hydrate (object(ObjectViews), array('_id' => object(MongoId), 'entity' => 'article', 'objectId' => '116597', 'logDate' => array('date' => '2014-07-16 13:39:01.000000', 'timezone_type' => '3', 'timezone' => 'Europe/Istanbul')), array()) 
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php at line 2843   +
at UnitOfWork ->getOrCreateDocument ('Ojs\AnalyticsBundle\Document\ObjectViews', array('_id' => object(MongoId), 'entity' => 'article', 'objectId' => '116597', 'logDate' => array('date' => '2014-07-16 13:39:01.000000', 'timezone_type' => '3', 'timezone' => 'Europe/Istanbul')), array()) 
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Cursor.php at line 224   +
at Cursor ->current ()
at iterator_to_array (object(Cursor), false) 
in vendor/doctrine/mongodb/lib/Doctrine/MongoDB/Cursor.php at line 628   +
at Cursor ->Doctrine\MongoDB\{closure} () 
in vendor/doctrine/mongodb/lib/Doctrine/MongoDB/Cursor.php at line 660   +
at Cursor ->retry (object(Closure), true) 
in vendor/doctrine/mongodb/lib/Doctrine/MongoDB/Cursor.php at line 629   +
at Cursor ->toArray (false) 
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/DocumentRepository.php at line 173   +
at DocumentRepository ->findBy (array('entity' => 'article', 'objectId' => '116597')) 
in src/Ojs/Common/Services/JournalService.php at line 338   +
at JournalService ->getArticleStats ('116597', object(Journal)) 
in src/Ojs/ReportBundle/Controller/AnalyticsReportController.php at line 66   +
at AnalyticsReportController ->detailAction ('116597')
at call_user_func_array (array(object(AnalyticsReportController), 'detailAction'), array('116597')) 
in app/bootstrap.php.cache at line 3094   +
at HttpKernel ->handleRaw (object(Request), '1') 
in app/bootstrap.php.cache at line 3056   +
at HttpKernel ->handle (object(Request), '1', true) 
in app/bootstrap.php.cache at line 3207   +
at ContainerAwareHttpKernel ->handle (object(Request), '1', true) 
in app/bootstrap.php.cache at line 2429   +
at Kernel ->handle (object(Request)) 
in web/app_dev.php at line 31   +

MongoDB映射文件中的相关字段映射:

 /**
     * @MongoDB\Date
     */
    public $logDate;
/**
     * Get logDate
     *
     * @return @MongoDb\Date $logDate
     */
    public function getLogDate()
    {
        return $this->logDate;
    }

    /**
     * Set logDate
     *
     * @param  $logDate
     * @return self
     */
    public function setLogDate($logDate)
    {
        $this->logDate = $logDate;

        return $this;
    }

首先,我问了两件关于这件事的问题。

  • 为什么mongodb将日期数据存储为对象/数组?
  • 我该如何修理这个狗屎?

感谢所有人的兴趣。

1 个答案:

答案 0 :(得分:0)

logDate是一个数组,因为它包含以下属性datetimezone_typetimezone。不知道为什么MongoDB会这样做,我从来没有真正使用它,所以我没有回答这个问题。但是,如果您还想处理时区信息,您可以将日期格式化为ISO8601并存储,然后交叉引用给定时区以获得时区的实际名称。

要修复logDate问题,您只需将其用作数组,例如logDate['date']获取实际日期时间。