I joust started playing around with Doctrine ORM library, and Im learning about all associations between tables.
So Im stuck with differences in Unidirectional and Bidirectional relation.
As I get it, Unidirectional relation has primary key only on one side, and that side is owning side right? And Bidirectional relation have primary key in both tables and therefore you can have relation from both sides, and set constrains on both sides.
Now, Im reading through Doctrine documentation about relations and there you have: Unidirectional and Bidirectional associations.
But they produce the same SQL, and the same tables with the same primary key-s and constrains. So I dont really see any difference in those two. And both examples have primary key on one side.
As I get it the true Bidirectional relation should have primary keys in both tables pointing to the other table right? And with given example on Doctrine documentation that is not the case. Both examples give the same result and are the same.
So what I did, is this, lets say I have User and Card Entity, and want relation to be OneToOne Bidirectional.
/**
* @Entity
* @Table(name="users")
*/
class User
{
/**
* @Id
* @GeneratedValue
* @Column(type="bigint")
*/
protected $id;
/**
* @OneToOne(targetEntity="Card", mappedBy="User")
* @JoinColumn(name="card_id", referencedColumnName="id")
*/
protected $card;
/**
* @Column(name="user_name", type="string")
*/
protected $userName;
/**
* @Column(name="user_pass", type="string")
*/
protected $userPass;
}
/**
* @Entity
* @Table(name="cards")
*/
class Card
{
/**
* @Id
* @GeneratedValue
* @Column(type="bigint")
*/
protected $id;
/**
* @OneToOne(targetEntity="User", inversedBy="Card")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* @Column(name="post_title", type="string")
*/
protected $cardType;
}
The difference here is I wrote @JoinColumn in both objects/entities. And in Doctrine example there is only one. Now I would get what I think is Bidirectional relation. If i look at EER diagram, I can see one line pointing from user to card, and the other from card to user.
So basicly did I get this right? Is the Doctrine documentation wrong? :D How would Bidirectional OneToOne relation look in EER diagram?
Thanks!