1
votes

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 queriesJonos

1 Answers

0
votes

You can add an OrderBy annotation in your entity class:

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

    /**
     * @ManyToMany(targetEntity="Property")
     * @OrderBy({"order" = "ASC"})
     **/
    private $properties;
}