如何在不刷新其他实体的情况下只刷新一个实体?如何使用Doolrine冲洗一个实体而不冲洗另一个实体?
例如: 我有2个对象BStatus和B.它们通过OneToOne关系相关。 我想在B上做一些工作,但不保存到数据库中,但将工作状态保存在BStatus中,以便其他进程可以读取它。
class B {
/**
* @var int
* @ORM\Column(type="integer")
*/
public $i = 0;
/**
* @var BStatus
* @ORM\OneToOne(targetEntity="BStatus", inversedBy="b")
*/
public $status;
/**
* B constructor.
*/
public function __construct() {
$this->status = new BStatus($this);
}
public function count() {
return $this->i++;
}
}
class BStatus {
/**
* @var float
* @ORM\Column(type="float")
*/
public $progress = 0;
// <UPDATED>
/**
* @var B
* @ORM\OneToOne(targetEntity="B", mappedBy="status")
*/
public $progress = 0;
// </UPDATED>
/**
* BStatus constructor.
* @param B $b
*/
public function __construct(B $b)
{
$this->b = $b;
}
}
$b = // Load from DB
$max = 100;
for ($i = 0; $i < $max; $i++) {
$num = $b->count();
$b->getStatus()->setProgress(($num + 1)/$max);
// Here I want to save the status
}
// Here I want to save $b
更新 BSTATUS有指针B.
更新2
试图脱离B,冲洗和合并乙回来。
for ($i = 0; $i < $max; $i++) {
$num = $b->count();
$status = $b->getStatus();
$status->setProgress(($num + 1)/$max);
$em->detach($b);
$em->flush();
$em->merge($b);
}
得到execption:
'Doctrine\ORM\ORMInvalidArgumentException' with message 'A new entity was found through the relationship BStatus#b' that was not configured to cascade persist operations for entity: local.
To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).'
你可以使用destruct的具体? – Deep
您没有cascade = {“persist”}关联。为什么简单的持续状态将不起作用?例如。 $ EM->坚持($ B->的getStatus()); 稍后调用。 – nutrija
是的,但当我调用flush()保存之前所做的所有更改 –