[英]Symfony 2 Doctrine one-to-many not writing join table
我正在嘗試在tax_rates
和countries
/地區之間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;
}
我在TaxRates的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.