1

On my mysql db, I have two tables: "User" And "UserProfile". The table "UserProfile" has a foreign key column named 'user_id' which links to "User" table's "id" column. Now, when I am generating all entity classes from my db tables using doctrine, the created "UserProfile" class contains a property named 'user'(which is of "User" type) and doesn't contain any property named 'user_id'. Is it ok?

Now, if I want to find a user's profile, given the user_id, I needed to write something like this:

$user_profile_repo = $this->em->getRepository("UserProfile"); $user_profile = $user_profile_repo->findOneBy(array("user_id"=>$id)); 

But as the generated entity class doesn't include the "user_id" property, the above code won't work. Now I need to know how can I do the tweak to make the above code work please? Thanks.

6
  • you should name the columns and the associations. i use docblock. check out doctrine-project.org/docs/orm/2.0/en/reference/… , it's a one to one i relationship that should be defined as a column on the user profile. in the user class, you can add a variable and define the association without a column Commented Oct 27, 2011 at 16:12
  • Well, that is what i don't want to do. Actually I don't want to edit the classes as they are auto-generated. So, when i will may need to generate the classes once again, all my changes will be gone. So, looking for some alternative ways. I believe there are some... Commented Oct 27, 2011 at 16:36
  • you dont need to. you just add comments and use the command line tool to generate everything for you. i'll add an answer with an example Commented Oct 27, 2011 at 16:37
  • Is this a legacy database? If not, I strongly recommend that you trash it and start by writing/annotating entities, then using orm:schema-tool:[create|update]. Generating d2 entities from a database schema is considered harmful (by me, at least). Commented Oct 27, 2011 at 18:59
  • @timdev, why do you think that? I though generating entities from sb tables is quickest way as i won't have to write anything. writing of my own can consume time and there is change to make mistakes. Please calrify me againt your opinion so that i can understand and become biased to do so. Thanks.. Commented Oct 28, 2011 at 6:54

1 Answer 1

1

the actual names of the tables/columns in the database are not really important. you can set them in the comments.

it should be something like this:

/** * models\User * @Table(name="User") * @Entity */ class User { /** * @Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false) * @Id * @GeneratedValue(strategy="IDENTITY") */ private $id; /** * @Column(name="name", type="string", length=50, precision=0, scale=0, nullable=false, unique=false) */ private $name; /** * @OneToOne(targetEntity="models\UserProfile", mappedBy="user") */ private $profile; } /** * models\UserProfile * @Table(name="UserProfile") * @Entity */ class UserProfile { /** * @OneToOne(targetEntity="models\User", inversedBy("profile")) * @JoinColumn(name="user_id", referencedColumnName="id") */ private $user; } 

in this case a user has the column id, and the userprofile has user_id

once generated, you should get in the User the method getProfile() and in the UserProfile you should get getUser()

it is similar to 5.7 oneToOne bidirectional: http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html

Sign up to request clarification or add additional context in comments.

2 Comments

Your suggestion gave me the clue to resolve this issue. However, i did able to resolve it by setting up the bi directional primary/fireign key constraint(to make it one to one) on my mysql database and then generated the entity classes through doctrine and thus got the properties you mentioned. Thanks for your help. :)
glad to help. i also use the schema tool to generate/update the mysql tables. it creates all the constraints

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.