找不到基本表或视图:1146表(Symfony)

时间:2018-07-23 14:52:50

标签: sql symfony doctrine-orm

大家好,我对一个教义的查询感到麻烦。 查询非常简单,并按以下方式进行管理:

<?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 AppBundle\Entity\Product;

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

回购文件为:

<?php

namespace AppBundle\Repository;

use Doctrine\ORM\EntityRepository;

class ProductRepository extends EntityRepository
{
    public function findAll()
    {
        $conn = $this->getEntityManager()->getConnection();
        $sql = "SELECT p.productName from Product as p ";
        $stmt = $conn->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }
}

相反,配置文件是:

doctrine:
    dbal:
        driver: pdo_mysql
        host: '%database_host%'
        port: '%database_port%'
        dbname: '%database_name%'
        user: '%database_user%'
        password: '%database_password%'
        unix_socket: /var/lib/mysql/mysql.sock
        server_version: '5.5'
        charset: UTF8
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: '%kernel.project_dir%/var/data/data.sqlite'
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #path: '%database_path%'

    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

但是当我执行查询时,Symfony给我这个错误:

  

执行'SELECT t0.idUser AS idUser_1,t0.email AS email_2,t0.username AS username_3,t0.password AS password_4 FROM User t0 INNER JOIN user_has_product ON t0.idUser = user_has_product.User_idUser WHERE user_has_product时发生异常。 Product_idProduct =?'带有参数[1]:\ n \ nSQLSTATE [42S02]:找不到基表或视图:1146表'projectKobe.user_has_product'不存在

我不明白如何解决此问题以及为什么查询不正确:“ SELECT p.productName FROM Product as p”。我没有做任何内在的加入。谢谢

1 个答案:

答案 0 :(得分:0)

它与ORM相关,因此请尝试更新数据库架构。

您可以在此处查看文档

官方文档 Native SQL

KNP大学的例子Doctrine Raw SQL Queries