我假设doctrine2会优化我的查询,以便为任何数据库事务提供最佳性能。
我在一个数据库表中插入了大约500个记录,我注意到它创建了500+查询来插入记录(每个记录一个查询),我想知道为什么不使用多个插入来一次插入所有记录,这难道不是减少了负载并优化了查询吗?我是不是从教义中遗漏了一些关于这种行为的东西?
下面是我用来插入的代码:
$content = json_decode($response->getBody());
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
foreach ($content as $value) {
$log = new log();
$log->fromArray($value);
$em->persist($log);
}
$em->flush();Update1:
下面是请求的fromArray()的内容,此函数的主要目的是将数组中的值合并为类属性:
/**
* Map parameters with class property.
*
* @param $array array
* @access public
* @return $this
*/
public function fromArray(array $array)
{
foreach ($array as $property => $value) {
$method = 'set'.ucwords($property);
$this->$method($value);
}
return $this;
}这是$content的内容
Array
(
[0] => stdClass Object
(
[id] => 111
[guid] => aaaa-bbbb-cccc
[wid] => 100
[pid] => 101
[start] => 2014-11-22T12:44:44+00:00
[stop] => 2014-11-22T15:23:11+00:00
[duration] => 9507
[description] => Log description
[tags] => Array
(
[0] => test
)
[at] => 2014-11-24T07:28:09+00:00
[uid] => 51
)
[1] => stdClass Object
(
[id] => 112
[guid] => dddd-eee
[wid] => 100
[pid] => 101
[billable] =>
[start] => 2014-11-22T15:35:07+00:00
[stop] => 2014-11-22T15:45:21+00:00
[duration] => 614
[description] => Lorem description
[tags] => Array
(
[0] => php
[1] => pm
)
[at] => 2014-11-24T04:35:30+00:00
[uid] => 51
)
)发布于 2015-03-02 13:06:11
请看一下这文档。在每次迭代时,Doctrine都会为您创建一个insert ...查询,毕竟,当您调用flush()时,Doctrine会在循环中一次将所有这些插入查询发送到db (在您的例子中使用类似于foreach(queries as query) { run->query.. }的机制)--这些查询中的任何一个都是插入的,因此,正如我在注释中指出的,在您的情况下不存在异常情况。ORM可能不适用于所有情况。
https://stackoverflow.com/questions/28808967
复制相似问题