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.

有帮助吗?

解决方案

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;
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top