命名空间表上的 Doctrine 关系
我有几个与其他模型有关系的学说模型。我们将这些称为 ItemsOne
、ItemsTwo
、ItemsThree
。每个都有一个定义到Products
学说类(产品表)的关系,并且该关系定义为:
$this->hasMany(
'Models_Products as Products',
array(
'local' => 'id',
'foreign' => 'product_id',
)
);
没有什么不寻常的。
我还有另一个表(prices
),它存储命名空间数据。我的意思是特定的表基于预定义的键(即 Vinyl
、CD
、DVD
)存储数据,因此行会显示一些内容像这样:
media_namespace entity_id quantity unit_price
CD 4 1000 0.99
DVD 4 2500 1.25
CD 7 3750 0.25
Vinyl 15 75 4.25
entity_id
是访问该表的每个模型的 ID。例如,CD/entity_id = 4
指的是 ItemsOne
模型,DVD/entity_id = 4
指的是 ItemsTwo
我想创建一个关系
- 我不知道这是否可以完成 - 而它将在模型中命名。因此,在上面的产品关系片段中,我需要类似的内容(对于 ItemsOne 模型):
$this->hasMany(
'Models_Prices as Prices',
array(
'local' => 'id',
'foreign' => 'entity_id',
)
);
但是,上面的内容将返回 entity_id
与 ItemsOne
表匹配的所有价格。部分正确。在上面的示例中,如果 entity_id = 4
,它将返回 CD
记录,但也会返回 DVD
记录,这不是我需要的。我希望它根据 media_namespace
表过滤数据。所以简而言之我需要的是:
prices.media_namespace = 'CD' AND prices.entity_id = itemsone.id
上面的内容能否体现在 Doctrine 中的一个 hasMany
关系中?我已经搜索过,但找不到有帮助的东西。
任何指点都将不胜感激!
I have several doctrine models that have relationships to other models. Let's call those ItemsOne
, ItemsTwo
, ItemsThree
. Each has a relationship defined to the Products
doctrine class (products table) and the relationship is defined as:
$this->hasMany(
'Models_Products as Products',
array(
'local' => 'id',
'foreign' => 'product_id',
)
);
Nothing out of the ordinary there.
I also have another table (prices
) which stores namespaced data. By that I mean that the particular table stores data based on a predefined key (i.e. Vinyl
, CD
, DVD
) so the rows would show something like this:
media_namespace entity_id quantity unit_price
CD 4 1000 0.99
DVD 4 2500 1.25
CD 7 3750 0.25
Vinyl 15 75 4.25
The entity_id
is the id of each of the models accessing this table. So for instance CD/entity_id = 4
refers to ItemsOne
model, DVD/entity_id = 4
refers to the ItemsTwo
model, etc.
I want to create a relationship - and I don't know if this can be done - whereas it will be namespaced in the model. So in the above snipped for the products relationship I need something like (for the ItemsOne model):
$this->hasMany(
'Models_Prices as Prices',
array(
'local' => 'id',
'foreign' => 'entity_id',
)
);
However the above will return all the prices with the entity_id
matching the ItemsOne
table which is partly correct. In the above example if entity_id = 4
, it will return the CD
record but also the DVD
one which is not what I need. I want it to be filtering the data based on the media_namespace
table. So in short what I need is:
prices.media_namespace = 'CD' AND prices.entity_id = itemsone.id
Can the above be reflected in a hasMany
relationship in Doctrine? I have searched and cannot something that would help.
Any pointers are more than appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 Doctrine 子类。为每个命名空间创建一个子类。这将允许您通过类名引用一组特定的数据。
在 Price 类中,您需要指定以下内容:
让我们假设 ItemsOne 使用命名空间 CD,ItemsTwo 使用命名空间 DVD,而 ItemsThree 使用命名空间 Vinyl。
对于这种情况,您可以向每个类添加以下关系:
ItemsOne:
ItemsTwo:
ItemsThree:
现在,每个Price 关系将只返回您期望的内容。 Doctrine,看到子类,将自动命名您在该表上执行的所有查询。
You need to use Doctrine subclasses. Create a subclass for each namespace. This will let you reference a specific set of data by class name.
In the Price class, you'll need to specify the following:
Let's make the assumption that ItemsOne uses namespace CD, ItemsTwo uses namespace DVD, and ItemsThree uses namespace Vinyl.
For this situation, you would add the following relationship to each class:
ItemsOne:
ItemsTwo:
ItemsThree:
Now, each of the Prices relations will return only what you expect. Doctrine, seeing the subclass, will automatically namespace all the queries you perform on that table.