来自Symfony2中的manyToOne,oneToMany协会的结果无法正常工作

时间:2014-09-12 19:11:04

标签: php mysql sql symfony doctrine-orm

我正在使用symfony2构建我的网络应用程序,但我无法弄清楚为什么查询不会返回两个表的结果,只有一个。在我的结果中,我只从 SupplierPayment 表中获取值。

我有1个看起来像这样的表:

SupplierPayment

 id | 
 ------
 1  |

我有另一张表如下:

SettlementReport

 id | SupplierPayment
 ------------------------------
 1  | 1

 SupplierPayment is a manyToOne association back to the SupplierPayment table. 

以下是我对每个实体的关联:

SupplierPayment实体

 /**
 * @ORM\OneToMany(targetEntity="WIC\SettlementBundle\Entity\SettlementReport", mappedBy="supplierPayment", fetch="EXTRA_LAZY")
 */
protected $settlementReport;

结算报告实体

 /**
 * @ORM\ManyToOne(targetEntity="WIC\SupplierBundle\Entity\SupplierPayment", inversedBy="supplierReport")
 * @ORM\JoinColumn(name="supplierPayment_id", referencedColumnName="id", nullable=true)
 */
protected $supplierPayment;

我希望从结算报告中返回来自SupplierPayment的值。

出于某种原因,当使用doctrine查询我的SupplierPayment表时使用 - > find(1) 它不包含 SettlementReport 表的结果。这是为什么?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

 /**
 * @ORM\ManyToOne(targetEntity="WIC\SupplierBundle\Entity\SupplierPayment", inversedBy="supplierReport")
 * @ORM\JoinColumn(name="supplierPayment_id", referencedColumnName="id", nullable=true)
 */
protected $supplierPayment;

修正:

/**
 * @ORM\ManyToOne(targetEntity="WIC\SupplierBundle\Entity\SupplierPayment", inversedBy="settlementReport")
 * @ORM\JoinColumn(name="supplierPayment_id", referencedColumnName="id", nullable=true)
 */
protected $supplierPayment;

我建议您为包含多个值的属性使用多个名称(是ArrayCollection)

使用:" $ settlementReports"而不是" $ settlementReport"

答案 1 :(得分:0)

inversedBy语句需要匹配关联实体的OneToMany propritie 改变inversedBy =" supplierReport" to inversedBy =" settlementReport"

/**
 * @ORM\ManyToOne(targetEntity="WIC\SupplierBundle\Entity\SupplierPayment", inversedBy="settlementReport")
 * @ORM\JoinColumn(name="supplierPayment_id", referencedColumnName="id", nullable=true)
 */
protected $supplierPayment;