Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form] Ordering collections #1421

Closed
j opened this issue Jun 23, 2011 · 4 comments
Closed

[Form] Ordering collections #1421

j opened this issue Jun 23, 2011 · 4 comments

Comments

@j
Copy link

j commented Jun 23, 2011

I'm not sure if we can already do this, but I can't figure out how to order an actual collection. My code is:

## CreativeType

public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('name')
        ->add('title')
        ->add('description')
        ->add('creativeQuestions', 'collection', array(
            'type' => new CreativeQuestionsType(),
            'allow_add' => true,
            'allow_delete' => true
        ))
        ->add('active');
}


## CreativeQuestionsType

public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('question', 'entity', array(
            'class' => 'ZGOffers\MainBundle\Entity\Question',
            'query_builder' => function($repository) { return $repository->createQueryBuilder('q')->orderBy('q.question', 'ASC'); },
            'property' => 'question'
        ))
        ->add('pos', 'integer');
}

That all works great, but when rendering my form and the collection, the collection is sorted by ID. I understand I can sort the questions within CreativeQuestionsType, but I want to be able to order by CreativeQuestions' pos field and actually order by the position the user defined them to be.

Is this functionality supported yet?

@j
Copy link
Author

j commented Jun 23, 2011

Nevermind, I didn't make note of the OrderBy annotations provided by the ORM. I'm not sure if this is the only wanted way of achieving this. If so, feel free to close this issue.

@j j closed this as completed Jun 23, 2011
@j j reopened this Jun 23, 2011
@fabpot
Copy link
Member

fabpot commented Jun 24, 2011

Please, ask your questions on the Symfony2 mailing-list.

@fabpot fabpot closed this as completed Jun 24, 2011
@j
Copy link
Author

j commented Jun 27, 2011

Will do. I initially thought this as a feature request without thinking twice about utilizing OrderBy... however, if it's not currently built in, it may be good just in case someone wants to dynamically change the ordering within the form? I'll start using the mailing list. PS -- SF2 is looking and feeling ridiculously awesome. :) Amazing work to you and the contributors.

@trsteel88
Copy link
Contributor

@j the @orderby will only work when loading from the database. If your form contains an error, you will notice that the order is not correct when it reloads with the errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants