0

こんにちは、ここと同じ質問があります:多対多の自己関係と余分なフィールドがありますか? しかし、答えが見つかりません:/最初にManyToOneを試し、他のサイトでOneToManyを試しました...しかし、次のようなものを使用できませんでした

    public function hasFriend(User $user)
{
    return $this->myFriends->contains($user);
}  

この問題があったため:

This function is called, taking a User type $user variable and you then use the contains()      function on $this->myFriends.

$this->myFriends は Requests の ArrayCollection (User とは異なるタイプ) であり、contains() に関する教義のドキュメントから:

The comparison of two elements is strict, that means not only the value but also the type must match.

では、追加のフィールドを使用してこの ManyToMany 関係を解決する最善の方法は何でしょうか? または、戻ってonetomanyとmanytooneの関係を設定した場合、hasFriendメソッドを変更するにはどうすればよいですか? たとえば、ID が ID の配列コレクションにあるかどうかを確認します。

編集:私はこのテーブルを持っています...そして私が必要とするのは:1.私の友達を選択してください...そして私のフォロワー...私が彼と友達であるかどうかを確認してください。(彼は私と友達になることができるので、私は彼と一緒にいる必要はありません... Twitterのように). 私は多くのことを行うことができますが、私のテーブルでわかるように、「閲覧済み」「彼が私を購読した時間」などの追加フィールドが必要です。

そして、このようなクエリを作成し、小枝でチェックできるようにします if (app.user.hasFriend(follower) またはそのようなもの)

           $qb = $this->createQueryBuilder('r')
                  ->select('u')
                  ->innerJoin('UserBundle:User', 'u')
                  ->Where('r.friend_id=:id')
                  ->setParameter('id', $id)
                  ->orderBy('r.time', 'DESC')
                  ->setMaxResults(50);

    return $qb->getQuery()
              ->getResult();

ここに画像の説明を入力

4

2 に答える 2

5

私は追加のフィールドとの多対多の関係を築こうとしていましたが、それを機能させることもできませんでした...フォーラムで読んだこと(場所を思い出せません)は次のとおりでした:

リレーションシップにデータを追加すると、それはリレーションシップではなくなります。それは新しいエンティティです。

そして、それは正しいことです。新しいフィールドで新しいエンティティを作成し、必要に応じてカスタム リポジトリを作成して、必要なメソッドを追加します。

A <--- フィールドを持つ多対多 ---> B

になるだろう

A -- 1 対多 --> C (新規フィールドあり) <-- 1 対多 -- B

そしてもちろん、C は A と B の両方と ManyToOne 関係を持っています。

これを行う方法についてあらゆる場所を検索しましたが、最終的には、それが正しいことであり、データを追加すると、関係がなくなります。

また、contains が通常行っていることをコピーしたり、カスタム リポジトリで上書きして、必要なことを実行したりすることもできます。

これが役立つことを願っています。

于 2013-03-04T15:55:51.243 に答える
3

元の回答とは関係がないため、別の回答を追加します。あなたが投稿した新しい情報を使用して、投稿したテーブル/エンティティを「フォロワー」と呼んでいます。元の実体「ユーザー」。

次の関連付けを作成するとどうなりますか。


namespace Acme\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Acme\UserBundle\Entity\User
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class User
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Acme\FollowerBundle\Entity\Follower", mappedBy="followeduser")
     */
    protected $followers;

    /**
     * @ORM\OneToMany(targetEntity="Acme\FollowerBundle\Entity\Follower", mappedBy="followeeuser")
     */
    protected $followees;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    public function __construct()
    {
        $this->followers = new \Doctrine\Common\Collections\ArrayCollection();
    $this->followees = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add followers
     *
     * @param Acme\FollowerBundle\Entity\Follower $follower
     */
    public function addFollower(\Acme\FollowerBundle\Entity\Follower $follower)
    {
        $this->followers[] = $follower;
    }

    /**
     * Add followees
     *
     * @param Acme\FollowerBundle\Entity\Follower $followee
     */
    public function addFollowee(\Acme\FollowerBundle\Entity\Follower $followee)
    {
        $this->followees[] = $followee;
    }    

    /**
     * Get followers
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getFollowers()
    {
        return $this->followers;
    }

    /**
     * Get followees
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getFollowees()
    {
        return $this->followees;
    }
}


namespace Acme\FollowerBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Acme\FollowerBundle\Entity\Follower
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Follower
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy="followers")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $followeduser;

    /**
     * @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy="followees")
     * @ORM\JoinColumn(name="followee_id", referencedColumnName="id")
     */
    protected $followeeuser;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set followeduser
     *
     * @param Acme\UserBundle\Entity\User $followeduser
     */
    public function setFolloweduser(\Acme\UserBundle\Entity\User $followeduser)
    {
        $this->followeduser = $followeduser;
    }

    /**
     * Get followeduser
     *
     * @return Acme\UserBundle\Entity\User 
     */
    public function getFolloweduser()
    {
        return $this->followeduser;
    }

    /**
     * Set followeeuser
     *
     * @param Acme\UserBundle\Entity\User $followeeuser
     */
    public function setFolloweeuser(\Acme\UserBundle\Entity\User $followeeuser)
    {
        $this->followeeuser = $followeeuser;
    }

    /**
     * Get followeeuser
     *
     * @return Acme\UserBundle\Entity\User 
     */
    public function getFolloweeuser()
    {
        return $this->followeeuser;
    }
}

これでうまくいくかどうかはわかりません。実際にテストする時間はあまりありませんが、うまくいかない場合は、途中であると思います. 多対多は必要ないので、2 つの関係を使用しています。ユーザーは多くのフォロワーを持つことができ、フォロワーは多くのユーザーをフォローできることを参照する必要がありますが、「ユーザー」テーブルは同じテーブルであるため、2 つの関係を作成しました。同じエンティティを参照するだけですが、異なるものを参照します。

それを試して、何が起こるか実験してください。次のようなことができるはずです。


$user->getFollowers();

$follower->getFollowedUser();

and you could then check if a user is being followed by a follower whose user_id equals $userThatIwantToCheck

and you could search in Followers for a Follower whose user = $user and followeduser=$possibleFriend

于 2013-03-04T21:38:17.653 に答える