Search code examples
symfonyfosuserbundle

Error 'Unknown Entity namespace alias' when using 'CompanyNameBundle:User'


I have a strange problem with Symfony2. I'm working on a project with 40-50 entities, and with one of them this code doesn't work:

$user = $em->getRepository('CompanyUserBundle:User')
    ->findOneBy([
        'username' => $person->getUsername()
    ]);

I get the error Unknown Entity namespace alias 'CompanyUserBundle'.. The strange thing is that if I change my code to:

$user = $em->getRepository('Company\UserBundle\Entity\User')
    ->findOneBy([
        'username' => $person->getUsername()
    ]);

It works perfectly... So, what's wrong here? I'm using the getRepository('Alias:Entity') construction all around the code, and there isn't any other problem...

I don't know if it's important, but User is a child entity from FOSUserBundle BaseUser class.


EDIT

Here is my User class (it's located at src/Company/UserBundle/Entity):

namespace Company\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="fos_user")
 * @ORM\Entity
 */
class User extends BaseUser
{
    /**
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

   /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

}

And here a part of my AppKernel.php:

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            /* ... other, non interesting, bundles... */
            new Company\UserBundle\UserBundle(),
            new FOS\UserBundle\FOSUserBundle(),
            new FR3D\LdapBundle\FR3DLdapBundle(),
            new JMS\AopBundle\JMSAopBundle(),
            new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
            new JMS\DiExtraBundle\JMSDiExtraBundle($this),
        );
    }
}

More information: as @herr said in the comments, using

$user = $em->getRepository('UserBundle:User')
    ->findOneBy([
        'username' => $person->getUsername()
    ]);

works fine... But I don't understand why. Why in this bundle the namespace alias is UserBundle instead of CompanyUserBundle? I know that this must be a really silly error, but I can't see it...


Solution

  • You can check in your UserBundle's Bundle class file it will with UserBundle.php instead of CompanyUserBundle.php.

    Just try using

    $em->getRepository('UserBundle:User')
    

    instead

    $em->getRepository('CompanyUserBundle:User')
    

    I think it's namespace / BundleClassName may be only UserBundle not CompanyUserBundle in this case.

    Thanks.