Ask your Symfony questions! Pay money and get answers fast! (more info)

Doctrine with I18N Symfony

  • SOLVED

I'm currently playing with I18n and I'm facing an issue.

Here is my schema:


RatingAttribute:
actAs:
Timestampable: ~
I18n:
fields: [name, genre, level1, level2, level3, level4, level5]
columns:
id:
type: integer(6)
notnull: true
primary: true
autoincrement: true
name:
type: string(50)
notnull: true
...


Here is my RatingAttributeTable class:

public function findOneById($id, $lang="en")
{
$query = $this->createQuery('r')
->leftJoin('r.Translation t WITH t.lang = ?', $lang)
->where("r.id = ?", $id);

return $query->fetchOne();
}



And here is my test:

$rating = RatingAttributeTable::getInstance()->findOneById(8);
echo "<pre>";
print_r($rating->toArray());
echo "</pre>";



This returns the following:

Array
(
[id] => 8
[created_at] => 2011-03-19 15:39:19
[updated_at] => 2011-03-19 15:39:19
[Translation] => Array
(
[en] => Array
(
[id] => 8
[name] => Decoration
[genre] =>
[level1] => Disappointing
[level2] => Correct
[level3] => Pleasant
[level4] => Nice
[level5] => Outstanding
[lang] => en
)
)
)


I'd like to be able to access $rating object as usual for instance
echo $rating->getLevel1();

However I obviously can't access my level1 like that. The only way to get my level1 is by doing that:
$rating->getTranslation()->en->level1;

I doubt this is the right way to do it, I tried to modify the query in several ways but still can't get the response I want. What is wrong? Isn't symfony supposed to bring what is under Translation->en at the same level as id, created_at and updated_at?

Answers (2)

2011-03-19

Martin Ratinaud answers:

Hi,

have you tried $rating->getLevel1();

It should return the level1 value of the current user's culture.
You can check your BaseRating Class in lib/model/base to see all functions you can use on this specific object (in the comments of the main function)


Jonathan comments:

Hello,

I indeed tried <em>$rating->getLevel1();</em>, from my understanding, it's the way it's supposed to work. ([[LINK href="http://www.symfony-project.org/jobeet/1_4/Doctrine/en/19"]][[/LINK]]

If I remove the "actAs: I18n", the $rating->getLevel1() produces the desired effect.


Martin Ratinaud comments:

Hi Jonathan,

the problem seems to be in your
public function findOneById($id, $lang="en")

{

$query = $this->createQuery('r')

->leftJoin('r.Translation t WITH t.lang = ?', $lang)

->where("r.id = ?", $id);



return $query->fetchOne();

}


FirstOf All, findOneById already exists and you should not override it as it already have the relation to the translation in it.

Thus, you should <strong>delete your <em>findOneById </em>function</strong> in the RatingTable file and use


$rating = RatingAttributeTable::getInstance()->findOneById(8);
echo "<pre>";
print_r($rating->toArray());
echo "</pre>";

2011-03-19

Gabor Fasi answers:

Create a function named getLevel1() in your RatingAttribute class that returns `$this->getTranslation()->en->level1;`.

If you want the language to be dynamic to the current user, you need to inject the current session into the object, and call its getCulture function.