Welcome Guest, Not a member yet? Register   Sign In
DataMapper: Multilang site
#1

[eluser]scherman[/eluser]
Hello people, i am developing a multilang site with Codeigniter and WanWizard's Datamapper and i can't figure out how to do something.

First of all, i am developing the Article's CRUD. This are my tables:
- Articles[id, title, created, updated]
- ArticlesLang[id, article_id, lang, title, text, created, updated]

So, i want to do the general article's list, like:
Quote:- id:1
- title: 'Example1'
- languages: 'English|Spanish|French'

- id:2
- title: 'Example2'
- languages: 'Spanish'

Do you understand? I hope so.
Let's see, i get the articles at this line:
Code:
$articles->include_related('articlelang')->group_by('id')->get();

And on the view, i do this:
Code:
<?php foreach($articles->all as $article): ?>
     <td>&lt;?php echo $article->id ?&gt;</td>
     <td>&lt;?php echo $article->title ?&gt;</td>
     <td>&lt;?php echo $article->articlelang_lang ?&gt;/*<--- HERE IS MY PROBLEM*/</td>
&lt;?php endforeach ?&gt;

For example, the article with the id=1 is available in three languages, so it should have 3 objects in $article->articlelang. How can i do to get this objects?
Because when i do $article->articlelang_lang it only returns me the first lang.

Do you understand where is my problem?

Please, i hope you can help me. This is the first time i'm doing multilang's stuffs, so, i don't know if i am doing it right, on the best way, so, if you know a better way to do that, i'll be very grateful if you explain me how.

Thanks to everybody, and sorry for my english.
#2

[eluser]WanWizard[/eluser]
include_related() generates a join, so you will have all problems related to joins in a one-to-many relation, namely replication. It does not return a hierarchy of objects you can loop through, it flattens the result.
#3

[eluser]scherman[/eluser]
Thanks WanWizard.

So, how should i do this? I can't figure out how to do this...
#4

[eluser]scherman[/eluser]
I'd like to avoid something like this on the view:

Code:
foreach($article->articlelang->get() as $translation){
    //etc.
}

Because it'd generate A LOT of database queries. Because at the list, i have 10 articles wich 2 or 3 translation per article.

Is there another way to do this?
#5

[eluser]WanWizard[/eluser]
Which is exactly why Datamapper using standard joins, and not maintain the object hierarchy when you run queries like this.

How would you do it with a standard query?

You're query would return something like
Code:
Field1 FieldA
Field1 FieldB
Field1 FieldC
Field2 FieldD
Field2 FieldE

So you loop over the result, keep track of the value of the first field in the result, and generate a 'break' when the value changes.




Theme © iAndrew 2016 - Forum software by © MyBB