Let me begin this reply by saying i really appreciate all the effort you're 
putting into this!

And i'm also happy to say i finally have it working.

 > What goes wrong in the RSM-Builder you use: the columnname word_id (from 
the second use of the word-table) is replaced by the column_name 
traslation_id, but that columnname already exists.

You were absolutely correct. Well spotted!
As soon as i changed this i started getting results.

For a while i've been trying to make this work with the Words entity as the 
Root Entity, but despite all my efforts i've been unable to successfully 
add the translation to the results.
In the end i approached it differently and that part of my code now looks 
like this:

        $rsm = new ResultSetMappingBuilder($this->_em);
        
$rsm->addRootEntityFromClassMetadata('Application\Entity\WordTranslations', 
'wtr');
        $rsm->addJoinedEntityFromClassMetadata('Application\Entity\Words', 
'w', 'wtr', 'originalword');
        
$rsm->addJoinedEntityFromClassMetadata('Application\Entity\Wordtypes', 
'wt', 'w', 'wordtype');
        $rsm->addJoinedEntityFromClassMetadata('Application\Entity\Words', 
't', 'wtr', 'translationword',
            "word_id"       => "translated_id",
            "word"          => "translation",
            "wordtype_id"   => "translation_wordtype_id",
            /* other fields */
            )
        );

So, the root entity is now the one that joins the word and it's 
translation. I didn't (really) have to change my native query for this: i 
only had to add a column from the word_translations table so that entity 
would be created, otherwise i had no results at all (which makes sense, 
since it has become the root entity).

 

> Your word_translations table is a n:n-coupling-table between  2 
> Word-items. You don't need a WordTranslations-entity (you can make that 
> extra entity, but you normally only do so when extra attributes are added 
> to the coupling table, as for instance in the case of a JobContract between 
> Emplyee-entities and Company-entities. In that case you split the 
> ManyToMany association in two OneToMany/ManyToOne associations).
>
> So you have a Word-entity, that has attributes $type (= a WordType 
> value-object) and $translations (= a collection of other Word-entities). 
> And you have a WordType-entity (for the different values a wordtype can 
> have). No need for a third entity. 


Ah, that is valuable information! I just went along with what doctrine (i'm 
kinda new to it) had generated for me, but what you outline here looks much 
cleaner. It's just a coupling table - no extra's, so i'll definitely give 
that a try as well.

About the random row : it's a one-time per (user) request only. It will 
select quite a bit of information .. i'm curious about memory usage and 
such, but i'll make sure to give it a try and compare the results. The 

->setFirstResult(rand(0, $count - 1))->setMaxResults(1)->getSingleResult();


looks like a nice trick!

One thing is for sure: it will keep my code much cleaner :-)


Just curious for what you are gonna use the random selection of words of 
> different types. 


Nothing so sophisticated as what it made you think of i'm afraid :-)

It's for a language application in which i want to present the user with a 
random word, so (s)he can practice the translations. You might be wondering why 
i need the translation as well at that point: this is because a word can have 
multiple translations (obviously) and i want the required translation to be 
random too. (of course correct translations that differ from the chosen one 
won't be regarded as an error, but in this case the user will be encouraged to 
give a different translation).

The type is also present because - for example - in case of a noun i will be 
able to ask the user to specify extra's such as the gender (if that applies for 
the selected language) or the plural form.

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to