0

I have an issue with symfony. when i execute this code

$quiz = new Quiz();
$quiz->setTitle('A quiz.');
$quiz->setAuthor('Alexandre');
$quiz->setContent("Blabla…");
$em = $this->getDoctrine()->getManager();
$em->persist($quiz);

Symfony show me this error

An exception occured in driver: SQLSTATE[HY000] [1049] Unknown database 'quiz' 

But if i try to create the database it says that the database exists

php bin/console doctrine:database:create
Could not create database `symfony` for connection named default
An exception occurred while executing 'CREATE DATABASE `symfony`':

SQLSTATE[HY000]: General error: 1007 Can't create database 'symfony'; database exists

And

php bin/console doctrine:schema:update --force
Nothing to update - your database is already in sync with the current entity metadata.

This is my app/config/parameters.yml file

# This file is auto-generated during the composer install
parameters:
    database_host: localhost
    database_port: ~
    database_name: symfony
    database_user: root
    database_password: ~
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    secret: 43c1fdc9712102f8b9c3e33ed1c46befd3dae9ab

Thank you for your help

3
  • Your database already exists, no need to run database:create. Have you created your Quiz class with your annotations to create the Quiz table? Commented Dec 16, 2015 at 0:11
  • The first error message indicates that doctrine is looking for a database called quiz? Check your config file and maybe the table name in your Quiz entity mapping. Also try removing the cache. I assume you copied/pasted the error. Or was it really unknown table quiz? In which case you need a doctrine:schema:create to make the quiz table. Commented Dec 16, 2015 at 0:58
  • Please add your doctrine mapping file (or your entity class with annotations). Obviously your database name differs from the name 'quiz'. It's probably a small mistake in your mapping. Commented Mar 23, 2016 at 15:51

2 Answers 2

1

You have bad configuration your database name is symfony already exist

put this configuration in your parameters.yml

parameters: 
    database_host: localhost
    database_port: ~ 
    database_name: quiz
    database_user: root
    database_password: ~ 
    mailer_transport: 
    smtp mailer_host: 127.0.0.1
    mailer_user: null 
    mailer_password: null 
    secret:
43c1fdc9712102f8b9c3e33ed1c46befd3dae9ab

Next execute in terminal project directory

php bin/console doctrine:database:create

If not have the entity quiz create this with command

php bin/console doctrine:generate:entity AppBundle/Quiz

Update your database

php bin/console doctrine:schema:update --force

Final step execute you method from controller to persist and flush object in table quiz from database quiz

Sign up to request clarification or add additional context in comments.

Comments

0

STEP

  1. php bin/console doctrine:database:drop --force
  2. php bin/console doctrine:database:create
  3. bin/console doctrine:schema:update --force

Symfony doc http://symfony.com/doc/current/book/doctrine.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.