命名空间表上的 Doctrine 关系

发布于 2024-12-19 06:22:54 字数 1555 浏览 3 评论 0原文

我有几个与其他模型有关系的学说模型。我们将这些称为 ItemsOneItemsTwoItemsThree。每个都有一个定义到Products 学说类(产品表)的关系,并且该关系定义为:

$this->hasMany(
    'Models_Products as Products',
    array(
     'local'   => 'id',
     'foreign' => 'product_id',
    )
);

没有什么不寻常的。

我还有另一个表(prices),它存储命名空间数据。我的意思是特定的表基于预定义的键(即 VinylCDDVD)存储数据,因此行会显示一些内容像这样:

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_idItemsOne 表匹配的所有价格。部分正确。在上面的示例中,如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断爱 2024-12-26 06:22:54

您需要使用 Doctrine 子类。为每个命名空间创建一个子类。这将允许您通过类名引用一组特定的数据。

  • 型号_价格类
    • 子类 Models_Price_CD
    • 子类 Models_Price_DVD
    • 子类 Models_Price_Vinyl

在 Price 类中,您需要指定以下内容:

public function setUp()
{
    parent::setUp();

    $this->setSubclasses(
        array(
            'Price_CD' => array(
                'media_namespace' => 'CD',
            ),
            'Price_DVD' => array(
                'media_namespace' => 'DVD',
            ),
            'Price_Vinyl' => array(
                'media_namespace' => 'Vinyl',
            ),
        )
    );
}

让我们假设 ItemsOne 使用命名空间 CD,ItemsTwo 使用命名空间 DVD,而 ItemsThree 使用命名空间 Vinyl。

对于这种情况,您可以向每个类添加以下关系:

ItemsOne:

$this->hasMany(
    'Models_Price_CD as Prices',
    array(
     'local'   => 'id',
     'foreign' => 'entity_id',
    )
);

ItemsTwo:

$this->hasMany(
    'Models_Price_DVD as Prices',
    array(
     'local'   => 'id',
     'foreign' => 'entity_id',
    )
);

ItemsThree:

$this->hasMany(
    'Models_Price_Vinyl as Prices',
    array(
     'local'   => 'id',
     'foreign' => 'entity_id',
    )
);

现在,每个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.

  • class Models_Price
    • subclass Models_Price_CD
    • subclass Models_Price_DVD
    • subclass Models_Price_Vinyl

In the Price class, you'll need to specify the following:

public function setUp()
{
    parent::setUp();

    $this->setSubclasses(
        array(
            'Price_CD' => array(
                'media_namespace' => 'CD',
            ),
            'Price_DVD' => array(
                'media_namespace' => 'DVD',
            ),
            'Price_Vinyl' => array(
                'media_namespace' => 'Vinyl',
            ),
        )
    );
}

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:

$this->hasMany(
    'Models_Price_CD as Prices',
    array(
     'local'   => 'id',
     'foreign' => 'entity_id',
    )
);

ItemsTwo:

$this->hasMany(
    'Models_Price_DVD as Prices',
    array(
     'local'   => 'id',
     'foreign' => 'entity_id',
    )
);

ItemsThree:

$this->hasMany(
    'Models_Price_Vinyl as Prices',
    array(
     'local'   => 'id',
     'foreign' => 'entity_id',
    )
);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文