Ask your Symfony questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Order embedRelation output Symfony

I am embedding forms/relations this way in my form template

if ($this->getObject()->getArts())
foreach ($this->getObject()->getArts() as $art) {

$picture_form = new ArtForm($art);


I have a 1:n relation (a page has many arts).

The form/relations are working fine

The problem is I have to display these art forms sorted by their "position" attribute.

How can I customize the embedRelation query to retrieve the art, adding an "order by position" condition, in order to display the art in the correct order ?

I dont see any orderby option in the embedRelation method currently..


Answers (2)


Markus Welter answers:


In schema.yml you can define a default order by:

class: Art
local: columnname
foreign: columnname
orderBy: columnname

Hope that helps!


shedmore comments:

Thanks for replying Markus...unfortunately that isnt quite what im asking's not flexible enough. Imagine I use that embedded form twice.

One for ordering art items, and one for adding descriptions to a set of art items. On the embedded forms meant for adding descriptions I want the embedRelation to order the embedded forms by the most recently created (at the top)

But on the other group of embedded forms (that just show thumb imgs that get reordered) I want it to appear by a custom "orderby" field defined in the db/schema.

So basically I need to be able to do it on the fly - which is why I was hoping for an option to sort the embedRelation query, but it doesnt seem to be there. I am open to hacks to that class ....but I just need something a little more flexible


José Nahuel Cuesta Luengo answers:

Evan, are you using Propel 1.5? If that's the case, take a look at `sfFormPropel::getRelationForm()`, which is the method that returns the sfFormPropelCollection that is embedded into the main Form.

There you'll see that the way in which the collection of related objects is obtained is by the `get<Model>s()` method. Yo may override that method in your model and order the collection of related objects before they get to the `embedRelation()` method. What I mean is you might be able to tweak this by customizing the order in the `Page::getArts()` method.

Also, I noticed that you loop over the related arts embedding the relation 'Arts'..

You should simply do:



shedmore comments:

I should have been more clear...I am using doctrine (not propel)...sorry - any thoughts considering that? Also I am really hoping someone can fill in the gaps with "how to do it"....I have looked at the sfformdoctrine class, and cant make sense of what to change/extend - otherwise I wouldnt be here ;)

...good point on the embedRelation though - thanks

José Nahuel Cuesta Luengo comments:

I haven't come across a similar issue in Doctrine, but looking at the code in `sfFormDoctrine::embedRelation()`, I see that related objects are obtained by a `$relationName`. Can't you add a relation between `Page` and `Art` that has then ordered in the way you need?

I might be completely out of track here, but this might head you in the right way :-)

OR, you might rearrange the fields of the embedded form (which contains the forms of every related object) from the `PageForm` after embedding the relation..

That's all I'm coming up with at the moment.. hope some of it helps!

shedmore comments:

Is possible (as you said)...but unfortunately I am not really understanding how to approach it. Could you provide a basic example to explain your point?