CustomUser-Table中FOS_User的相关用户ID

时间:2015-06-03 14:13:04

标签: symfony doctrine

我有三张表:

客户 CustomerUser FOS_User

我收到此错误:

  

[学说\ ORM \映射\ MappingException]
    财产" id"在" FPM \ AppBundle \ Entity \ CustomerUser"已经宣布,但必须只宣布一次

我不知道我的错误在哪里,因为在配置中我有

  

orm:auto_mapping:true

我有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;
    }
}

我在安装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
    }
}

这是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>

1 个答案:

答案 0 :(得分:1)

您的CustomerUser表不需要id列,因为它是多对多连接表。此外,如果您在CustomerUser表中不需要额外的列,则无需创建整个实体!

你可以告诉Doctrine你想要一个多对多关系,而Doctrine会自动创建你的第三个表。看看here

如果您仍想继续使用CustomerUser实体,请查看this example