我收到错误“试图在类上调用方法'生成URL'”Symfony 2.6


I get error "Attempted to call method "generateUrl" on class" Symfony 2.6

在生成角色CRUD(控制台生成:doctrine:crud(之后,我正在尝试使用RoleType覆盖Form,

但是我收到错误:
Attempted to call method "generateUrl" on class "BISSAP'UserBundle'Form'RoleType".

CRUD 的一部分 - 角色控制器.php

public function newAction()
    {
        $entity = new Role();
        //$form   = $this->createCreateForm($entity);
        $form   = $this->createForm(new RoleType(),$entity, array(
            'action' => $this->generateUrl('role_create'),
            'method' => 'POST',
        ));
        return $this->render('BISSAPUserBundle:Role:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

路由的一部分 - role.yml

role_new:
    path:     /new
    defaults: { _controller: "BISSAPUserBundle:Role:new" }
role_create:
    path:     /create
    defaults: { _controller: "BISSAPUserBundle:Role:create" }
    requirements: { _method: post }

角色类型的一部分.php

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('role')
            ->add('description')
            ->add('groupRole',
                      'entity',
                       array(
                             'class'=>'BISSAP'UserBundle'Entity'GroupRole',
                             'property'=>'name',
                             'query_builder' => function ('BISSAP'USerBundle'Entity'GroupRoleRepository $repository)
                             {
                                 return $repository->createQueryBuilder('c');
                                        //->where('c.parent = :parent')
                                        //->setParameter('parent', 19)
                                        //->add('c.name', 'ASC');
                             }
                            )
                      )
            ->add('Enregistrer', 'submit', array(
                'attr' => array(
                'class' => 'blabla')));
            //->add('groupRole');
    }

所以,我不明白,为什么生成网址不起作用?!

真的很奇怪!似乎$this代表RoleType实例而不是RoleController

试试这个:

public function newAction()
{
    $entity = new Role();
    $url = $this->generateUrl('role_create');
    $form = $this->createForm(new RoleType(), $entity, array(
        'action' => $url,
        'method' => 'POST',
    ));
    return $this->render('BISSAPUserBundle:Role:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}