简体   繁体   English

Symfony 2 Doctrine一对多不写连接表

[英]Symfony 2 Doctrine one-to-many not writing join table

I am trying to create a one-to-many between tax_rates and countries , where one tax rate can have multiple countries associated with it, with a join table on the countries referencing the tax_rate with the following schema: 我正在尝试在tax_ratescountries /地区之间tax_rates ,其中一个税率可以与多个国家/地区相关联,并且在引用tax_rate的国家/地区上使用以下模式的联接表:

/** @Entity **/
class TaxRates
{
    ...

    /**
    * @ORM\OneToMany(targetEntity="Acme\DemoBundle\Entity\Country", mappedBy="tax_rate", cascade={"persist"})
    */
    protected $country;

    ...

    public function __construct()
    {
        $this->country = new ArrayCollection();
    }
}

/** @Entity **/
class Country
{
    /**
      * @ORM\ManyToOne(targetEntity="Acme\TaxBundle\Entity\TaxRates", inversedBy="country")
      * @ORM\JoinColumn(name="taxrate_id", referencedColumnName="id")
      */
    protected $tax_rate;
}

I have the following in my Symfony form from the TaxRates with the country being included as a Entity Type: 我在TaxRates的Symfony表单中有以下内容,其中国家/地区被包含为实体类型:

$builder->add('country', 'entity', array(
                'class' => "AcmeDemoBundle:Country",
                'property' => "name",
                "multiple" => true,
            ))

When I load into the form, enter valid data and then submit it, it writes correctly into the TaxRates table. 当我加载到表单中时,输入有效数据然后提交它,它会正确写入TaxRates表。 However it does not add the id into the join table on the countries so I have a record, with nothing referencing it. 但是它没有将id添加到国家/地区的连接表中,所以我有一个记录,没有任何引用它。

If anyone could advise on what I'm doing wrong that'd be fantastic, thank you. 如果有人能告诉我我做错了什么就太棒了,谢谢你。

It's just a guess but I suspect you are not calling $country->setTaxRate. 这只是一个猜测,但我怀疑你没有调用$ country-> setTaxRate。 Do something like: 做类似的事情:

class TaxRates {
  function addCountry($country) {
    $this->countries[] = $country;
    $country->setTaxRate($this); // This is what might be missing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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