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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$7
Doctrine with I18N

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?

This question has been answered.

Jonathan | 03/19/11 at 10:47am Edit

Previous versions of this question: 03/19/11 at 10:50am

(4) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    03/19/11
    12:17pm
    Gabor Fasi says:

    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.

  • avatar
    Last edited:
    03/19/11
    6:06pm
    Martin Ratinaud says:

    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)

  • avatar
    Last edited:
    03/19/11
    1:05pm
    Jonathan says:

    Hello,

    I indeed tried $rating->getLevel1();, from my understanding, it's the way it's supposed to work. (

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

  • avatar
    Last edited:
    03/19/11
    1:53pm
    Martin Ratinaud says:

    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 delete your findOneById function in the RatingTable file and use


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

This question has expired.



Jonathan had additional discourse to offer.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.