4
votes

I'm working with FOSUserBundle and I need to build Users Profile. This is what I did:

Create the User class and extends from BaseUser as FOSUser docs said

namespace Sunahip\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToOne(targetEntity="Profile", mappedBy="user")
     */
    protected $profile;

    /**
     * @ORM\ManyToMany(targetEntity="Sunahip\UserBundle\Entity\Group")
     * @ORM\JoinTable(name="fos_user_user_group",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
     */
    protected $groups;

}

Create a Profile entity

namespace Sunahip\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;

/**
 * @ORM\Entity
 * @ORM\Table(name="profile")
 */
class Profile extends BaseUser
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Id
     * @ORM\OneToOne(targetEntity="User", inversedBy="profile")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * @ORM\Column(name="register_type", type="smallint", length=1)
     */
    protected $register_type;

    /**
     * @ORM\Column(name="rif", type="string", length=25)
     */
    protected $rif;

    /**
     * @ORM\Column(name="ci", type="string", length=25)
     */
    protected $ci;

    /**
     * @ORM\Column(name="firstname", type="string", length=25)
     */
    protected $firstname;

    /**
     * @ORM\Column(name="lastname", type="string", length=25)
     */
    protected $lastname;

    /**
     * @ORM\Column(name="state", type="string", length=150)
     */
    protected $state;

    /**
     * @ORM\Column(name="city", type="string", length=150)
     */
    protected $city;

    /**
     * @ORM\Column(name="town", type="string", length=150)
     */
    protected $town;

    /**
     * @ORM\Column(name="urbanization", type="string", length=150)
     */
    protected $urbanization;

    /**
     * @ORM\Column(name="urbanization", type="string", length=150)
     */
    protected $street;

    /**
     * @ORM\Column(name="aparment", type="string", length=150)
     */
    protected $aparment;

    /**
     * @ORM\Column(name="aparment_no", type="string", length=150)
     */
    protected $aparment_no;

    /**
     * @ORM\Column(name="reference", type="string", length=250)
     */
    protected $reference;

    /**
     * @ORM\Column(name="zipcode", type="string", length=250)
     */
    protected $zipcode;

    /**
     * @ORM\Column(name="fax", type="string", length=250)
     */
    protected $fax;

    /**
     * @ORM\Column(name="local_phone", type="string", length=250)
     */
    protected $local_phone;

    /**
     * @ORM\Column(name="movil_phone", type="string", length=250)
     */
    protected $movil_phone;

    /**
     * @ORM\Column(name="alt_email", type="string", length=250)
     */
    protected $alt_email;

    /**
     * @ORM\Column(name="alt_email", type="string", length=250)
     */
    protected $website;

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

    public function setUser(User $user)
    {
        $this->user = $user;
    }

    public function getUser()
    {
        return $this->user;
    }

    public function setRegisterType($register_type)
    {
        $this->register_type = $register_type;
    }

    public function getRegisterType()
    {
        return $this->register_type;
    }

    public function setRif($rif)
    {
        $this->rif = $rif;
    }

    public function getRif()
    {
        return $this->rif;
    }

    public function setCI($ci)
    {
        $this->ci = $ci;
    }

    public function getCI()
    {
        return $this->ci;
    }

    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;
    }

    public function getFirstname()
    {
        return $this->firstname;
    }

    public function setLastname($lastname)
    {
        $this->lastname = $lastname;
    }

    public function getLastname()
    {
        return $this->lastname;
    }

    public function setState($state)
    {
        $this->state = $state;
    }

    public function getState()
    {
        return $this->state;
    }

    public function setCity($city)
    {
        $this->city = $city;
    }

    public function getCity()
    {
        return $this->city;
    }

    public function setTown($town)
    {
        $this->town = $town;
    }

    public function getTown()
    {
        return $this->town;
    }

    public function setUrbanization($urbanization)
    {
        $this->urbanization = $urbanization;
    }

    public function getUrbanization()
    {
        return $this->urbanization;
    }

    public function setStreet($street)
    {
        $this->street = $street;
    }

    public function getStreet()
    {
        return $this->street;
    }

    public function setAparment($aparment)
    {
        $this->aparment = $aparment;
    }

    public function getAparment()
    {
        return $this->aparment;
    }

    public function setAparmentNo($aparment_no)
    {
        $this->aparment_no = $aparment_no;
    }

    public function getAparmentNo()
    {
        return $this->aparment_no;
    }

    public function setReference($reference)
    {
        $this->reference = $reference;
    }

    public function getReference()
    {
        return $this->reference;
    }

    public function setZipcode($zipcode)
    {
        $this->zipcode = $zipcode;
    }

    public function getZipcode()
    {
        return $this->zipcode;
    }

    public function setFax($fax)
    {
        $this->fax = $fax;
    }

    public function getFax()
    {
        return $this->fax;
    }

    public function setLocalPhone($local_phone)
    {
        $this->local_phone = $local_phone;
    }

    public function getLocalPhone()
    {
        return $this->local_phone;
    }

    public function setMovilPhone($movil_phone)
    {
        $this->movil_phone = $movil_phone;
    }

    public function getMovilPhone()
    {
        return $this->movil_phone;
    }

    public function setAltEmail($alt_email)
    {
        $this->alt_email = $alt_email;
    }

    public function getAltEmail()
    {
        return $this->alt_email;
    }

    public function setWebsite($website)
    {
        $this->website = $website;
    }

    public function getWebsite()
    {
        return $this->website;
    }

}

Now, I'm trying to validate that entities by running the command doctrine:schema:validate and I get this error:

[Doctrine\ORM\Mapping\MappingException] Duplicate definition of column 'urbanization' on entity 'Sunahip\UserBundle\Entity\Profile' in a field or discriminator column mapping.

My questions:

  1. I don't know what is wrong and also don't know what the error means is the first time I got this error.
  2. I don't know if I'm building users profiles in the right way I mean if I should extends from BaseUser or from User

Can I give some help here? Advices? Ideas?

1
paste the rest of your Profile entity. Easiest solution would be like in error message - duplicated definition for urbanization column - Tomasz Madeyski
@TomaszMadeyski you're right I miss to change the columns name on two attributes and I already fix that but now the error is this ` [Doctrine\ORM\Mapping\MappingException] Entity 'Sunahip\UserBundle\Entity\Profile' has a composite identifier but uses an ID generator other than manually assigning (Identity, Sequence). This is not supported.`, why? - ReynierPM
because you have multiple @ORM\Id at your entity definition - Tomasz Madeyski
@TomaszMadeyski and what is the solution in this case? - ReynierPM
I guess your field $id is your real Id, so remove all other @ORM\Id entries (like one in $user 's annotation) - Tomasz Madeyski

1 Answers

4
votes

You have (had) basically two probles here:

  1. Duplicated urbanization column name somewhere there which needs to be removed. Only one column with the same name is allowed

  2. Duplicated @ORM\Id annotation in your Profile entity. Remove one from $user because it is not your Id