0

I'm using Doctrine 2. I have an associative entity which points to two entities. Both are part of the composite key.

My problem is that JoinColumn only allows to specify the physical name, not the logical name. I need the two to be different. Everything else can be mapped, so I feel like I missed something.

/**
 * @ORM\Entity()
 * @ORM\Table(name="user_friend")
 */
class UserFriend
{
    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="User", inversedBy="friends")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
     **/
    private $user;

    /**
     * @ORM\Id
     * @ORM\ManyToOne[...]
     **/
    private $friend;

    private $connectedDate;
}

By the way, I tried separating the relationships from the id columns (user, userId), but that caused problems with cascade persists.

Any solution or perhaps a different route would be appreciated.

Yogeshree Koyani
  • 1,649
  • 10
  • 19
afilina
  • 878
  • 1
  • 11
  • 25
  • what do you mean by "physical name and logical name"? – xurshid29 Oct 07 '15 at 18:09
  • @xurshid29 Physical name is the one in the actual database, logical name is the one in your code, hence the mapping. – afilina Oct 08 '15 at 00:24
  • What name exactly do you want to change? The thing that corresponds to user_id is UserFriend->user->id . There is no UserFriend->userId in the code. – greg0ire Oct 08 '15 at 07:28
  • @greg0ire I was hoping not being forced to do an extra join in my query to get the user's id. Doing a join (userFriend.user) to fetch just the ID that I already possess in an unnecessary step. If I understand correctly, there's no way to map the user_id column in this associative entity? – afilina Oct 08 '15 at 13:02
  • I don't think so, no. But maybe doctrine is clever enough to get the `id` from the `user_id` column when you call `userFriend.user`. BTW, you should probably rename `UserFriend` to `FriendShip`, it makes things easier to understand IMHO – greg0ire Oct 08 '15 at 16:20
  • Looks like your question was already asked here : [Can you get a foreign key from an object in Doctine2 without loading that object?](http://stackoverflow.com/questions/8211679/can-you-get-a-foreign-key-from-an-object-in-doctine2-without-loading-that-object) – greg0ire Oct 08 '15 at 16:21
  • Also, looks like you don't have to do anything, it is exactly as I said. – greg0ire Oct 08 '15 at 16:22

0 Answers0