简体   繁体   中英

Symfony: Use distinct field for EntityType in a FormType form

In my Symfony 3.4 project there's a contact us form that use a Symfony form Form/ContactType.php in which one of the dropdown boxes needs to the distinct values of an entity as the values that are displayed in that dropdown box.

Ex: ContactType.php form

class ContactType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('type', EntityType::class, ['class'=>'AppBundle:Employees']);
    }
}

In this form there is a type field for which the employee types must be loaded. So the sql that needs to use for the above dropdown should be:

SELECT distinct(e.type) AS type FROM AppBundle:Employees e

How can I load the result of this query in to this EntityType field in the Symfony form?

You should take a look on query-builder for Entity form type. https://symfony.com/doc/current/reference/forms/types/entity.html#query-builder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM