简体   繁体   中英

How get total and name to field with query builder in formtype symfony

I want to create a select field with referece of my types available.

I have 3 entity. Post, Types[typebase_id, count=0], TypesBase[name,quantity].

I want to display => Choices =>

  • TypeA (3)
  • TypeB (12)

1- I need to get TypeBase Name.

2- I need to group by Types (Typebase_id and count=0)

3- The selected value will be Typebase_id of Types entity.

I have this in my formtype.

$builder->add('types', 'entity', array(
                    'class' => 'PostBundle:Types',
                    'query_builder' => function(EntityRepository $er) use ($user) {
                        return $er->createQueryBuilder('w')
                            ->where("w.user = :user")
                            ->setParameter('user', $user)
                            ->andWhere("w.active = :active")
                            ->setParameter('active', 0);
                    },
                ));

I try __toString in Types, but i cant get TypeBase Name.

Thanks

Use choice_label parameter. Docs: http://symfony.com/doc/2.7/reference/forms/types/entity.html#choice-label

$builder->add('types', 'entity', array(
    'class' => 'PostBundle:Types',
    'query_builder' => function(EntityRepository $er) use ($user) {
        return $er->createQueryBuilder('w')
            ->where("w.user = :user")
            ->setParameter('user', $user)
            ->andWhere("w.active = :active")
            ->setParameter('active', 0);
    },
    'choice_label' => function ($type) {
        return $type->getName();
    }
));

Or just: 'choice_label' => 'name' .

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