我在 class" Symfony 2.6 上收到错误 "Attempted to call method "generateUrl"
I get error "Attempted to call method "generateUrl" on class" Symfony 2.6
生成角色 CRUD 后(控制台 generate:doctrine:crud),我正在尝试使用 RoleType 覆盖表单,
但我收到错误:
Attempted to call method "generateUrl" on class "BISSAP\UserBundle\Form\RoleType".
CRUD 的一部分 - RoleController.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 }
RoleType.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');
}
所以,我不明白,为什么 generateUrl 不起作用?!
真奇怪!似乎 $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(),
));
}
生成角色 CRUD 后(控制台 generate:doctrine:crud),我正在尝试使用 RoleType 覆盖表单,
但我收到错误:
Attempted to call method "generateUrl" on class "BISSAP\UserBundle\Form\RoleType".
CRUD 的一部分 - RoleController.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 }
RoleType.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');
}
所以,我不明白,为什么 generateUrl 不起作用?!
真奇怪!似乎 $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(),
));
}