flik
flik

Reputation: 3643

Class 'entities\User' does not exist ?

I have created a new project in folder [docx] and generated entities from existing database in the folder entities. Here is fetch.php code:

<?php
// bootstrap.php
// replace with file to your own project bootstrap
require_once 'bootstrap.php';


$em->getConfiguration()->addEntityNamespace('', 'entities');

$UserRepo = $em->getRepository('User');


$data = $UserRepo->findAll();
echo '<pre>'; \Doctrine\Common\Util\Debug::dump($data);
exit;

Here is the code of User Entity:

<?php

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user", indexes={@ORM\Index(name="address_id", columns={"address_id"})})
 * @ORM\Entity
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="text", length=65535, nullable=false)
     */
    private $username;

    /**
     * @var \Address
     *
     * @ORM\ManyToOne(targetEntity="Address")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="address_id", referencedColumnName="id")
     * })
     */
    private $address;



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

    /**
     * Set username
     *
     * @param string $username
     *
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Get username
     *
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set address
     *
     * @param \Address $address
     *
     * @return User
     */
    public function setAddress(\Address $address = null)
    {
        $this->address = $address;

        return $this;
    }

    /**
     * Get address
     *
     * @return \Address
     */
    public function getAddress()
    {
        return $this->address;
    }
}

When I try to fetch data from this entity I am getting an error:

Fatal error: Uncaught Doctrine\Common\Persistence\Mapping\MappingException: Class 'entities\User' does not exist in D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php:96 Stack trace: #0 D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\RuntimeReflectionService.php(41): Doctrine\Common\Persistence\Mapping\MappingException::nonExistingClass('entities\\User') #1 D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php(281): Doctrine\Common\Persistence\Mapping\RuntimeReflectionService->getParentClasses('entities\\User') #2 D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php(311): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getParentClasses('entities\\User') #3 D:\xampp\htdocs\docx\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php(78): Doctrine\Common\Persistence\Mapping\Abstr in D:\xampp\htdocs\docx\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php on line 96

Upvotes: 2

Views: 1325

Answers (1)

Ashfaq
Ashfaq

Reputation: 269

Namespaces are just for organizing files to avoid conflicts. For help Link

You need to add files in your code like:

<?php
// bootstrap.php
// replace with file to your own project bootstrap
require_once 'bootstrap.php';
require_once 'entities/User.php';

$em->getConfiguration()->addEntityNamespace('', 'entities');

$UserRepo = $em->getRepository('User');


$data = $UserRepo->findAll();
echo '<pre>'; \Doctrine\Common\Util\Debug::dump($data);
exit;

For more information you need to check doctrine autoload classes section.

Upvotes: 1

Related Questions