Mat.eo
Mat.eo

Reputation: 31

Symfony Entity one-to-one relation

Can you look at the relationship between entities, unfortunately all the time is not working properly. I will be grateful for the help. This is a relation 1 to 1.

User Entity

 /**
    *
    * @ORM\Column(type="string")
    *
    * @ORM\OneToOne(targetEntity="Structure", mappedBy="user")
    */
    protected $structure;

Structure Entity

/**
* @var string
*
* @ORM\Column(name="user", type="string", length=50, unique=false)
* @ORM\OneToOne(targetEntity="User", inversedBy="structure")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;

Upvotes: 1

Views: 5332

Answers (4)

Farshadi
Farshadi

Reputation: 143

Try this one ...

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



 /**
 *@ORM\OneToOne(targetEntity="Structure",mappedBy="user",cascade={"persist","remove"})
 */
private $structure;

Upvotes: 2

Mat.eo
Mat.eo

Reputation: 31

As suggested by @Albeis in a comment:

Try to remove @ORM\Column of both sides...relations itself don't have column..Regards Check http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-one-bidirectional

This solved my problem. The issue was about to remove @ORM\Column of both sides. Thanks for help.

Upvotes: 1

Mehmet Emre Vural
Mehmet Emre Vural

Reputation: 388

    /**
    * @ORM\OneToOne(targetEntity="Core\UserBundle\Entity\Profile", mappedBy="user")
    */
    private $profile;


    /**
    * @ORM\OneToOne(targetEntity="Core\UserBundle\Entity\User", inversedBy="profile")
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id",onDelete="CASCADE")
    */
    private $user;

user_id inside profile entity.
if you set onDelete="CASCADE", if you delete user, profile will deleting automatically by mysql.

Upvotes: 0

Paul
Paul

Reputation: 141829

You can't use mappedBy with @OneToOne. You should use inversedBy on both sides.

Upvotes: 0

Related Questions