I have a User
-Entity with the very base information. The User
should be able to share more detailed data in a profile. There are different ProfileSet
s for different User
s.
In this example I try to keep it simple: ProfileSet_A
is a profile with personal information, ProfileSet_B
stores anonymous data.
A User
can only have one ProfileSet
.
Entities (Pseudocode)
// table: users
class User {
protected $id,
$email,
$username,
$password; // ...
}
// table: - none -
class ProfileSet {
protected $id,
$name; // ...
}
// table: profileset_a
class ProfileSet_A extends ProfileSet {
protected $firstname,
$lastname,
$morePrivateStuff; // ...
}
// table: profileset_b
class ProfileSet_B extends ProfileSet {
protected $anyAnonymousStuff; // ...
}
// table: user_has_profileset
class UserHasProfileSet {
protected $user, // relation to User
$profileSet; // relation to ProfileSet_A OR ProfileSet_B
}
Form, ProfileSet_A
username: [ textfield ]
email: [ textfield ]
firstname: [ textfield ]
lastname: [ textfield ]
morePrivateStuff: [ textfield ]
Form, ProfileSet_B
username: [ textfield ]
email: [ textfield ]
anyAnonymousStuff: [ textfield ]
Problems
UserHasProfileSet
should relate to User $user
and a ProfileSet
, which could be an instance of ProfileSet_A
or ProfileSet_B
. I'd like to have just the field $profileSet
instead of $profileSet_A
, $profileSet_B
, ...
I'd like to edit the User
and it's ProfileSet
(A or B) within the same form.
Question
How to solve the problems in a clean way? I'm open to best-practice alternatives. Maybe I'm thinking in a wrong way.
Thanks in advance!
If you use Doctrine
in your project you can archive this with the Doctrine inheritance , for example with MappedSuperClass
:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User
{
// other fields
/**
* @ORM\OneToOne(targetEntity="ProfileSet")
*/
private $profile;
}
/**
* @ORM\Entity
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({"a" = "ProfileSet_A", "b" = "ProfileSet_B"})
* @ORM\Table(name="profile_sets")
*/
class ProfileSet
{
// common properties
}
/**
* @ORM\Entity
* @ORM\Table(name="profile_sets_a")
*/
class ProfileSetA extends ProfileSet
{
// ...
}
/**
* @ORM\Entity
* @ORM\Table(name="profile_sets_b")
*/
class ProfileSetB extends ProfileSet
{
// ...
}
In this case Doctrine will create 3 tables: profile_sets
which will contain common fields and the type of profile, profile_sets_a
and profile_sets_b
will contain specific fields. When you fetch your User
with $profile
property, Doctrine will automatically map the needed Object.
As you have only one ProfileSet
entry for each User
it's not necessary to define extra UserHasProfileSet
entity and you can just set OneToOne
association directly. But you can do it the same way if you have to.
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.