Sorting the subforms in a embedRelation form

Discussion relating to version 1.3.x and 1.4.x

Sorting the subforms in a embedRelation form

Postby cyril » Wed Apr 21, 2010 7:31 pm

Hi, this si my problem:

I have a 1:n relation (a recipe ans many steps).

In the backend I generated a form and I added the steps with the $this->embedRelation('Steps'); method.

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

How can I customize the query to retrieve the steps, adding an "order by position" condition, in order to display the steps in th order ?

Or is there an option with the embedRelation method ?
cyril
Junior Member
 
Posts: 2
Joined: Wed Apr 21, 2010 7:25 pm

Re: Sorting the subforms in a embedRelation form

Postby xplo » Wed Apr 21, 2010 9:22 pm

i m not sure i understand what u want.
But if you want to order a list with sfFormWidgetPropel|DoctrineChoice : propel has the option order_by and i guess doctrine too but i cant confirm it for doctrine.
xplo
Faithful Member
 
Posts: 593
Joined: Wed Sep 17, 2008 3:23 pm

Re: Sorting the subforms in a embedRelation form

Postby cyril » Wed Apr 21, 2010 11:05 pm

No, that is not the problem.

this is the schema:
Code: Select all
Recipe:
  actAs:
    Sluggable:
      fields: [name]
    Timestampable: ~
  columns:
    name: { type: string(64), notnull: true }
    author_id: { type: integer(4), notnull: false }


RecipeStep:
  columns:
    recipe_id: { type: integer }
    description: { type: text(5000) }
    position: { type: integer(1), unsigned: true, default: 0 }
  relations:
    Recipe:
      class: Recipe
      local: recipe_id
      foreign: id
      foreignAlias: Steps
      foreignType: many
      type: one
      onDelete: CASCADE


With the admin generator, I generated the recipe module (I can edit the recipes now).

Then in the RecipeForm.class.php:
Code: Select all
public function configure()
{
    $this->embedRelation('Steps');
}


Now, into the same form, I can edit a recipe and its steps (each step is an embedded form with a "description" textarea and a "position" field).
The "steps" forms are automatically generated, and their are not sorted by position, and I want to sort these forms by the "position" field.

So how can I sort the generated "Steps" embedded forms ?

How can I rewrite the Doctrine query which retrieve the Steps for a given recipe, in order to add a "order by position ASC" ?
cyril
Junior Member
 
Posts: 2
Joined: Wed Apr 21, 2010 7:25 pm

Re: Sorting the subforms in a embedRelation form

Postby to0om » Tue May 03, 2011 7:00 pm

Same problem over here - so far, I haven't found any possibility to sort embedded relations. There are options, but there's no order_by:

Code: Select all
  /**
   * Embed a Collection form based on a Relation into this form.
   * Available options:
   *  - title: The title of the collection form once embedded. Defaults to the relation name.
   *  - decorator: The decorator for the sfWidgetFormSchemaDecorator
   *  - add_empty: Whether to allow the user to add new objects to the collection. Defaults to true
   * Additional options are passed to sfFromPropel::getRelationForm()
   *  - empty_label: The label of the empty form
   *
   * @param string $relationName The name of a relation of the current Model, e.g. 'Book'
   * @param array  $options      An array of options
   *
   * @return sfPropelForm        The current form instance
   */
  public function embedRelation($relationName, $options = array())
  {
    $options = array_merge(array(
      'title'               => $relationName,
      'decorator'           => null,
      'add_empty'           => true,
      'max_additions'       => 0,
      'empty_label'         => null,
    ), $options);
to0om
Junior Member
 
Posts: 12
Joined: Tue Feb 26, 2008 9:35 am

Re: Sorting the subforms in a embedRelation form

Postby pachocho » Fri Jan 18, 2013 7:51 pm

have you tried overwriting the getSteps() method?

Something like:
class RecipeQUery
public function getSteps($recipe_id) {
return RecipeStepsQuery::create()->
joinWithRecipe()->
filterByRecipeId($recipe_id)->
orderByPosition()->
find();
}

When the form class calls the getSteps() method to populate the sub forms will call your one...
pachocho
Member
 
Posts: 87
Joined: Mon May 26, 2008 9:18 am
Location: Italy

Re: Sorting the subforms in a embedRelation form

Postby pachocho » Fri Jan 18, 2013 10:54 pm

I just tried the solution i posted and it doesn't work...BUT...
there is an option "criteria" not mentioned in the documentation (much more important than the add new text, well done -_-" ) that basically lets you sort, filter, join and so on.

Try $this->embedRelation('Steps', array('criteria' => StepsQuery::create()->orderByPosition()));
pachocho
Member
 
Posts: 87
Joined: Mon May 26, 2008 9:18 am
Location: Italy


Return to symfony 1.3 and 1.4

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests

cron