I have a User entity that has a oneToMany mapping to a UserProfile entity.
Currently, I am only using the newest UserProfile entity created, while the
rest are available for historical purposes. In order to reference only the
latest UserProfile entity, I have another oneToOne mapping to UserProfile
called currentProfile. However, this creates a circular reference, where my
user_profile table references user via user_id, and my user table
references user_profile via current_profile_id.
So it looks something like this:
class User {
/**
* @Id
*/
private $id;
/**
* @OneToMany(targetEntity="UserProfile", mappedBy="user")
*/
private $userProfiles;
/**
* @OneToOne(targetEntity="UserProfile")
* @JoinColumn(name="current_profile_id", referencedColumnName="id")
*/
private $currentProfile;
}
class UserProfile {
/**
* @Id
*/
private $id;
/**
* @ManyToOne(targetEntity="User", inversedBy="profiles")
* @JoinColumn(name="user_id", referencedColumnName="id",
nullable=false)
*/
private $user;
}
I previously had no qualms about this until a co-worker mentioned it was
bad practice to have circular references, and cited an example where it was
difficult to delete a user since you would have to set current_profile_id
to null, then delete the user_profile, then delete user. His suggestion was
to have a boolean on the UserProfile entity called currentProfile which
could be toggled true or false. However, this defeats the purpose I had in
mind since part of the reason I designed it this way was to easily access
the current profile from the User entity, so that I can easily reference
the current profile via DQL, and would save me fetching all rows from the
user entity just to filter out the latest one.
So is there a doctrine-friendly way to handle this without a circular
reference, while still being saved from the burden of fetching all rows at
once, or is the circular reference not so bad in this case? One idea was to
create a view, but I am not sure how to map a view to my entity class.
Thanks,
Richard
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.