0

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...

I've created a 'Userbundle', and i want to display the info profile of an user.

When I access to the correct URL I have the famous message error:

"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."

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:

// 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.

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.

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?. Image:http://es.tinypic.com?ref=r0s8k5

IMPORTANT: When I generated the entity USer by console, I say "no" when asked to generate repository. May be this is the problem. Any suggestion now? Thanks again

2 Answers2

0

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

Community
  • 1
  • 1
Dezigo
  • 3,220
  • 3
  • 31
  • 39
0

Found it!!

It's was a silly mistake. I've began the PHP repository file with

<?
    ...

and must be

<?php
...

Sorry at all!