我正在嘗試使用 Symfony 及其捆綁系統創建一個插件系統。我知道 bundle 必須是獨立的,但對于我的系統,我知道我的 bundle 永遠不會在另一個背景關系中使用。
我有一個名為“Mark”的學說物體,我想從名為“Student”的主應用程式中關聯一個物體,以便學生可以從我的標記插件中獲得標記。
目前,我只有一個非常簡單的物體 Mark,我不知道如何將新欄位與我的 Student 物體相關聯
namespace Zenith\QuinzeBundle\Model;
[...]
#[ORM\Entity(repositoryClass: MarkRepository::class)]
class Mark
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $comment = null;
public function getId(): ?int
{
return $this->id;
}
public function getComment(): ?string
{
return $this->comment;
}
public function setComment(string $comment): self
{
$this->comment = $comment;
return $this;
}
}
這是我的學生物體(簡化):
namespace Zenith\Entity;
[...]
#[ORM\Entity(repositoryClass: StudentRepository::class)]
class Student
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $firstname = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
public function __construct()
{
$this->userfieldValues = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
如何將這兩個物體關聯起來,一個在應用程式中,另一個在捆綁包中?
uj5u.com熱心網友回復:
您可以創建一個指向學生表的外鍵鏈接表。連接表:學生 | 標記 | 插件物體,其中學生是學生物體的外鍵。Mark 是分配給學生的插件的識別符號,不使用任何外鍵。使用此解決方案,您可以隨時添加新的學生插件,而無需修改學生物體。
uj5u.com熱心網友回復:
正如@bechir 建議的那樣,我使用了這個鏈接形式的 Doctrine 檔案并起到了魅力!
編輯:正如評論所說,解決問題的部分是使用 Doctrine 工具中的 ResolveTargetEntityListener 類。它可以允許您注入一個實作所需介面的物體。我為合同創建了一個捆綁包,并為我的物體添加了我需要的每個介面。然后在我的主應用程式中需要這個合同包,然后我的包將我的物體注入到學說元資料中以重寫關聯。
在 ContractsBundle 中
namespace Zenith\ContractsBundle;
interface StudentInterface { ... }
namespace Zenith\ContractsBundle;
interface MarkInterface { ... }
在主 Symfony 應用程式中:
namespace Zenith\Entity;
use Zenith\ContractsBundle\StudentInterface;
class Student implements StudentInterface {
{...}
#[ORM\OneToMany(mappedBy: 'student', targetEntity: MarkInterface::class)]
private Collection $marks;
}
在捆綁包中:在主要 Symfony 應用程式中:
namespace Zenith\MyBundle\Entity;
use Zenith\ContractsBundle\MarkInterface;
class Mark implements MarkInterface { ... }
然后,在您的服務中定義原則偵聽器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/524168.html
標籤:php交响乐教义捆
上一篇:在物件中呼叫物件?
