Doctrine Symfony3试图调用名为“的未定义方法”;getId";“类的”;AppBundle\Entity\CustomerOnline“;

Doctrine Symfony3试图调用名为“的未定义方法”;getId";“类的”;AppBundle\Entity\CustomerOnline“;,doctrine,symfony,Doctrine,Symfony,我是symfony的新员工,我正在尝试用它启动一个应用程序 我在中创建了以下orm定义 /src/AppBundle/Resources/config/doctrine/CustomerOnline.orm.yml AppBundle\Entity\CustomerOnline: type: entity table: customer_online uniqueConstraints: customer_online_tracking_uindex: columns:

我是symfony的新员工,我正在尝试用它启动一个应用程序

我在中创建了以下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: {  }
然后我使用原则:generate:entities命令来创建我的实体

我还使用条令:generate:crud命令为我的实体生成一个crud控制器

当我导航到路由以将实体的实例添加到DB时,它会正确保存数据,但是当它重定向到/customeronline/1111/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字段定义为tracking

我的问题是,既然
tracking
是我的id在orm.yml定义中的名称,为什么生成器将getId()函数放在控制器中而不是getTracking()函数中?我如何将其更改为默认值

负责生成该代码的细枝模板最初与此处相同

/**
{% 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()));


因为变量名小写错误,而且系统说据我所知,它没有定义

,所以命令行的symfony CRUD也适用于通过命令行生成的实体。您需要将这些实体定义为带有注释的类。我明白了,但是有人说每种类型、注释、yml、xml都可以做相同的事情,即使使用注释是最佳实践,所以我认为这不是问题所在。实际上,我尝试了注释,但也遇到了同样的问题。就CRUD by命令行而言,我认为它还可以用于使用条令:映射:导入,然后是条令:生成:实体通过实体类和注释快速导入整个数据库,这是不正常的,但与您的方式有关,以前从未测试过。否则,您的错误来自您没有获得getter/setter的事实。但是,我再次道歉,不知道它如何与yml类定义一起工作。很好,我真的很感谢任何建议来解决这个问题。我对相同行为的意思是,如果我的类不使用名为
id
的自动生成的id,而是使用另一个名称
,例如tracking
,它们只会导致getId()丢失的错误。显然,我可以在类中创建函数getId()&setId($id),它只调用getTracking()和setTracking($tracking)函数,一切都正常工作。问题是生成器不会读取在类中实际设置为id的变量名,无论是注释、yml还是XML。据我所知,symfony CRUD by命令行也适用于通过命令行生成的实体。您需要将这些实体定义为带有注释的类。我明白了,但是有人说每种类型、注释、yml、xml都可以做相同的事情,即使使用注释是最佳实践,所以我认为这不是问题所在。实际上,我尝试了注释,但也遇到了同样的问题。就CRUD by命令行而言,我认为它还可以用于使用条令:映射:导入,然后是条令:生成:实体通过实体类和注释快速导入整个数据库,这是不正常的,但与您的方式有关,以前从未测试过。否则,您的错误来自您没有获得getter/setter的事实。但是,我再次道歉,不知道它如何与yml类定义一起工作。很好,我真的很感谢任何建议来解决这个问题。我对相同行为的意思是,如果我的类不使用名为
id
的自动生成的id,而是使用另一个名称
,例如tracking
,它们只会导致getId()丢失的错误。显然,我可以在类中创建函数getId()&setId($id),它只调用getTracking()和setTracking($tracking)函数,一切都正常工作。问题是生成器不会读取在类中实际设置为id的变量名,无论是注释、yml还是xml
 return $this->redirectToRoute('{{ route_name_prefix }}_show', array('id' => ${{ entity_singularized }}->getId()));