John Davidson

php - Symfony doctrine has one through relationship

0 comments
Message:


I want to make a relationship like "has one through" in Laravel.


I have a table called "locked_users" with 2 columns "user_id" and "user_type" related to 2 tables "members" and "customers" with a one-to-one relationship.


For example "locked_users" I would have:



  • user_id = 1 and user_type = member

  • user_id = 1 and user_type = customer


I tried to have both relationship "Customer" and "Member" in my "LockedUser" entity but it doesn't seem to work:


/**
* @var Customer|null
*
* @ORM\ManyToOne(targetEntity="Eccube\Entity\Customer", inversedBy="customers", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private ?Customer $Customer = null;

/**
* @var Member|null
*
* @ORM\ManyToOne(targetEntity="Eccube\Entity\Member", inversedBy="members",cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private ?Member $Member = null;


I also tried to use Inheritance Mapping which creates an abstract class called "LockedUser" and moves the relationship to 2 subclasses "LockedCustomer" and "LockedMember" but it doesn't seem to work either.


    /**
* LockedUser
*
* @ORM\MappedSuperclass
* @ORM\Table(name="mtb_locked_user",uniqueConstraints={@ORM\UniqueConstraint(name="user_type", columns={"user_id", "type"})})
* @ORM\Entity(repositoryClass="Customize\Repository\LockedUserRepository")
* @UniqueEntity(fields={"Customer", "Member"})
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
*/
class LockedUser extends \Eccube\Entity\AbstractEntity{}


    /**
* @ORM\Table(name="mtb_locked_user",uniqueConstraints={@ORM\UniqueConstraint(name="user_type", columns={"user_id", "type"})})
* @ORM\Entity
* @ORM\DiscriminatorValue(value="locked_customer")
*/
class LockedCustomer extends LockedUser
{
/**
* @var Customer|null
*
* @ORM\ManyToOne(targetEntity="Eccube\Entity\Customer", inversedBy="customers")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private ?Customer $Customer = null;
}


    /**
* @ORM\Table(name="mtb_locked_user",uniqueConstraints={@ORM\UniqueConstraint(name="user_type", columns={"user_id", "type"})})
* @ORM\Entity
* @ORM\DiscriminatorValue(value="locked_member")
*/
class LockedMember extends LockedUser
{
/**
* @var Member|null
*
* @ORM\ManyToOne(targetEntity="Eccube\Entity\Member", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private ?Member $Member = null;
}

When I save an entity. it just only can save one "Customer" or "Member". If one works then the other doesn't. Sometimes both do not work



My Answer:
this in Symfony using Doctrine:

php
/**
* @ORM\Entity
*/
class User
{
// ...

/**
* @ORM\OneToMany(targetEntity="UserGroup", mappedBy="user")
*/
private $userGroups;
}

/**
* @ORM\Entity
*/
class Group
{
// ...

/**
* @ORM\OneToMany(targetEntity="UserGroup", mappedBy="group")
*/
private $userGroups;
}

/**
* @ORM\Entity
*/
class UserGroup
{
// ...

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

/**
* @ORM\ManyToOne(targetEntity="Group", inversedBy="userGroups")
*/
private $group;
}


In this example, we have three entities: User, Group, and UserGroup. The User entity has a one-to-many relationship with the UserGroup entity through the userGroups property. The Group entity also has a one-to-many relationship with the UserGroup entity through the userGroups property. The UserGroup entity has a many-to-one relationship with both the User and Group entities through the user and group properties, respectively. This creates a many-to-many relationship between the User and Group entities through the UserGroup entity.

Rate this post

4 of 5 based on 6185 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us