未知的实体名称空间别名“ AppBundle”和“类'App \\ Entity \\ Product”不存在

时间:2018-07-22 15:43:49

标签: symfony doctrine repository fosrestbundle

我正在尝试实现FOSRestBundle,所以我刚刚创建了我的一流控制器。

    <?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;
use App\Entity\Product;

class ProductController extends FOSRestController
{
    /**
     * @Rest\Get("/product")
     */
    public function getAction()
    {
        $em = $this->getDoctrine()->getManager();
        $restresult = $em->getRepository('AppBundle:Product')->getAllProduct();
        if ($restresult === null) {
            return new View("there are no products exist", Response::HTTP_NOT_FOUND);
        }
        return $restresult;
    } // "Get products"     [GET] /product*/
}

但是Symfony服务器给我这个错误: -“未知实体名称空间别名'AppBundle'。” -“ Doctrine \ ORM \ ORMException”;

我的composer.json是:

{
    "name": "symfony/framework-standard-edition",
    "license": "MIT",
    "type": "project",
    "description": "The \"Symfony Standard Edition\" distribution",
    "autoload": {
        "psr-4": {
            "AppBundle\\": "src/AppBundle"
        },
        "psr-0": {"": "src/"},
        "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
    },
    "autoload-dev": {
        "psr-4": { "Tests\\": "tests/" },
        "files": [ "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php" ]
    },
    "require": {
        "php": ">=5.5.9",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/orm": "^2.5",
        "friendsofsymfony/rest-bundle": "^2.3",
        "incenteev/composer-parameter-handler": "^2.0",
        "jms/serializer-bundle": "^2.4",
        "nelmio/cors-bundle": "^1.5",
        "sensio/distribution-bundle": "^5.0.19",
        "sensio/framework-extra-bundle": "^5.0.0",
        "symfony/monolog-bundle": "^3.1.0",
        "symfony/polyfill-apcu": "^1.0",
        "symfony/swiftmailer-bundle": "^2.6.4",
        "symfony/symfony": "3.4.*",
        "twig/twig": "^1.0||^2.0",
        "symfony/orm-pack": "^1.0"
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0",
        "symfony/phpunit-bridge": "^4.1"
    },
    "scripts": {
        "symfony-scripts": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-install-cmd": [
            "@symfony-scripts"
        ],
        "post-update-cmd": [
            "@symfony-scripts"
        ]
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-bin-dir": "bin",
        "symfony-var-dir": "var",
        "symfony-web-dir": "web",
        "symfony-tests-dir": "tests",
        "symfony-assets-install": "relative",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        },
        "branch-alias": {
            "dev-master": "3.4-dev"
        }
    }

产品实体类为:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Product
 *
 * @ORM\Table(name="Product")
 * @ORM\Entity
 */
class Product
{
    /**
     * @var integer
     *
     * @ORM\Column(name="idProduct", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idproduct;

    /**
     * @var string
     *
     * @ORM\Column(name="productName", type="string", length=100, nullable=false)
     */
    private $productname;

    /**
     * @var boolean
     *
     * @ORM\Column(name="Purchased", type="boolean", nullable=true)
     */
    private $purchased;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="User", mappedBy="productproduct")
     */
    private $useruser;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->useruser = new \Doctrine\Common\Collections\ArrayCollection();
    }

}

,然后ProductRepository文件为:

<?php

namespace AppBundle\Repository;

use Doctrine\ORM\EntityRepository;

class ProductRepository extends EntityRepository
{
    public function getAllProduct()
    {
        $conn = $this->getEntityManager()->getConnection();
        $sql = "SELECT * from product";
        $stmt = $conn->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }
}

我也尝试替换

    $restresult = $em->getRepository('AppBundle:Product')->getAllProduct();

    $restresult = $em->getRepository(Product::class)->getAllProduct();

,但现在的错误是: -“类'App \ Entity \ Product'不存在” -“ Doctrine \ Common \ Persistence \ Mapping \ MappingException”

我已经尝试过其他堆栈超低用户的解决方案,但是没有任何解决方案。 有人可以帮我吗?

3 个答案:

答案 0 :(得分:0)

  

use App\Entity\Product;

这应该是罪魁祸首。 (在您的控制器中)

编辑:对不起,错过了第一个错误。您可以发布您的学说配置吗?是自动映射吗?

答案 1 :(得分:0)

在ProductController文件中

代替

use App\Entity\Product;

替换为

use AppBundle\Entity\Product;

答案 2 :(得分:0)

您的目录结构错误。 https://github.com/axeowl/projectKobe/tree/master/src

实体和仓库目录应位于AppBundle中。