开发者

Mapping two tables to one entity in Doctrine2

I'm looking at using doctrine for an application I'm working on - but after reading the documentation I'm having trouble conceptualizing how to represent the database structure we have in terms of entities.

I have many tables which have partner tables which hold translation data like the following....

Mapping two tables to one entity in Doctrine2

Where I would like to have one Entity (Navigation Element) which had access to the 'label' field depending on what Language I set in my application. The following from the Doctrine documentation seems to suggest that you need to define one (single) table which is used to persist an entity

http://www.doctrine-project.org/docs/orm/2.0/en/reference/bas开发者_JAVA技巧ic-mapping.html By default, the entity will be persisted to a table with the same name as the class name. In order to change that, you can use the @Table annotation as follows:

Or do I need to define two entities and link them (or allow the translation table to inherit from the element table).

And what strategy would I use to always insert a language_id clause to the Join (to ensure I'm pulling the right label for the currently set language). Is this something I would define in the entity itself, or elsewhere?


This seems to suit a One-To-Many Bidirectional association. This is the scenario from that page translated to your situation:

/** @Entity */
class NavigationElement
{
    // ...
    /**
     * @OneToMany(targetEntity="NavigationElementTranslation", mappedBy="navigationElement")
     */
    private $translations;
    // ...

    public function __construct() {
        $this->translations = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

/** @Entity */
class NavigationElementTranslation
{
    // ...
    /**
     * @ManyToOne(targetEntity="NavigationElement", inversedBy="translations")
     * @JoinColumn(name="navigation_element_id", referencedColumnName="id")
     */
    private $navigationElement;
    // ...
}

You could add a getLabel($languageId) method to the NavigationElement entity that searches through the translations to get the correct label:

public function getLabel($languageId) {

    foreach($this->translations as $trans) {
        if($trans->languageId == $languageId)
            return $trans->label;
    }

    throw new InvalidArgumentException();
}

And you could use the following DQL to ensure you only load the translation you want into the $translations property:

$query = $em->createQuery(
    "SELECT ne, net
     FROM Entity\NavigationElement ne
     JOIN ne.translations net WITH net.languageId = :langId"
);
$query->setParameter('langId', $languageId);
$navigationElements = $query->execute();

This situation sounds like one where you would want to cache aggressively. Make sure you look into Doctrine 2's caching mechanisms too.

Also, internationalization can be handled reasonably well in PHP with gettext if you find join tables for translations start to become unmanageable.


I would also direct anyone who has to tackle this same problem to take a look at the following doctrine extension.

http://www.gediminasm.org/article/translatable-behavior-extension-for-doctrine-2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜