logo
Ask your Symfony questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$14
Order embedRelation output

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);
$this->embedRelation('Arts');
}

}



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..

Thoughts?

shedmore | 05/14/11 at 4:17am Edit
Tutorial: How to assign prize money


The experts have suggested, on average, a prize of $10 for this question.

(6) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    05/14/11
    4:35am
    Markus Welter says:

    Hi!

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


    yourmodelname:
    columns:
    ...
    relations:
    Arts:
    class: Art
    local: columnname
    foreign: columnname
    orderBy: columnname


    Hope that helps!

    Regards

    Previous versions of this answer: 05/14/11 at 4:35am

  • avatar
    Last edited:
    05/14/11
    9:12am
    José Nahuel Cuesta Luengo says:

    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:


    $this->embedRelation('Art');


    Regards,
    Nahuel

  • avatar
    Last edited:
    05/14/11
    4:39am
    shedmore says:

    Thanks for replying Markus...unfortunately that isnt quite what im asking for...it'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

  • avatar
    Last edited:
    05/14/11
    12:23pm
    shedmore says:

    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

  • avatar
    Last edited:
    05/14/11
    1:27pm
    José Nahuel Cuesta Luengo says:

    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!

  • avatar
    Last edited:
    05/14/11
    10:47pm
    shedmore says:

    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?

    Thanks
    Evan

This question has expired.





Current status of this question: Community pot



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.