0

Hi i had fully successfully setted my entity onetoMany and ManyToOne i generated setters and getters and in user entity it created this method:

user entity:

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

requests entity:

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

And when i using my own custom queries it works good... but i cant figure out how to use this generated function from symfony:

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

I dont know what to pass there... i tried first get user object based on id of user from twig... worked good but the error occur:

$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
Lukas Lukac
  • 7,766
  • 10
  • 65
  • 75
  • This function was created by Symfony2? It should be called: ```public function addFollowee(TB\RequestsBundle\Entity\Requests $followee)``` – stwe Mar 17 '13 at 13:15
  • yes, even if what would then i pass to it? – Lukas Lukac Mar 17 '13 at 13:44
  • I do not know what you're doing there. But if you try to set up a "following" feature for users, look here: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-self-referencing – stwe Mar 17 '13 at 13:58
  • i saw this one bilion of times :) i cant use it because of extra fields in my entity... i am trying set follower via user like addFollowee.. and dont know what i need to put there... now i did it like => request repository and then new Requests() and fill all of the fields... but i am looking for the best way – Lukas Lukac Mar 17 '13 at 14:07

1 Answers1

3

Maybe you should think about what you're trying to before coding it. Grab a pen and a sheet of paper. :)

Tell me if I'm wrong, but here is what I think you're trying to do :

One user can have many "followee". One "followee" can have one user.

So, a OneToMany relation is ok.

Here is how to write it, from the doc :

Requests.php (btw, you should use Request.php)

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

User.php

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

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

Now you can check if you your relation is ok, and update your schema :

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

About getters/setters :

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;
}

User.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)

Now, to set a user to a Request, use

$request->setUser($user);

And to add a Request to a user, use

$user->addRequest($request);
Gmajoulet
  • 700
  • 4
  • 8
  • hm... your code is almost the same... i think... and what is this requests from? $request->setUser($user); its $request=new Request(); ?because then it will throw error with all empty fields except the setUser one ofc. – Lukas Lukac Mar 17 '13 at 14:43
  • Are you kidding me ? From your code : 'TB\RequestsBundle\Entity\Requests' I don't know where it's from, it depends on your app ! It's the entity you want to add to your user ! – Gmajoulet Mar 17 '13 at 15:33
  • ... you did not get me... i meaned everything is clear... just to the last code... you wrote $request->setUser($user); yes sure $request represents a request entity... but how i will 'create,call' it ? Do i have to in my controller write as i said $request=new Request() ? Or somehow different. You get my app exactly. Add followee in controller... based on ours entity. – Lukas Lukac Mar 17 '13 at 19:04
  • I don't know what your app is, and I don't know how you want requests to be created. If you want to create a request through a form, please see the form documentation : http://symfony.com/doc/master/book/forms.html If you want to create a new Request, just use new Requests() and fill it the needed data – Gmajoulet Mar 18 '13 at 13:19