symfony executing multiple queries to populate repeated form field
My symfony project has an object A which has a one-to-many relationship with object(s) B, i.e.
A
B
B
B
A
B
A
B
B
In the form for A, I'm using $this->embedRelation('B') to display the form(s) for entering/editing B. All good so far.
The form for B contains an auto-generated sfWidgetFormChoice which is automatically populated from the database. However, when there are multiple occurances of form B embedded into form A, symfony queries the database multiple times to populate the sfWidgetFormChoice select control:
SELECT b.bid AS b__bid, b.blah AS b__blah, FROM blah b
SELECT b.bid AS b__bid, b.blah AS b__blah, FROM blah b
SELECT b.bid AS b__bid, b.blah AS b__blah, FROM blah b
SE开发者_如何学GoLECT b.bid AS b__bid, b.blah AS b__blah, FROM blah b
SELECT b.bid AS b__bid, b.blah AS b__blah, FROM blah b
Is there a way of indicating to symfony that it only needs to run this query once?
You can fetch this result from Doctrine's result cache. To achieve this, you must configure the result cache, and then activate it for the query corresponding to the sfWidgetFormChoice (or is it sfWidgetFormDoctrineChoice?). The problem is you don't have access to this piece of code. Or don't you? Look at the options of sfWidgetFormDoctrineChoice. You can specify a method through the option "table_method". All you have to do is write the query and activate the result cache on it and you're done. Good luck!
精彩评论