FuelPHP ORM 可以处理这个数据库设计吗?

发布于 2024-12-23 06:54:09 字数 736 浏览 3 评论 0原文

我想知道 Fuel ORM 是否可以处理此处描述的设计:

http://www .codeproject.com/KB/aspnet/LocalizedSamplePart2/normalizedSchema.gif

必须生成并执行以下 SQL 或类似的内容:

SELECT CultureId
FROM Culture WHERE CultureName = @CultureName

SELECT I.ItemId, IL.[Name], IL.[Description],
I.Price, CL.[Name], CL.[Description]
FROM Item I
INNER JOIN Item_Locale IL ON I.ItemId = IL.ItemID AND IL.CultureId = @CultureIdD
INNER JOIN Category C ON I.CategoryId = C.CategoryId
INNER JOIN Category_Locale CL ON C.CategoryID = CL.CategoryId AND CL.CultureId = @cultureId
WHERE I.SellerId = @SellerID

或者我必须使用普通查询?

请指教。

I'm wondering if Fuel ORM can handle the design described here:

http://www.codeproject.com/KB/aspnet/LocalizedSamplePart2/normalizedSchema.gif

The following SQL, or maybe something similar, must be generated and executed:

SELECT CultureId
FROM Culture WHERE CultureName = @CultureName

SELECT I.ItemId, IL.[Name], IL.[Description],
I.Price, CL.[Name], CL.[Description]
FROM Item I
INNER JOIN Item_Locale IL ON I.ItemId = IL.ItemID AND IL.CultureId = @CultureIdD
INNER JOIN Category C ON I.CategoryId = C.CategoryId
INNER JOIN Category_Locale CL ON C.CategoryID = CL.CategoryId AND CL.CultureId = @cultureId
WHERE I.SellerId = @SellerID

Or I must use normal queries?

Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

归途 2024-12-30 06:54:09

您需要按照已给出的链接中的描述配置主键和外键

从给定关系的示例来看:

  • Item HasOne Item_Locale
  • Item_Locale BelongsTo Item
  • Item HasMany Categories
  • Category BelongsTo Item
  • Category HasOne Category_Locale
  • Category_Locale BelongsTo Category

的查询看起来像这样:

$items = Model_Item::query()
    ->related('locale')
    ->related('categories')
    ->related('categories.locale')
    ->where('SellerID', $sellerID)
    ->get();

我假设区域设置在运行时不会更改,但可以不要进行硬编码。在这种情况下,您需要类似以下内容。首先设置默认条件,这些条件始终是关系的一部分。这些需要添加到模型中关系的定义中,如 Model_Item 中的以下示例。

protected static $_has_one = array(
    'locale' => array(
        'model_to' => 'Model_Item_Locale',
        'key_from' => 'ItemId',
        'key_to' => 'ItemId',
        'conditions' => array(
            'join_type' => 'inner',
            'where' => array(),
        ),
    ),
);

我还无法添加实际的 where 条件,因为它必须是动态的,并且您不能在类属性定义中添加它。因此,我们将在类加载的毫秒添加它:

public static function _init()
{
    $culture_id = ; // get that from somewhere
    static::$_has_one['locale']['where'][] = array('CultureId', '=', $culture_id);
}

这将每次添加到连接部分,紧邻主键-外键匹配。

该查询将返回一组项目,这些项目将在属性中包含区域设置信息,并具有一组类别,这些类别都有自己的区域设置属性来描述它们。

foreach ($items as $i)
{
    echo $i->locale->Name;
    foreach ($i->categories as $c)
    {
        echo $c->locale->Name;
    }
}

You'll need to configure the primary keys and foreign keys as described in the link already given.

Judging by the example given the relations are:

  • Item HasOne Item_Locale
  • Item_Locale BelongsTo Item
  • Item HasMany Categories
  • Category BelongsTo Item
  • Category HasOne Category_Locale
  • Category_Locale BelongsTo Category

The query for would look something like this:

$items = Model_Item::query()
    ->related('locale')
    ->related('categories')
    ->related('categories.locale')
    ->where('SellerID', $sellerID)
    ->get();

I am assuming the locales don't change during runtime, but can't be hardcoded. In which case you need something like the following. Start with setting up the default conditions that are always part of the relations. These need to be added to the definition of the relations in the models, like the following example that would be in Model_Item.

protected static $_has_one = array(
    'locale' => array(
        'model_to' => 'Model_Item_Locale',
        'key_from' => 'ItemId',
        'key_to' => 'ItemId',
        'conditions' => array(
            'join_type' => 'inner',
            'where' => array(),
        ),
    ),
);

I couldn't add the actual where condition yet as it has to be dynamic and you can't have that in a class property definition. Thus we'll add it the millisecond the class is loaded:

public static function _init()
{
    $culture_id = ; // get that from somewhere
    static::$_has_one['locale']['where'][] = array('CultureId', '=', $culture_id);
}

This will be added to the join-on part each time, next to the primarykey-foreignkey matching.

The query will return a set of items which will have the locale info inside a property and have an array of categories which all have their own locale property describing them.

foreach ($items as $i)
{
    echo $i->locale->Name;
    foreach ($i->categories as $c)
    {
        echo $c->locale->Name;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文