Answer the question
In order to leave comments, you need to log in
How to make a connection of one table field with several entities (entity)?
I ran into a problem that baffled me. It seems that everything just seemed - you need to make a multi-level menu manager in the admin panel. The problem is that each of the menu items takes the link itself (URL) from the entity (Entity) that is attached to this menu item. There are several types of entities, respectively, and there are also several classes of the menu item (MenuItem):
/**
* @ORM\Entity
* @ORM\Table("menus")
* @Gedmo\Mapping\Annotation\Tree(type="nested")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discriminator", type="string")
* @ORM\DiscriminatorMap({
* "simple" = "MenuItemSimple",
* "page" = "MenuItemPage",
* "article" = "MenuItemArticle",
* "category" = "MenuItemCategory"
* })
* @ORM\Entity(repositoryClass="App\Repository\MenuItemRepository")
*/
abstract class MenuItem
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\Column(type="string", length=256, nullable=true)
*/
private $titleLink;
/**
* Href property for link
*
* @ORM\Column(type="string", length=256, nullable=true)
*/
private $hrefLink;
public function getTitleLink(): ?string
{
return $this->titleLink;
}
public function setTitleLink(?string $titleLink): self
{
$this->titleLink = $titleLink;
return $this;
}
public function getHrefLink(): ?string
{
return $this->hrefLink;
}
public function setHrefLink(?string $hrefLink): self
{
$this->hrefLink = $hrefLink;
return $this;
}
}
/**
* @ORM\Entity
*/
class MenuItemCategory extends MenuItem {
/**
* @ORM\ManyToOne(targetEntity="Category")
*/
private $entity;
public function getEntity(): ?Category
{
return $this->entity;
}
public function setEntity(?Category $entity): self
{
$this->entity = $entity;
return $this;
}
}
/**
* @ORM\Entity
*/
class MenuItemSimple extends MenuItem {}
Answer the question
In order to leave comments, you need to log in
You are already using DiscriminatorMap
which is the correct solution for this problem. Nothing now prevents you from having separate properties in inherited classes containing links to specific entities specific to each specific type of menu item. You can also declare getEntity()
/ setEntity()
in a class as MenuItem
abstract, and implement them in child classes. Of course, you will not be able to set types at the language level, but no one will stop you from using PHPDoc to create type hints.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question