Doctrine 2 ManyToOne乘以JoinColumns

时间:2016-04-01 19:19:41

标签: doctrine-orm symfony

我有下一个实体:

/**
* Customer
*
* @ORM\Table(name="customer")
* @ORM\Entity()
*/
class Customer
{
    /**
    * @var int
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

    /**
    * @var Account[]|ArrayCollection
    *
    * @ORM\OneToMany(targetEntity="Account", mappedBy="customer")
    */
    private $accounts;

    /**
    * @var Account
    *
    * @ORM\OneToOne(targetEntity="Account")
    *
    * @ORM\JoinColumns({
    *      @ORM\JoinColumn(name="id",referencedColumnName="customer_id"),
    *      @ORM\JoinColumn(name="default_account_id", referencedColumnName="id")
    * })
    */
    private $defaultAccount;
}


/**
* Account
*
* @ORM\Table(name="account")
*/
class Account
{
    /**
    * @var int
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;


    /**
    * @var Customer
    *
    * @ORM\ManyToOne(targetEntity="Customer", inversedBy="accounts")
    * @ORM\JoinColumn(nullable=false)
    */
    private $customer;
}

认为客户有多个帐户,帐户的开头应该是客户的默认帐户。我想使用带有乘法字段的外键来确保默认客户帐户属于该客户,但我收到下一个错误:

Column name `customer_id` referenced for relation from MyBundle\Entity\Customer towards MyBundle\Entity\Account does not exist.

实际上ORM级别没有“customer_id”字段,因为它是“customer.id”但我不知道如何引用它。

1 个答案:

答案 0 :(得分:0)

似乎行@ORM\JoinColumn(name="id",referencedColumnName="customer_id"),是多余的。尝试按如下方式配置此字段:

/**
* @var Account
*
* @ORM\OneToOne(targetEntity="Account")
*
* @ORM\JoinColumn(name="default_account_id", referencedColumnName="id")
*/
private $defaultAccount;

顺便说一句,我认为如果你只是在你的is_default实体中添加一个布尔列Account会更好。