1
votes

I keep getting the error in the title when I want to login using the FOSUserBundle on Symfony. The problem is, I already have an "id" for my User table from my database so I don't want to create an "id" field like they ask on the FOSUserBundle guide. I don't understand why it would give me this error when there is no more "id" field in my code.

Is this "id" field mandatory?

Here is the code of my User class (here called "Utilisateurs")`use Doctrine\ORM\Mapping as ORM; use FOS\UserBundle\Model\User as BaseUser;

/**
 * Utilisateurs
 *
 * @ORM\Table(name="utilisateurs", indexes={@ORM\Index(name="FK_UTILISATEURS_id_sexe", columns={"id_sexe"}), @ORM\Index(name="FK_UTILISATEURS_id_niveau", columns={"id_niveau"})})
 * @ORM\Entity
 */

class Utilisateurs extends BaseUser

{


public function __construct()
{
    parent::__construct();

}

/**
 * @var string
 *
 * @ORM\Column(name="nom", type="string", length=25, nullable=true)
 */
private $nom;

/**
 * @var string
 *
 * @ORM\Column(name="prenom", type="string", length=25, nullable=true)
 */
private $prenom;


/**
 * @var \DateTime
 *
 * @ORM\Column(name="date_naissance", type="date", nullable=true)
 */
private $dateNaissance;



/**
 * @var string
 *
 * @ORM\Column(name="url_photo", type="string", length=100, nullable=true)
 */
private $urlPhoto;



/**
 * @var integer
 *
 * @ORM\Column(name="id_utilisateur", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $idUtilisateur;

/**
 * @var \Site\UserBundle\Entity\Sexes
 *
 * @ORM\ManyToOne(targetEntity="Site\UserBundle\Entity\Sexes")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_sexe", referencedColumnName="id_sexe")
 * })
 */
private $idSexe;



/**
 * @var \Site\UserBundle\Entity\Niveaux
 *
 * @ORM\ManyToOne(targetEntity="Site\UserBundle\Entity\Niveaux")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_niveau", referencedColumnName="id_niveau")
 * })
 */
private $idNiveau;`

As you can see I already have an "id_utilisateur" field which is the id of this entity. And here is the code of the entity information in XML: The XML Code

Also here is a screenshot of the error I get when I try to log in: The Error

1

1 Answers

0
votes

I think the problem is that per convention the id field is often called just id and in some places FOS UserBundle is expecting exactly that, e.g. in the UserProvider.

There are a few ways you can get around this. For instance you could just write your own UserProvder (using the one linked above as a reference) where you substitute the id with your field. You might have to do this in other places as well.

The easier solution would be to just change your entity to something like this:

/**
 * @var integer
 *
 * @ORM\Column(name="id_utilisateur", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

public function getId() { return $this->id; }

Similarly in xml this would look like this:

<id name="id" column="id_utilisateur" type="integer">
    <generator strategy="IDENTITY" />
</id>

This way in your entity you will use the expected property and accessor method, but in the background it will map to the database field id_utilisateur, so you you don't have to make any changes to your database.

This should already solve your problems. When a new user is generated Doctrine will take map $user->getId() to user_table.id_utilisateur automatically. If your existing code is making use of the old get-method you could just keep it around and mark it as deprecated:

/**
 * @deprecated Use getId() instead.
 */
public function getIdUtilisateur()
{
    return $this->getId();
}