Symfony 2 学说一对多不写连接表


Symfony 2 Doctrine one-to-many not writing join table

我正在尝试在 tax_ratescountries 之间创建一个一对多,其中一种税率可以有多个与之关联的国家/地区,并在引用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;
}

我的Symfony表格中有以下内容来自税率,国家/地区被列为实体类型:

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

当我加载到表单中,输入有效数据然后提交时,它会正确写入 TaxRates 表。但是,它不会将 id 添加到国家/地区的连接表中,所以我有一条记录,没有任何引用

如果有人能就我做错了什么提出建议,那就太棒了,谢谢。

这只是

一个猜测,但我怀疑你没有调用$country->setTaxRate。 执行以下操作:

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