简体   繁体   中英

symfony doctrine inconsistent mapping

I have this error in the symfony2 console:

The mappings Cpj\ProjectsBundle\Entity\Project#id_user and Cpj\UserBundle\Entity\User#projects are inconsistent with each other

this is my user class (I'm using fosUserBundle:

class User extends BaseUser
{
...
/**
 * @ORM\OneToMany(targetEntity="Cpj\ProjectsBundle\Entity\Project", mappedBy="user")
 **/
private $projects;
}

and this is my project class

class Project {
...
/**
 * @ORM\ManyToOne(targetEntity="Cpj\UserBundle\Entity\User", inversedBy="projects")
 * @ORM\JoinColumn(name="id_user", referencedColumnName="id")
 **/
private $id_user;
}

I think I correctly followed the instructions in the symfony cookbook but I'm unable to understand what's wrong... thanks

In your User entity you used

mappedBy="user"

Then named the property in your Project entity

private $id_user;

You need them to match for this to work, the mappedBy annotation option refers to the property name not the database field.

This line, that defines the database field which will store the reference, can be different however

  @ORM\JoinColumn(name="id_user", referencedColumnName="id")

Usually we use mappedBy="user" & private $user & JoinColumn(name="id_user" [...] for good readability both in the application and in the database. Keep in my you'll be working with entities, $project->getUser()->getName(); is much prettier than $project->getUserId()->getName(); . In the database on the other hand a id_user field in the project table is more telling than using user .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM