1

I'm working in a site with a lot of entities that use a common property.

every time that I want to create or update any of this entities, I need to present this property in the form (HTML Select TAG) in a specific Order (lets say alphabetically).

I do remember, not entirely sure, that in synfony2 just by overriding the findBy() class repository method and adding the orderBy clause in it was enough, but it doesn't work like that in symfony3.

the only way to acomplish this orderBy in symfony3 is changing the buildForm method and add a query_builder option... something like this:

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('myProperty', EntityType::class, array(
            'class' => 'MyBundle:MyPropertyClass',
            'required' => false,
            'query_builder' => function ($er) {
                return $er->createQueryBuilder('p')->orderBy('p.order', 'ASC');
        }))

But I don't like the idea of COPY&PASTE this code in every entityFormType just to order the input elements (EntityType).

I'm sure that there must be a better/proper way to order the elements (again... lets say alphabetically) when editing an Entity with any EntityType field. The solutions offered in symfony2 doesn't work here.

Anybody with the same issue?

What is NOT working

1.- ClassRepository findAll() or findBy() methods In symfony2, just by overriding some methods in the Class Repository every select input were displayed as we needed

class MyClassRepository extends EntityRepository{

    public function findAll(){
    return $this->findBy(array(),array('myProperty' => 'ASC'));
    }

    /**
     * @inheritdoc
     */
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
    $orderBy = $orderBy === null ? array('myProperty' => 'ASC') : $orderBy;
    return parent::findBy($criteria, $orderBy, $limit, $offset);
    }

}

2.- @orderby

This is to sort an array of properties in an object, I'm currenly using this but in the other side of the relationship, what I'm trying to find out is how to sort items in a select input from an edit Action/Controller without having to change the code in all the formType classes that use this property::Class (it's something like an enum that is everywhere in my model, to clarify a little bit)

1
  • I think the question is why it isn't using the class repository for making the queries
    – Jonos
    May 31, 2016 at 14:18

1 Answer 1

0

You can add an OrderBy annotation in your entity class:

/** @Entity **/
class MyEntity
{
    // ...

    /**
     * @ManyToMany(targetEntity="Property")
     * @OrderBy({"order" = "ASC"})
     **/
    private $properties;
}
3
  • 1
    already try this... but its in the wrong side of the relationship, This only works in the @...toMany and I needed on the @...toOne side and there it is completly ignored
    – Jonos
    May 31, 2016 at 20:23
  • Are you sure your relationship is in the right side? Isn't it more logical to have a OneToMany between your entity and its properties?
    – Veve
    Jun 1, 2016 at 7:38
  • 1
    not really, remember that the thing I'm trying to do is to sort elements in a select input entity type to choose one object in a edit action. In symfony2 all this kind of queries were performed by the class repository... apparently not anymore, or maybe i'm not overriding the right method...
    – Jonos
    Jun 2, 2016 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.