Doctrine 1.2 automatically joins i18n?


I would like to extend the behavior of i18n so that it automatically includes the translation table in any type of query (DQL, relationships, getTable).
In addition, he needs to determine the default language setting, so when I execute a query without a set of languages, it returns to the default language.
Note. I am looking for generalized behavior, so this applies to all objects in the i18n model, and not to writing and overriding for each of the classes.

Here is an example:
table product โ†’ id, category_id, price ...
table product_translation โ†’ id, lang, name, description ...

With the current solution, when I do something like this: Doctrine_Core::getTable('Product')->findAll() , it gets all products without attaching to translations.
So in the controller I have to loop all the records and reuse the translated values, $product->name = $product->Translation['en']->name

I would like something like this:

  • Doctrine_Core::getTable('Product')->findAll() it should get the combined values โ€‹โ€‹for lang = 'en'
  • Doctrine_Core::getTable('Product)->findAll('en') as above
  • It should also work with relationships, so for example, if I have a User class that has many $user->Products , it should return a collection with translations enabled.
  • Also something like $user->Products('en') should return a collection for other (non-standard) languages
  • Magic functions would also be nice (if possible) ... something like Doctrine_Core::getTable('Product')->getByCategoryAndLang(1,'en')

Can anyone help? I look at patterns and behavior, I think this is the way to go, but I donโ€™t know how to implement this.

EDIT: I see that there is not much interest in this, so let me try a simpler question. As usual, you get i18n fields through relationships. For example, how can I call $user->Products and get products with uploaded translations?

+7
source share
1 answer

I think you do not need to propagate standard Doctrine behavior unless you want this to be fully automatic. But still, you can try to do this, just like we do - we use a DAO (data access object) that returns a specific Doctrine object (a view of the Doctrine table):

 \DAO::get('Some\Namespace\Classname') 

where Classname stands for the table described by the PHP class model. Our DAO class creates an instance of Classname , which is enclosed in a proxy (see "Design Patterns").

Outside of the table class model, we create another class for this table, which stands above the table model and manipulates this model. In this class, we write methods such as getProducts($args) , getProduct($id) , getProductsByCategory($catId) , etc.

I think this is what you are looking for ...

In the getProducts($args) method, then you can implement ->leftJoin() inside the DQL, which will join the translation table by specifying the $lang identifier in the $args parameter. Simple example (not verified):

 class Products extends \DAO { public function save($item) { $item->save(); } public function getProducts($args = array()) { $order = array('p.id'); $result = \Doctrine_Query::create() ->from('Some\Namespace\Product p') ->where('1 = 1'); if(!empty($args['lang'])) { $result = $result->leftJoin('Some\Namespace\ProductTranslation pt ON pt.product_id = p.id AND pt.language = ?', $args['lang']); } $result = $result->orderBy($order); $result = $result->execute(); return $result; } } 

Then calling

 $products = DAO::get('Some\Namespace\Product')->getProducts(array('lang' => 1)); 

You get all products with English translations uploaded ...

It is not so automated, and you have to write your own DAO class for each model, but this is a good approach, since you have a different data definition class (model) and a data management class (controller) needed for an MVC / MVP object-oriented application architecture ...

+1
source

All Articles