0

こんにちは、エンティティ onetoMany と ManyToOne を完全に正常に設定しました。セッターとゲッターを生成し、ユーザー エンティティでこのメソッドを作成しました。

ユーザー エンティティ:

    /**
 * @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="followeeuser")
 */
protected $followees;   

リクエストエンティティ:

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

そして、私が独自のカスタムクエリを使用するとうまくいきます...しかし、symfonyから生成されたこの関数を使用する方法がわかりません:

    public function addFollowee(\TB\UserBundle\Entity\User $followee)
{
    $this->followees[] = $followee;
}  

そこに何を渡すかわかりません...最初に小枝からのユーザーのIDに基づいてユーザーオブジェクトを取得しようとしました...うまくいきましたが、エラーが発生しました:

$user->addFollowee($userRepository->find($target_user_id));

Found entity of type TB\UserBundle\Entity\User on association TB\UserBundle\Entity\User#followees, but expecting TB\RequestsBundle\Entity\Requests
4

1 に答える 1

3

コーディングする前に、何をしようとしているのかを考える必要があるかもしれません。ペンと紙を用意します。:)

私が間違っているかどうか教えてください。しかし、あなたがやろうとしていることは次のとおりです。

1 人のユーザーが複数の「フォロワー」を持つことができます。1人の「フォロワー」は1人のユーザーを持つことができます。

したがって、OneToMany の関係は問題ありません。

doc からの書き方は次のとおりです。

Requests.php (ところで、Request.php を使用する必要があります)

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="requests")
 **/
private $user;

ユーザー.php

/**
 * @ORM\OneToMany(targetEntity="Requests", mappedBy="user", cascade={"all"})
 **/
private $requests;

public function __construct()
{
    $this->requests = new \ArrayCollection();
}

これで、関係が正常かどうかを確認し、スキーマを更新できます。

php app/console doctrine:schema:validate
php app/console doctrine:schema:update --force

ゲッター/セッターについて:

Requests.php

public function getUser()
{
    return $this->user;
}

public function setUser(User $user) // Please add a Use statement on top of your document
{
    $this->user = $user;
    return $this;
}

ユーザー.php

public function addRequest(Requests $request)
{
    $this->requests->add($request);
    return $this;
}

public function removeRequest(Requests $request)
{
    $this->requests->removeElement($request);
    return $this;
}

// Get requests and set requests (you know how to write those ones)

ここで、ユーザーをリクエストに設定するには、次を使用します

$request->setUser($user);

そして、ユーザーにリクエストを追加するには、使用します

$user->addRequest($request);
于 2013-03-17T14:14:52.283 に答える