简体   繁体   English

symfony 2.3错误:自动加载器找到文件但未找到类

[英]symfony 2.3 error: autoloader find file but not class

First, sorry if my english it's not so good. 首先,对不起,如果我的英语不好。 I readed a lot of questions like the one i have, but any solution works. 我读了很多问题,例如我遇到的问题,但是任何解决方案都可以。 The question is that I'm developing a porject in Symfony 2.3, yes, i'm beginner using it... 问题是我正在Symfony 2.3中开发一个对象,是的,我是初学者使用它。

I've created a 'Userbundle', and i want to display the info profile of an user. 我创建了一个“ Userbundle”,我想显示用户的信息资料。

When I access to the correct URL I have the famous message error: 当我访问正确的URL时,出现著名的消息错误:

"The autoloader expected class "Mylife\\UserBundle\\Entity\\UserRepository" to be defined in file "D:\\www\\Symfony/src\\Mylife\\UserBundle\\Entity\\UserRepository.php". The file was found but the class was not in it, the class name or namespace probably has a typo." “将在文件“ D:\\ www \\ Symfony / src \\ Mylife \\ UserBundle \\ Entity \\ UserRepository.php”中定义自动装带器预期的类“ Mylife \\ UserBundle \\ Entity \\ UserRepository”。找到了文件,但该类不在它,类名或名称空间可能有错字。”

That's my default controller code: 那是我的默认控制器代码:

namespace Mylife\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Mylife\UserBundle\Entity\User;
use Mylife\UserBundle\Form\Frontend\RegisterType;

class DefaultController extends Controller
{

public function profileAction(){
    $user_id=1;

    $em=$this->getDoctrine()->getEntityManager();
    $profile=$em->getRepository('UserBundle:User')
                ->findProfile($user_id);

    return $this->render('UserBundle:Default:profile.html.twig', array( 'profile' => $profile));

}

And my UserRepository.php code: 和我的UserRepository.php代码:

// src/Mylife/UserBundle/Entity/UserRepository.php

namespace Mylife\UserBundle\Entity;
use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{
    public function findProfile($user)
    {
    $em = $this->getEntityManager();

    $consult= $em->createQuery('
                               SELECT u, nk, n
                               FROM UserBundle:User u
                               WHERE u.user= :id');

    $consult->setParameter('id', $user);

    return $consult->getResult();
    }
}

I have the same problem when trying to use a form class in the same bundle, but i no see any error in namesapce or class name. 尝试在同一捆绑包中使用表单类时,我遇到相同的问题,但在namesapce或类名中看不到任何错误。

The project structure is: 项目结构为:

-src
   -Mylife
      -UserBundle
         ....
         -Entity
            ...
            -User.php
            -UserRepository.php

I'm going mad trying to solve the problem and reading a lot of forums and examples. 我很生气,试图解决问题并阅读许多论坛和示例。 I've tryed to dissable APC, to restart Apache, erase the cache, and nothing of this worked. 我试图禁用APC,重新启动Apache,清除缓存,但这些都没有用。

Thanks a lot!! 非常感谢!! Carlos 卡洛斯

PD: I'm not sure why appears a piece of code at the top of the error page and why it begins in "getEntityMAnager();..." row... Why is not showing the text code before it?. PD:我不确定为什么在错误页面的顶部会显示一段代码,为什么它会在“ getEntityMAnager(); ...”行中开始...为什么不显示文本代码? Image: http://es.tinypic.com?ref=r0s8k5 图片: http//es.tinypic.com?ref = r0s8k5

IMPORTANT: When I generated the entity USer by console, I say "no" when asked to generate repository. 重要说明:当我通过控制台生成实体USer时,当要求生成存储库时我说“否”。 May be this is the problem. 可能这就是问题所在。 Any suggestion now? 现在有什么建议吗? Thanks again 再次感谢

Try to add this comment in your User entity file: 尝试在用户实体文件中添加此注释:

/**
 *
 * @ORM\Entity(repositoryClass="YourProject\UserBundle\Entity\UserRepository")
 */
class User
{
...
}

or something like this: custom repository class in symfony2 或类似的东西: symfony2中的自定义存储库类

Found it!! 找到了!!

It's was a silly mistake. 这是一个愚蠢的错误。 I've began the PHP repository file with 我已经开始使用PHP存储库文件

<?
    ...

and must be 并且必须

<?php
...

Sorry at all! 不好意思!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM