I'm trying to create a plug-in system using Symfony and its bundle system. I know bundle must be independent but for my system, I know my bundle will never be used in another context.
I have a doctrine entity called "Mark" and I want to associate an entity from the main application called "Student" so a student can have a mark from my mark plugin.
For the moment, I only have an entity Mark that is really simple and I don't know how to associate a new field to my Student entity
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;
}
}
And this is my Student entity (simplified) :
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;
}
How can I associate this two entities that one is in an app and the other in a bundle ?
You can create a single foreign key link table to the student table. Connecting table: student | mark | plugin entity where student is the foreign key to the student entity. Mark is the identifier of the plug-in assigned to the student, without using any foreign keys. With this solution, you can add a new student plugin at any time without modifying the student entity.
As @bechir suggested, I used this link form Doctrine documentation and worked as a charm !
EDIT : As the comment said the part that solve the problem is the use of the ResolveTargetEntityListener class from Doctrine tools. It can permit you to inject an Entity that implements the same interface that is needed. I created a bundle for contracts and added every interfaces I need for my entities. Then required this contracts bundle in my main app and my bundle then inject my entities into the doctrine metadata to rewrite the association.
In ContractsBundle
namespace Zenith\ContractsBundle;
interface StudentInterface { ... }
namespace Zenith\ContractsBundle;
interface MarkInterface { ... }
In main Symfony app :
namespace Zenith\Entity;
use Zenith\ContractsBundle\StudentInterface;
class Student implements StudentInterface {
{...}
#[ORM\OneToMany(mappedBy: 'student', targetEntity: MarkInterface::class)]
private Collection $marks;
}
In bundle : In main Symfony app :
namespace Zenith\MyBundle\Entity;
use Zenith\ContractsBundle\MarkInterface;
class Mark implements MarkInterface { ... }
Then, define the doctrine listener in your services.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Symfony compares favorably with other PHP frameworks in terms of reliability and maturity. This framework appeared a long time ago, in 2005, that is, it has existed much longer than most of the other tools we are considering. It is popular for its web standards compliance and PHP design patterns.
https://symfony.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.