Inheritance PHP and Doctrine: Duplicate definition of column

2

Good afternoon.

The following error is occurring:

Error: Duplicate definition of column 'num_user' on entity 'User' in a field or discriminator column mapping.

My User class extends from OwnGroup.

Then the User Class mapping is as follows:

 /**
  * @Table(name="ge_usuario")
  * @Entity
  */
  class Usuario extends PossuiGrupo {

  /**
   * @var integer
   *
   * @Column(name="numg_usuario", type="integer", nullable=false)
   * @Id
   * @GeneratedValue(strategy="AUTO")
   */
    protected $numgUsuario;

And within the class has group like this:

 /**
     * @Entity
     * @Table(name="ge_grupo_usuario")
     */
    class PossuiGrupo extends ModelObject {

   /**
     * @Id
     * @Column(name="numg_usuario", type="integer", nullable=false)
     * @ManyToOne(targetEntity="Usuario")
     */
    protected $usuario;

   /**
     * @Id
     * @Column(name="numg_grupo", type="integer", nullable=false)
     * @ManyToOne(targetEntity="Grupo")
     */
    protected $grupo;

    public function __construct($usuario, $grupo) {
        $this->usuario = $usuario;
        $this->grupo = $grupo;
    }

Is there any way to work with Doctrine like this, since I can not change the structure of the database.

    
asked by anonymous 26.07.2016 / 21:49

1 answer

0

What I would do would be to create a many-to-many relationship between users and groups. Thus, it is implied that:

  • A user can join several groups;
  • A group can have multiple users.

That way, these would be the classes.

Class Usuario :

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="ge_usuario")
 */
class Usuario
{
    /**
     * @ORM\Column(name="numg_usuario", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @ORM\ManyToMany(targetEntity="Grupo", inversedBy="usuario")
     * @ORM\JoinTable(name="ge_grupo_usuario")
     */
    public $grupos = [];
}

class Grupo :

<?php

namespace AppBundle\Entity;

/**
 * @ORM\Entity
 * @ORM\Table(name="ge_grupo")
 */
class Grupo
{
    /**
     * @ORM\Column(name="numg_grupo", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @ORM\ManyToMany(targetEntity="Usuario", mappedBy="grupos")
     */
    public $usuarios = [];
}

It should have one or the other aspect to customize, but overall it's more or less that. :)

    
28.07.2016 / 10:42