简体   繁体   中英

Related User id from FOS_User in CustomUser-Table

I have three Tables:

Customer CustomerUser FOS_User

I get this Error:

[Doctrine\\ORM\\Mapping\\MappingException]
Property "id" in "FPM\\AppBundle\\Entity\\CustomerUser" was already declared, but it must be declared only once

I don't know where is my mistake, because in the config I have

orm: auto_mapping: true

I have File CustomperUser.php

<?php

namespace FPM\AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * CustomerUser
 */
class CustomerUser
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var \Rowo\UserBundle\Entity\User
     */
    private $idUser;

    /**
     * @var \FPM\AppBundle\Entity\Customer
     */
    private $idCustomer;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set idUser
     *
     * @param \Rowo\UserBundle\Entity\User $idUser
     * @return CustomerUser
     */
    public function setIdUser(\Rowo\UserBundle\Entity\User $idUser = null)
    {
        $this->idUser = $idUser;

        return $this;
    }

    /**
     * Get idUser
     *
     * @return \Rowo\UserBundle\Entity\User
     */
    public function getIdUser()
    {
        return $this->idUser;
    }

    /**
     * Set idCustomer
     *
     * @param \FPM\AppBundle\Entity\Customer $idCustomer
     * @return CustomerUser
     */
    public function setIdCustomer(\FPM\AppBundle\Entity\Customer $idCustomer = null)
    {
        $this->idCustomer = $idCustomer;

        return $this;
    }

    /**
     * Get idCustomer
     *
     * @return \FPM\AppBundle\Entity\Customer 
     */
    public function getIdCustomer()
    {
        return $this->idCustomer;
    }
}

The File which i create during the installation of FOS_UserBundle:

<?php

namespace Rowo\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;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

Here is the xml-File

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="FPM\AppBundle\Entity\CustomerUser" table="customer_user">
    <indexes>
      <index name="customer_user_customer_fk1_idx" columns="id_customer"/>
      <index name="customer_user_user_fk2_idx" columns="id_user"/>
    </indexes>
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <many-to-one field="id" target-entity="FosUser">
      <join-columns>
        <join-column name="id_user" referenced-column-name="id"/>
      </join-columns>
    </many-to-one>
    <many-to-one field="idCustomer" target-entity="Customer">
      <join-columns>
        <join-column name="id_customer" referenced-column-name="id"/>
      </join-columns>
    </many-to-one>
  </entity>
</doctrine-mapping>

You don't need a id column for your CustomerUser table as it is the many-to-many join-table. Besides if you don't need extra columns in the CustomerUser table you don't need to create this whole entity!

You can just tell Doctrine that you want a many-to-many relation and Doctrine will automatically create your third table. Take a look here

If you still want to continue with your CustomerUser entity then take a look at this example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM