Search code examples
symfonypropel

Symfony2 & Propel: How to query objects ordered by their i18n column


Suppose my schema look something similar to the following -

<?xml version="1.0" encoding="UTF-8" ?>
<table name="vegetables" phpName="Vegetable">
  <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
  <column name="name" type="varchar" required="true" />
  <behavior name="i18n">
    <parameter name="i18n_columns" value="name" />
  </behavior>
</table>

In my controller classes I would do something like this to get Vegetable Objects.

$vegetables = VegetableQuery::create()->joinWithI18n($locale = 'en_US')->find();  //To get all vegetables
//or
$vegetables = VegetableQuery::create()->orderById()->joinWithI18n($locale = 'en_US')->find(); //To get all vegetables order by id
//And then to iterate I would do something like follow -
foreach($vegetables as $vegetable){
  //Do something with $vegetable here
}

Now the question is how should I query if I want to order my query with the i18n column(i.e name) in this case.

I have found an alternative solution is as follow -

$vegetables_i18n = VegetableI18nQuery::create()->orderByName('asc')->filterByLocale('en_US')->joinVegetable()->find();
// And then to iterate all the vegetables I would do something like follow
(foreach $vegetables_i18n as $vegetable_18n){
  $vegetable = $vegetable_18n->getVegetable();
  //Do something $vegetable here
}

I really wonder is there any other alternate ways to write the query for vegetables ordered by i18n column. (to be more precise I want to write a query on above VegetableQuery class rather than on the above VegetableI18nQuery class.)

Really appreciated if you can help.


Solution

  • I found the answer to this by doing like the following-

    $vegetables = VegetableQuery::create()->joinWithI18n($locale = $propel_locale)->orderBy('VegetableI18n.name', 'asc' );