我需要编写一个foreach循环,其中用户被订阅并使用doctrine插入到数据库中。我的代码:Symfony,创建新对象很慢
$i=0;
$batchSize=20;
foreach ($members as $member) {
$subscription = new Subscription($company, $user);
$entityManager->persist($subscription);
// debug
$i++;
error_log($i);
if ($i % $batchSize === 0) {
$entityManager->flush();
$entityManager->clear();
}
}
这段代码真的很慢。对于大约100个用户,这段代码需要几分钟才能执行。这应该快很多吧?
当我删除对象创建(和setMember,和entityManager线)时,此代码执行不到一秒钟。
我在做什么错?
更新:实体代码 我改变了一些变量,因为代码秘密。如果任何代码错误,那是因为我做了很快的修改,所以我可以在这里发布我的问题。在上面的订阅类使用
class Subscription implements \JsonSerializable
{
protected $id;
protected $company;
protected $user;
private $created_on;
private $blocked;
private $attributes;
public function __construct(Company $company, User $user)
{
$this->company = $company;
$this->user = $user;
$this->created_on = new \DateTime();
$this->blocked = false;
$this->attributes = new AttributeCollection();
$this->setDefaultAttributes();
}
public function jsonSerialize() {
return array(
'id' => $this->id,
'user' => $this->user,
'blocked' => $this->blocked,
'created_on' => $this->created_on->format('Y-m-d H:i:s')
);
}
// due to company secrets variables have been changed to a,b,c,d,e,f
public function setDefaultAttributes()
{
if (null == $this->attributes)
$this->attributes = new AttributeCollection();
$this->attributes->addAttribute(new Attribute('a'));
$this->attributes->addAttribute(new Attribute('b'));
$this->attributes->addAttribute(new Attribute('c'));
$this->attributes->addAttribute(new Attribute('d'));
$this->attributes->addAttribute(new Attribute('e'));
$this->attributes->addAttribute(new Attribute('f'));
}
public function getId()
{
return $this->id;
}
public function setUser(User $user = null)
{
$this->user = $user;
return $this;
}
public function getUser()
{
return $this->user;
}
public function setCompany(Company $company = null)
{
$this->company = $company;
return $this;
}
public function getCompany()
{
return $this->company;
}
public function getCreatedOn()
{
return $this->created_on;
}
public function setBlocked($blocked)
{
$this->blocked = $blocked;
return $this;
}
public function getBlocked()
{
return $this->blocked;
}
public function setAttributes(AttributeCollection $attributes = null)
{
$this->attributes = $attributes;
return $this;
}
public function getAttributes()
{
return $this->attributes;
}
}
AttributeCollection类:
class AttributeCollection
{
protected $id;
protected $attributes;
public function __construct()
{
$this->attributes = new ArrayCollection();
}
public function removeDefaults()
{
foreach ($this->attributes as $attribute)
if ($attribute->isSystemDefault())
$this->removeAttribute($attribute);
}
public function clearDefaults()
{
foreach ($this->attributes as $attribute)
$attribute->setSystemDefault(false);
}
public function getAttributes()
{
return $this->attributes;
}
public function addAttribute(Attribute $attribute)
{
if (!$this->findAttributeByName($attribute->getName()))
$this->attributes[] = $attribute;
}
public function removeAttribute(Attribute $attribute)
{
$this->attributes->removeElement($attribute);
}
public function findAttributeByName($name)
{
foreach ($this->attributes as $attribute)
if ($attribute->getName() == $name)
return $attribute;
return;
}
public function get($name)
{
$attribute = $this->findAttributeByName($name);
if ($attribute)
return $attribute->getValue();
return;
}
public function getAll()
{
return $this->attributes->toArray();
}
}
也许刷新'20'实体是一项耗时的工作。你试过降低'$ batchSize'吗? – D4V1D
@ D4V1D我现在知道这是耗时的模型创建。如果我删除原则相关的行,并离开模型创建。它需要同样长的时间。 –
这行'error_log($ i);'做了什么?我的意思是它使用哪个处理程序?它每次都打开/关闭日志文件吗? –