3

I've went through the Jobeet Tutorial for integrating Zend Lucene into a symfony (1.4.8) project in order to add search capabilities into my frontend of my site (through indexing). Among others, the key concept is to use updateLuceneIndex during model's save action (needs to be overridden) in order to create/update the index of the specific entry.

My model has i18n fields, some of which (i,e name, title) I want to be inserted in the index. Everything works as expected but when it comes to save the i18n fields into the index all I get is blank values ($this->getName() returns empty string). I'm inspecting the created index with the Luke.

I ended up that this has nothing to do with the Zend Lucene but with symfony. It seems that during save the information for i18n fields isn't available (or is it?). I've also tried hook up the update during preSave(), postSave() but no avail.

So I want to ask how am I supposed to get my model's i18n field values during the save action in order to update the index accordingly?

Important note: This happens only during doctrine:data-load task. If I manually insert or update a record the index gets updated accordingly.

One last related question. It would be nice if I could save different keywords for each of the languages of the field of the model. How can I get the different values for each field's language inside the model?

denys281
  • 2,004
  • 2
  • 19
  • 38
pankar
  • 1,623
  • 1
  • 11
  • 21
  • In order to answer my second question I've found out that using `$this->Translation['']['']` successfully returns the cultured field's value. Even this though doesn't work in `data-load` task (always in the `save` model's method) – pankar Oct 30 '11 at 09:36

1 Answers1

2

The reason of this strange behaviour of Symfony is that when you are loading fixtures via cli, it has no context loaded (for instance when you try to get context instance sfContext::getInstance(), youll get "context instance does not exists" error exception).

With no context instance available, there is no "current culture" and with no current culture, there is no value of i18n fields.

The symfony context actualy supports all I18N functionalities with current User culture ($currentUserCulture = sfContext::getInstance()->getUser->getCulture()).

This all means 2 things:

  • You cant use symfony "current user culture" capabilities while you are in cli session
  • If you needs to have sfContext::getInstance() somewhere in your code (especialy in the models), you have to close it into condition to avoid any troubles with unexpected and hard to find exceptions while in cli

Example of getting current culture in model class (it will not pass condition while in cli):

if (sfContext::hasInstance()) {
  sfContext::getInstance()->getUser()->getCulture();
}

So when you cant use Symfony i18n shortcuts (like $record->getName()), you have to work around it. In Your symfony1-doctrine models you always have $this->Translation object available. So you can access your translation values object via something like $this->Translation[$culture].

Its up to you to work with that, you can use your default culture $this->Translation[sfConfig::get('sf_default_culture')], or interate trough all your supported cultures from some global configuration (i recommends you to set it in one of your configuration files globaly accross of all apps - maybe /config/app.yml).

Example of getting $record Translation object in any situations:

if (sfContext::hasInstance()) {
    $translation            = $this->Translation[sfContext::getInstance()->getUser()->getCulture()];
}
else {
    $translation            = $this->Translation->getFirst();
    // or: $translation         = $this->Translation[$yourPreferedCulture];
}
// you can access to modified fields of translation object
$translationModified    = $translation->getModified();
palmic
  • 1,846
  • 2
  • 20
  • 30