我是symfony的新手,我正在尝试用它启动一个应用程序。
我在中创建了以下doctrine orm定义 /src/AppBundle/Resources/config/doctrine/CustomerOnline.orm.yml
AppBundle\Entity\CustomerOnline:
type: entity
table: customer_online
uniqueConstraints:
customer_online_tracking_uindex:
columns:
- tracking
id:
tracking:
type: string
nullable: false
length: 15
options:
fixed: false
id: true
fields:
recipientPhone:
type: string
nullable: true
length: 15
options:
fixed: false
column: recipient_phone
repositoryClass: AppBundle\Repository\CustomerOnlineRepository
lifecycleCallbacks: { }
然后我使用doctrine:generate:entities命令来创建我的实体
我还使用了doctrine:generate:crud命令为我的实体生成crud控制器
当我导航到路径以将我的实体实例添加到数据库时,它会正确保存数据,但是当它重定向到/ customeronline / 1111111111 / show时显示实例时我收到错误
Attempted to call an undefined method named "getId" of class "AppBundle\Entity\CustomerOnline".
500 Internal Server Error - UndefinedMethodException
在控制器中,我看到它为newAction生成了以下代码
/**
* Creates a new CustomerOnline entity.
*
*/
public function newAction(Request $request)
{
$customerOnline = new CustomerOnline();
$form = $this->createForm('AppBundle\Form\CustomerOnlineType', $customerOnline);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($customerOnline);
$em->flush();
return $this->redirectToRoute('customeronline_show', array('id' => $customerOnline->getId()));
}
return $this->render('customeronline/new.html.twig', array(
'customerOnline' => $customerOnline,
'form' => $form->createView(),
));
}
但是在CustomerOnline实体中,它没有getId()或setId()函数,而是正确的getTracking()和setTracking($ tracking),因为我在CustomerOnline.orm.yml中将我的id字段定义为跟踪档案
我的问题是为什么生成器将getId()函数放在控制器而不是getTracking()中,因为tracking
是orm.yml定义中我的id的名称,我该如何更改它默认为那个?
负责生成该代码的twig模板原来就在这里
/**
{% block phpdoc_method_header %}
* Creates a new {{ entity }} entity.
{% endblock phpdoc_method_header %}
*
{% block phpdoc_method_annotations %}
{% if 'annotation' == format %}
* @Route("/new", name="{{ route_name_prefix }}_new")
* @Method({"GET", "POST"})
{% endif %}
{% endblock phpdoc_method_annotations %}
*/
{% block method_definition %}
public function newAction(Request $request)
{% endblock method_definition %}
{
{% block method_body %}
${{ entity_singularized }} = new {{ entity_class }}();
$form = $this->createForm('{{ namespace }}\Form\{{ entity_class }}Type', ${{ entity_singularized }});
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist(${{ entity_singularized }});
$em->flush();
{% if 'show' in actions -%}
return $this->redirectToRoute('{{ route_name_prefix }}_show', array('id' => ${{ entity_class|lower }}->getId()));
{%- else -%}
return $this->redirectToRoute('{{ route_name_prefix }}_index'));
{%- endif %}
}
{% endblock method_body %}
{% block method_return %}
return $this->render('{{ entity|lower|replace({'\\': '/'}) }}/new.html.twig', array(
'{{ entity_singularized }}' => ${{ entity_singularized }},
'form' => $form->createView(),
));
{% endblock method_return %}
}
我做的唯一改变是来自
$this->redirectToRoute('{{ route_name_prefix }}_show', array('id' => ${{ entity_class|lower }}->getId()));
到
return $this->redirectToRoute('{{ route_name_prefix }}_show', array('id' => ${{ entity_singularized }}->getId()));
因为变量名称在小写的情况下是错误的,系统说它没有定义