很抱歉的標題,我不知道怎麼說了。LifecycleCallback稱爲ArrayCollection的
我有兩個Entity:菌株對管一個一對多的關係,以及級聯= {「堅持」,「刪除」}屬性。 對於兩次實體我定義上有PrePersist回調飛他們的名字。但是,這隻適用於Tube實體,在Strain實體上我有一個問題,因爲它的名字是在第一個管道名稱上定義的。我認爲PrePersist在Tubes之前應用於應變。那麼如果Tubes沒有名字,那麼Strain的名字就是空的。
如果我做一個foreach而在控制器堅持一個各管一個堅持之前的應變,它工作正常。但我不太喜歡它。我們可以在某處配置它嗎,比如PrePersist的優先順序?
我的實體:
管
class Tube
{
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Strain", inversedBy="tubes")
*/
private $gmoStrain;
...
// SETTERS & GETTERS
public function setStrain(Strain $strain)
{
$this->strain = $strain;
return $this;
}
public function getStrain()
{
return $this->strain;
}
...
//LifecycleCallback
/**
* Before persist.
*
* @ORM\PrePersist()
*/
public function prePersist()
{
// Give a name to the tube
// The name is composed like this:
// ProjectPrefix_BoxLetter_xxxType
// ProjectPrefix (The prefix of the first Tube)
$projectPrefix = $this->getBox()->getProject()->getPrefix();
// BoxLetter (idem, the first tube)
$boxLetter = $this->getBox()->getBoxLetter();
// In array the first cell is 0, in real box, it's 1
$cell = $this->cell + 1;
// Adapt the boxCell like: 1 => 001, 10 => 010, 100 => 100, never more than 999
if ($cell < 10) {
$boxCell = '00'.$cell;
} elseif ($cell > 99) {
$boxCell = $cell;
} else {
$boxCell = '0'.$cell;
}
// Type Letter
$lastLetter = $this->getStrain()->getType()->getLetter();
// Generate the tube name
$this->name = $projectPrefix.'_'.$boxLetter.$boxCell.$lastLetter;
}
}
應變:
class Strain
{
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Tube", mappedBy="strain", cascade={"persist", "remove"})
*/
private $tubes;
...
//SETTERS & GETTERS
public function addTube(Tube $tube)
{
if (!$this->tubes->contains($tube)) {
$tube->setGmoStrain($this);
$this->tubes->add($tube);
}
}
public function removeTube(Tube $tube)
{
if ($this->tubes->contains($tube)) {
$this->tubes->removeElement($tube);
}
}
public function getTubes()
{
return $this->tubes;
}
...
//LifecycleCallback
/**
* Before persist.
*
* @ORM\PrePersist()
*/
public function postPersist()
{
// The automatic name of the strain is the name of the first tube
// when the strain is registred the first time
$this->systematicName = $this->getTubes()->first()->getName();
}
}
很抱歉這麼晚回答。我剛剛嘗試過,但我有一個空白頁面。我明白你想要做什麼。但是在這裏,我需要將我的prePersist代碼放在此頁面中,而不是放在實體中?我從來沒有用過它,對不起。 – mpiot
沒關係,我需要刪除應變中的@ORM \ PrePersist,並將其保存在Tube中。然後我在Strain PrePersist上使用EventListener,然後對Tubes做一個foreach,並在我稱之爲生成應變名稱的函數之後堅持。 – mpiot