簡體   English   中英

在 Symfony 中接收各種實體作為參數

[英]Receive all kind of Entity as an Argument in Symfony

我有一種方法可以從 Symfony 中的工作單元(實體管理器)獲取實體的更改集,我希望它接收所有實體(帖子、用戶...)而不是特定實體。

public function getChanges(Post $event): array
    {
        $uow = $this->entityManager->getUnitOfWork();
        $uow->computeChangeSets();

        return $uow->getEntityChangeSet($event);
    }

你有什么想法嗎? 一種解決方案是獲取 object 作為參數,但我更喜歡僅獲取 function 中的 Symfony 實體對象。

尋找學說實體聽眾。

https://symfony.com/doc/current/doctrine/events.html#doctrine-entity-listeners

並且不要過濾其中的實體,從示例中刪除這部分:

    // if this listener only applies to certain entity types,
    // add some code to check the entity type as early as possible
    if (!$entity instanceof Product) {
        return;
    }
if (!$this->entityManager->contains($entity)) {
            throw new Exception('The given object must be doctrine entity');
        } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM