使用原则扩展基类,如果我的基类具有 1:n 关联
我正在尝试弄清楚如何在 Symfony 中实现两个包之间的正确分离,但我遇到了一个原则问题。
我有两个捆绑包,一个更通用的捆绑包(充满抽象类),称为“BaseProduct”,然后是该捆绑包的一个实现(它提供了专门化该捆绑包功能的机会),称为“ClientProduct”。
我在“产品”捆绑包中有两个关联的实体。
abstract class Product
{
// One product has many attributes
private $attributes;
private function getAttributes()
{
return $this->attributes;
}
}
abstract class Attribute
{
private $name;
private $value;
private function getName()
{
return $this->name;
}
private function getValue()
{
return $this->value;
}
}
然后是具体的实现
class Product extends BaseProduct/Product
{
}
class Attribute extends BaseProduct/Attribute
{
}
我的问题
首先,我会使用“mappedSuperclass”来执行此操作,但是通过阅读文档,您无法将集合设置为非具体类上的关联(可以理解)。
我正在尝试寻找另一种方法来做到这一点。
这可能吗?因为我的“基类”与另一个“基类”(都是抽象的)有关系。在这种情况下,我是否需要将关系转移到“具体”实现中,或者我可以将它们保持在这个级别(抽象)吗?我的实体定义(“.yml”)未显示,但我打算将它们链接到具体实现(其中实体之间的关联也在其他具体实现之间),并保留没有实体定义的基类。
欢迎任何输入 - 这里的目标是创建一个可以专门化的解耦包。我确信这是一个非常常见的目标,所以我有兴趣看看其他人是如何做到这一点的(如果 github 上有任何捆绑包可以做到这一点,我有兴趣看到它们)。
I am trying to work out exactly how to implement a proper separation between two bundles in Symfony and I've run into a doctrine problem.
I have two bundles, a more general bundle (full of abstract classes) called 'BaseProduct' and then an implementation of that bundle (which offers the chance to specialise the functionality of that bundle) called 'ClientProduct'.
I have an two entities within the 'Product' bundle that are associated.
abstract class Product
{
// One product has many attributes
private $attributes;
private function getAttributes()
{
return $this->attributes;
}
}
abstract class Attribute
{
private $name;
private $value;
private function getName()
{
return $this->name;
}
private function getValue()
{
return $this->value;
}
}
And then a concrete implementation
class Product extends BaseProduct/Product
{
}
class Attribute extends BaseProduct/Attribute
{
}
My Question
First up, I would have used 'mappedSuperclass' to do this, but from reading the docs you cant set up collections as associations (understandably) on non concrete classes.
I'm trying to find another way to do this.
Is this even possible? Because my 'base class' has the relationship to another 'base class' (both abstract). In that case do I need to move the relationship into the 'concrete' implementation or can I keep them at this level (abstract)? My entity definitions ('.yml') are not shown, but I intend to link these to the concrete implementations (where the associations between entities are also between other concrete implementations) and leave the base classes without entity definitions.
Any input welcome - the goal here is to create a decoupled bundle that can be specialised. I'm sure this is a pretty common goal, so I'd be interested to see how others are doing it (if there are any bundles on github doing this I'd be interested to see them).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了类似的情况,我使用的解决方法是提供一个具体的基类,它被简单地视为抽象的(即:基类永远不会在任何地方实例化)。
如果您想强制基类的“抽象性”(这样其他开发人员就不会愚蠢地尝试实例化基类),您可以在其构造函数中抛出异常。
I ran into something similar, and the work-around I used was to provide a concrete base class which is simply treated as if it were abstract (ie: the base class is never instantiated anywhere).
If you wanted to enforce the "abstractness" of your base class (so other developers don't foolishly try to instantiate one), you can throw an exception in its constructor.