doune1000 2014-12-28 08:00
浏览 37
已采纳

持久化@OneToMany字段时自动传播身份

I am developing twig fiddle, the js/sql/php-fiddle equivalent for Twig language. We can add several templates to render a result, and I have the following entities:

class Fiddle
{

    /**
     * @var ArrayCollection[FiddleTemplate]
     *
     * @ORM\OneToMany(targetEntity="FiddleTemplate", mappedBy="fiddle", cascade={"all"}, orphanRemoval=true)
     */
    protected $templates;

    // ...

}

class FiddleTemplate
{

    /**
     * @var Fiddle
     *
     * @ORM\ManyToOne(targetEntity="Fiddle", inversedBy="templates")
     * @ORM\JoinColumn(name="fiddle_id", referencedColumnName="id", onDelete="cascade")
     * @ORM\Id
     */
    protected $fiddle;

    /**
     * @var string
     *
     * @ORM\Column(name="filename", type="string", length=64)
     * @ORM\Id
     */
    protected $filename = 'main.twig';

    // ...
}

As you can imagine, I can add / remove templates from UI.

Fiddle's id takes part of the Fiddle entity, so if I want to persist templates for the first time, I first need to persist the fiddle. I do it using a Doctrine event suscriber (see rev history if interested). But if I try to add a template to an already-existing fiddle and save, I come up with the following exception:

Entity of type Fuz\AppBundle\Entity\FiddleTemplate is missing an assigned ID for field 'fiddle'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.

Sounds logic enough, as I need to spread the Fiddle's instance to the template before persisting.

But I did not found any Doctrine event fired on the Fiddle entity, preUpdate is not called even if templates takes part of the entity, because it does not require any update. Only a pre/postPersist event is fired on the FiddleTemplate entity, but it does not help that much.

I currently do something quite ugly IMHO: before persisting, I call a spreadFiddle on my entity:

$fiddle->spreadFiddle();
$this->em->persist($fiddle);
$this->em->flush();

The method looks like:

public function spreadFiddle()
{
    foreach ($this->templates as $template)
    {
        $template->setFiddle($this);
    }
}

Here my question come:

Is there a better way (using events or annotations) to spread the Fiddle entity to all templates?

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-12-28 08:15
    关注

    Isn't it better to make Fiddle::addTemplate($template) method, which will add template to collection ($this->templates->add($template)) and set link to self ($template->setFiddle($this)). AFAIK, it is recommended way if Fiddle is an aggregation root of your model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度