【问题标题】:Repository->findAll gives unknown database errorRepository->findAll 给出未知数据库错误
【发布时间】:2018-01-24 12:45:38
【问题描述】:

我刚开始使用 symfony。我用树枝、学说制造者安装了 SF4……

我创建了我的数据库“Autotrader”,我创建了一个迁移并迁移了它。

但是每当我尝试从数据库中获取数据时,我都会收到以下错误:

这是我正在使用的代码:

<?php

namespace App\Controller;

use App\Entity\Car;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class CarController extends Controller
{

    public function index()
    {
        $repository = $this->getDoctrine()->getRepository('App:Car');
        $cars = $repository->findAll();

        return $this->render('Car/list.html.twig', array(
            "title" => "Our cars",
            'cars' => $cars
        ));
    }
}

参数配置:

parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): ''

doctrine:
    dbal:
        # configure these for your database server
        driver: 'mysql'
        server_version: '5.7'
        charset: 'utf8mb4'
        dbname:               'autotrader'
        host:                 '127.0.0.1'
        port:                  3306
        user:                 'root'
        password:             'secret'

        # With Symfony 3.3, remove the `resolve:` prefix
        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

【问题讨论】:

  • 那里写着:“未知数据库:Autotrader”
  • 是的,但是如果我检查 Sequel Pro,数据库就在那里
  • 请给我们您的参数配置
  • doctrine.yaml 添加:)

标签: php symfony doctrine


【解决方案1】:

这是未知的数据库错误。

您确定您已经在正确的 localhost 路径和在教义.yml 文件中提到的端口号创建了 Autotrader db?

另外,在你的学说.yml 中替换 dbname: 'autotrader'dbname: Autotrader 再试一次!!

【讨论】:

  • 我编辑了我的数据库,所以它的名字没有大写,这就是为什么它说'autotrader'。如前所述,路径应该是正确的,因为它与我用于使用 cli 创建和迁移的路径相同
  • 您可能会犯一些错误,因为错误非常明显 - 它没有在 mysql:://root:secret@120.0.0.1:3306 位置找到名为 Autotrader 的数据库。
【解决方案2】:

我发现了问题所在。 我在 vagrant/homestead 中运行我的应用程序并迁移到本地,难怪找不到数据库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 2014-02-01
    相关资源
    最近更新 更多