Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Change language in a loop
  • Hello,

    For generate my sitemap of my multilanguage site, I would like to execute a query for several languages.

    I have a loop with all my languages.
    In this this I set the language : Config::set('language', $lang)
    and then I do Model_product::query()->related('translate')->get();

    The result of this query for each languages is the same : the first one.

    Why it doesn't work with a Config::set('language', $lang) ?
  • I can't comment, I don't see any relation between a config key and an ORM query? How does the ORM know what language strings you want from the DB?
  • Sorry, I wasn't clear.
    My model has a "has_one" relation : 
    protected static $_has_one = array(
    'translate' => array(
    'model_to' => 'Model_Brand_Description',
    'key_from' => 'id',
    'key_to' => 'brand_id',
    'cascade_save' => false,
    'cascade_delete' => false,
    'conditions' => array(
    'where' => array(array('lang_code', '=', '%LANG_CODE%')),
    'order_by' => array('name' => 'ASC')
    ),
    ),
    );


    And in the file "fuel/app/classess/database/query/builder/join.php", where class Database_Query_Builder_Join extends \Fuel\Core\Database_Query_Builder_Join, the is a : 

    $conditions[] = $db->quote_identifier($c1).$op.' '.(is_null($c2)?'NULL':$db->quote_identifier(str_replace('%LANG_CODE%', \Config::get('language'), $c2)));

    That's why there is a relation between ORM query and the config.
  • Looks like a very complex way of achieving this, but I don't immediately see why that would be a problem.

    All your code is in a controller, you don't run queries in a view?

    If you enable the profiler and enable database profiling, what does the generated SQL like like?
  • There is no code in the view.

    In the profiler panel, there are the two corrects queries : 

    SELECT ...
    FROM `brands` AS `t0` 
    LEFT JOIN `brand_descriptions` AS `t1` ON (`t0`.`id` = `t1`.`brand_id` AND `t1`.`lang_code` = 'en') 
    LEFT JOIN `products` AS `t2` ON (`t0`.`id` = `t2`.`brand_id`) 
    LEFT JOIN `product_categories` AS `t3` ON (`t2`.`id` = `t3`.`product_id` AND `t3`.`link_type` = 'M') 
    LEFT JOIN `origins` AS `t4` ON (`t2`.`origin_id` = `t4`.`id`) 
    LEFT JOIN `origin_descriptions` AS `t5` ON (`t4`.`id` = `t5`.`origin_id` AND `t5`.`lang_code` = 'en') 
    WHERE ...
    ORDER BY ...







    SELECT ...

    FROM `brands` AS `t0` 
    LEFT JOIN `brand_descriptions` AS `t1` ON (`t0`.`id` = `t1`.`brand_id` AND `t1`.`lang_code` = 'ja') 
    LEFT JOIN `products` AS `t2` ON (`t0`.`id` = `t2`.`brand_id`) 
    LEFT JOIN `product_categories` AS `t3` ON (`t2`.`id` = `t3`.`product_id` AND `t3`.`link_type` = 'M') 
    LEFT JOIN `origins` AS `t4` ON (`t2`.`origin_id` = `t4`.`id`) 
    LEFT JOIN `origin_descriptions` AS `t5` ON (`t4`.`id` = `t5`.`origin_id` AND `t5`.`lang_code` = 'ja') 
    WHERE ...
    ORDER BY ...



    In PHPMyadmin, this queries return the good datas, but not in the code ... 
  • HarroHarro
    Accepted Answer
    If that is what the profiler reports, than those queries have actually run.

    I expect the issue is related to ORM object caching, because you are not using WHERE clauses in the ORM query, you hacked a variable where in using the condition.

    Since both queries select the same products and origins records, instead of hydrating the result, the objects are returned rom cache for performance reasons. You could try to include

        ->from_cache(false)->

    into your ORM query, and see if that makes a difference.

    If that doesn't work, you might want to ping Steve ("uru") on IRC, he knows a lot more of the ORM's inner workings that I do.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion