Legacy PHP and Doctrine: Duplicate Definition of column

Asked

Viewed 159 times

2

Good afternoon.

The following error is happening:

Error: Duplicate Definition of column 'numg_usuario' on Entity 'Usuario' in a field or discriminator column Mapping.

My User class extends from Possesgrupo.

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 a group is 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 like this with Doctrine, and I can’t change the database structure.

inserir a descrição da imagem aqui

  • You can supplement the question with the template of your database at the moment?

  • Good morning, I edited the question. Thank you.

1 answer

0

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

  • A user can be part of several groups;
  • A group may have several users.

Thus, 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 two aspects to customize, but in general it is more or less that. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.