Doctrine - ManyToOne与表之间

时间:2014-03-06 21:01:42

标签: php mysql sql symfony doctrine

我想创建一个模型,它将使表用户引用表CustomFieldValue nad表CustomField,同时引用CustomFieldValue。 CustomFieldValue只有id,value和两列,一列来自用户,另一列来自CustomField。我希望在注册表单中添加动态添加新字段等功能。这是个好主意吗?如果是,请帮我这个模型,因为它不起作用: 用户:

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

    /**
     * @ORM\Column(type="string", length=25, unique=true)
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=64)
     */
    private $password;


    /**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="users",cascade={"persist"})
     * 
     */
    private $roles;

    /**
     * @ORM\Column(type="string", length=60, unique=true)
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=60)
     */
    private $sex;

    /**
     * @ORM\ManyToMany(targetEntity="Region", inversedBy="users")
     * @ORM\JoinColumn(name="region", referencedColumnName="id")
     */
    private $region;


    /**
     * @ORM\ManyToMany(targetEntity="Type", inversedBy="users")
     * @ORM\JoinColumn(name="type", referencedColumnName="id")
     */
    private $type;

    /**
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;


    /**
    * @ORM\ManyToOne(targetEntity="CustomFieldValue",inversedBy="id")
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
    */
    private $customValues;

CustomFieldValue:

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

    /**
     * @var string
     *
     * @ORM\Column(name="value", type="string", length=255)
     */
    private $value;

    /**
    *ORM\OneToMany(targetEntity="User", mapped-by="id")
    *@ORM\JoinColumn(name="user_id", referencedColumnName="id")
    */

    private $user;


    /**
    *@ORM\OneToMany(targetEntity="CustomField", mapped-by="id" )
    *@ORM\JoinColumn(name="field_id", referencedColumnName="id")
    */

    private $field; 

的CustomField:

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

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="type", type="string", length=255)
     */
    private $type;

    /**
     * @var boolean
     *
     * @ORM\Column(name="required", type="boolean")
     */
    private $required;

    /**
    * @ORM\ManyToOne(targetEntity="CustomFieldValue", inversedBy="id")
    * @ORM\JoinColumn(name="customfield_id", referencedColumnName="id")
    */
    private $customValues;

1 个答案:

答案 0 :(得分:1)

您的地图有点偏离:

用户实体应该是:

/**
 * @ORM\ManyToOne(targetEntity="CustomFieldValue", inversedBy="user")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $customValues;

CustomFieldValue应该是:

/**
 *ORM\OneToMany(targetEntity="User", mappedBy="customValues")
 */
private $user;


/**
*@ORM\OneToMany(targetEntity="CustomField", mappedBy="customValues" )
*/

private $field; 

CustomField应为:

/**
 * @ORM\ManyToOne(targetEntity="CustomFieldValue", inversedBy="field")
 * @ORM\JoinColumn(name="customfield_id", referencedColumnName="id")
 */
private $customValues;

当您调用mappedBy时,您不需要连接列,这已经告诉学说在该字段上查找连接列声明。对于mappedBy和inversedBy字段,这些是将2连接在一起而不是实际连接列名称的字段。