类'实体\用户'不存在?

时间:2018-03-18 05:22:32

标签: php doctrine-orm

我在文件夹[docx]中创建了一个新项目,并从文件夹实体中的现有数据库生成了实体。 这是fetch.php代码:

<?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;

以下是用户实体的代码:

<?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;
    }
}

当我尝试从此实体获取数据时,我收到错误:

第96行的

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

1 个答案:

答案 0 :(得分:1)

命名空间仅用于组织文件以避免冲突。如需帮助Link

您需要在代码中添加文件,如:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-core:11.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

有关更多信息,您需要检查doctrine autoload classes部分。