1

I would like to receive advice / guidelines / a possible example on how to implement the following (in the best way possible) :

I have Two Entities and i'm using Doctrine CTI Inheritance :

  • User
  • Player (extends User)

I would like to make a simple CRUD. How can i add/edit a user or a player ? I have a userRole field in my User Entity and it can take several values such as Player, Guest, Admin etc.. (they are all children of the User Entity). Depending what we choose, we will add/edit/delete a User, a Player an Admin etc.. This userRole field must be an EntityType in the form :

    ->add('userRole',EntityType::class, [
        'class' => UserRole::class,
    ])

These are my solutions atm :

  • Create 2 FormTypes (one for User and one for Player), 2 Controllers and the related twig views.
  • Based on a previous question i asked on stackoverflow, A solution was based on Form Events for the backend and javascript for the front (to hide / remove fields based on a select). The problem though is that it works fine most of the time (Symfony 2.8+, Doctrine Inheritance and Forms). I also realised that if you don't have javascript enabled the whole page containing the form will break.
  • For my last solution, i don't know how to implement it but here it goes : 2 FormTypes (PlayerFormType extends UserFormType), only one twig file with some nice conditions to render the right form fields and only One Controller. I don't know how it would look in the controller (possibly process multiple forms ?) but what i like here, is that there's no js involved ! So if js is disabled everything still works.

User Entity

/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"user" = "User", "player" = "Player"})
*
*/
class User
{
/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", unique=true)
 * @Assert\NotBlank()
 */
private $username;

/**
 * @ORM\Column(type="string")
 * @Assert\NotBlank()
 */
private $password;

/**
 * @ORM\Column(type="string")
 * @Assert\NotBlank()
 */
private $firstname;

/**
 * @ORM\Column(type="string")
 * @Assert\NotBlank()
 */
private $lastname;

/**
 * @ORM\Column(type="string")
 */
private $gender;

/**
 * @ORM\Column(type="datetime")
 */
private $birthday;

/**
 * @ORM\Column(type="string", unique=true)
 * @Assert\NotBlank()
 * @Assert\Email(
 *     message = "The email '{{ value }}' is not a valid email.",
 *     checkMX = true
 * )
 */
private $email;

/**
 * @ORM\ManyToOne(targetEntity="UserRole", inversedBy="user")
 * @ORM\JoinColumn(nullable=false)
 * @Assert\NotBlank()
 */
private $userRole;

/**
 * @ORM\ManyToOne(targetEntity="Address", inversedBy="user", cascade={"persist", "remove"})
 * @ORM\JoinColumn(nullable=false)
 */
private $address;

Player Entity

/**
* @ORM\Entity()
*/
class Player extends User
{
/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="PlayerRole", inversedBy="player")
 * @ORM\JoinColumn(nullable=false)
 */
private $playerRole;

/**
 * @ORM\ManyToOne(targetEntity="Game", inversedBy="player")
 * @ORM\JoinColumn(nullable=false)
 */
private $game;

/**
 * @ORM\ManyToOne(targetEntity="Team", inversedBy="player")
 * @ORM\JoinColumn(nullable=false)
 */
private $team;
Community
  • 1
  • 1
Elbarto
  • 1,213
  • 1
  • 14
  • 18

0 Answers0