0

名前と姓を持つ User オブジェクトがあります。ユーザーはゴーストとして設定できます。ユーザーがゴーストの場合、他のユーザーが好きな名前を付けられるようにしたいと思います。

|User   |   |Representation|
|-------|   |--------------|
|ID     |   |owner         |
|isGhost|   |ghost         |
|name   |   |name          |
|surname|   |surname       |

owner常にログインしているユーザーをghost指しますUser
owner

私が達成したいのは、ユーザーがゴーストでない場合-(所有者として)見たいUser.nameのですが、ユーザーがゴーストの場合-(所有者User.surnameとして)見たいです.Representation.nameRepresentaion.surname

そのような状況で最も効果的なマッピングを作成するにはどうすればよいでしょうか?

編集: 他に-私はユーザーを参照する関係オブジェクトを持っており、関係を使用しgetUser()て、ユーザーがゴーストかどうかに応じて適切な名前と姓を確認したいと思います。

おそらく、追加のテーブルの代わりに ArrayCollection を使用し、すべての表現をゴースト ユーザーに保存しますか? ゴースト ユーザー 1 人あたりの予測最大表現は約 5 ~ 10 です。あまりない。

4

1 に答える 1

0

2 つのテーブルを設計したのと同じように処理を進める必要があります。OneToManyゴースト上で User と Representation の関係を作成し、 OneToManyUser と Owner の関係をもう 1 つ作成します。

ユーザーにクエリを実行するたびに表現オブジェクトをロードしたくないため、所有側がユーザーであることを確認してください。

次に、->getRepresentations()必要に応じてユーザーに関連付けられた表現をフェッチするだけを実行します。

質問は、ユーザーがゴーストであり、所有者の表現を持っていない場合、何を表示しますか?

この機能を実行できます:

public function getUsername($ownerRepresentation = false)
{

    // user is ghost mode, but we do not have an owner for its representation
    if ($this->isGhost && false === $ownerRepresentation) {
        return '~';
    }

    // user is ghost, and we have provided an owner to search for his representation
    if ($this->isGhost && false !== $ownerRepresentation) {
        $representations = $this->getRepresentations();

        foreach ($representations as $representation) {
            if ($representation->getOwner()->getId() === $ownerRepresentation->getId()) {
                return $representation->getName() . ' ' . $representation->getSurname();
            }
        }

        // no representation found for this owner
        return '~';
    }

    // user is no ghost, we can freely display his real name
    return $this->getName() . ' ' . $this->getSurname();
}
于 2012-08-11T18:21:09.040 に答える