duanmi1900
2013-09-16 20:44
浏览 104
已采纳

Doctrine多对一关联不断从关联表中提取所有行

I'm attempting to set up a many-to-one relationship between a series of sales memos and transaction records.

/**
 * @var TransactionInterface
 *
 * @ORM\ManyToOne(targetEntity="Twb\Common\Model\Broker\TransactionInterface")
 * @ORM\JoinColumn(name="FormNoSeller", referencedColumnName="Form")
 */
private $formnoseller;

/**
 * @var TransactionInterface
 *
 * @ORM\ManyToOne(targetEntity="Twb\Common\Model\Broker\TransactionInterface")
 * @ORM\JoinColumn(name="FormNoBuyer", referencedColumnName="Form")
 */
private $formnobuyer;

They are split between two different bundles at the moment ('SalesBundle' and 'BrokerBundle'), and with that in mind I am using interfaces from the SalesMemo entity in SalesBundle to the Transaction entity in BrokerBundle.

For some reason, when I reference either or both of $formnoseller and $formnobuyer in my forms, I notice in dev.log that, after selecting all Transaction rows matching the $formnoseller and/or $formnobuyer fields in the SalesMemos, Doctrine tries to SELECT all rows in the Transaction table (the entity for which TransactionInterface references). This is a bit of a problem, since there is an innumerable amount of rows in the DB, which takes up a lot of memory.

Is there any way to have Doctrine avoid selecting all rows with associations? Or am I even understanding properly how Doctrine does associations? Many thanks for any help.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在一系列销售备忘录和交易记录之间建立多对一关系。

  / ** 
 * @var TransactionInterface 
 * 
 * @ORM \ ManyToOne(targetEntity =“Twb \ Common \ Model \ Broker \ TransactionInterface”)
  * @ORM \ JoinColumn(name =“FormNoSeller”,referencedColumnName =“Form”)
 * / 
private $ formnoseller; 
 
 / ** 
 * @var TransactionInterface 
 * 
 * @ORM \ ManyToOne  (targetEntity =“Twb \ Common \ Model \ Broker \ TransactionInterface”)
 * @ORM \ JoinColumn(name =“FormNoBuyer”,referencedColumnName =“Form”)
 * / 
private $ formnobuyer; 
    
 
 

目前它们在两个不同的包之间分开('SalesBundle'和'BrokerBundle'),考虑到这一点,我使用SalesBundle中的SalesMemo实体到Transaction实体的接口 在BrokerBundle中。

出于某种原因,当我在表单中引用 $ formnoseller $ formnobuyer 中的一个或两个时,我注意到了 在 dev.log,在选择与SalesMemos中的 $ formnoseller 和/或 $ formnobuyer 字段匹配的所有Transaction行之后,Doctrine尝试 SELECT < em> Transaction表中的所有行(TransactionInterface引用的实体)。 这有点问题,因为DB中有无数行,占用了大量内存。

有没有办法让Doctrine避免选择所有行 与协会? 或者我甚至理解Doctrine如何协会? 非常感谢您的帮助。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dousi1097 2013-09-17 07:00
    已采纳

    My understanding of your problem is that you're using an Entity Field Type for $formnoseller and $formnobuyer (or you don't specify the type). Giving the choice to select any élément from the underlying table is the expected behaviour for the Entity Field Type (Used by default for OneToMany relationships)

    If you don't whant a select list of all the elements of your table for those Fields, you should use an other form field type. You should also have a look at data transformers in the documentation.

    已采纳该答案
    打赏 评论
  • duanbamo0127 2013-09-16 21:05

    If it were me, I would write a stored procedure and do an inner or outer join as appropriate.

    Once upon a time, they called this "client server" code. About 15 years ago, it created such a mess the whole industry moved to n-tier development. I'd like to know how the table joins got placed back into the presentation tier again? ORMs and LINQ-to-SQL are a return to client/server".

    If you have to do it this way, do the join in LINQ on the Models. Do not do it with the ORM language.

    打赏 评论

相关推荐 更多相似问题