Error create database with shell - php

I come here for the first time because I have a problem when I want to create my database with doctrine and Symfony. When I want export my classes in my database I have this problem. I think everything is okay, doctrine find my base but can't create it. Also, I created my database named "symfony" in phpmyadmin.
I hope someone can help me, thank you so much :).
php app/console doctrine:database:create
Could not create database for connection named `symfony`
An exception occurred while executing 'CREATE DATABASE `symfony`':
SQLSTATE[HY000]: General error: 1007 Can't create database 'symfony'; database e
xists
Configuration file:
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: null
database_name: symfony
database_user: root
database_password: null
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: en
secret: 255a33a57b471045aa29326785659c6b0
database_path: null
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true

To summarize:
The error informs you that a database with the same name was already created. There are three options:
(1) Delete your current database without saving it, using MySQL command line. If you want to delete your database run: DROP database name_database
(2) Delete the database without saving it, using console: C:\wamp\www\Symfony>php app/console doctrine:database:drop
(3) In the case you like to save the content of your current database. Rename you current database. As far as I know you cannot do this directly. This is a work around. First, create a new database using command line: CREATE DATABASE name_old_database;. Second, copy all tables to the new database RENAME TABLE name_database.name_table TO name_old_database.name_table;. Finally,DROP DATABASE name_database
See further info: How do I quickly rename a MySQL database (change schema name)?

Related

Create Entity Manager in Symfony3?

I have an existing entity in a Symfony3 project that I am trying to add an entity manager to. My doctrine.yml file looks like this
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
port: 3306
dbname: fullstackdb
user: root
password: root
charset: UTF8
lego:
driver: pdo_mysql
host: localhost
port: 3306
dbname: fullstackdb
user: root
password: root
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
auto_mapping: true
default:
connection: default
mappings:
lego:
connection: lego
mappings:
AppBundle: ~
However anytime I try to access this entity manager through php bin/console doctrine:database:create --connection=lego
, it says the manager does not exist! I'm not sure if I'm missing a step - the only thing I've done to create the manager is to make the yml file.
It doesn't show up when I run bin/console debug:container.
Grateful for any help!
1) You are using the same schema name (database name) on the same host in two different entity managers without schema_filters. This means that when you do an update to one of the entity managers it will attempt to delete the data of the other entity manager.
2) You are asking about a entity manager but in the command you are passing a connection.
3) Run php bin/console debug:container | grep doctrine
This will give you all the doctrine services. Update your question accordingly.
4) Provide the exact command and exact error you get so we can track it down to the origin
5) For a more information run the command in verbose mode adding -v at the end
6) Are you importing your doctrine.yml in your config.yml ?

Symfony2 gives a ParameterNotFoundException on a database driver

Lately I have created an extra database with it's own user. Therefore I have created an extra database driver in the parameters.yml. This is, as far as I know, the standard approach for this kind of situations. So far, it works. In one of the services I created, I can use this database driver. When running the code on the website, there are no problems at all.
But of course there is a problem, otherwise I won't asking for your guys help.
I'm trying to install a plugin by running the following command:
$ ./composer.phar require pugx/autocompleter-bundle
This gives the following error:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database_driver_geo". Did you mean this: "database_driver"?
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-update-cmd event terminated with an exception
Installation failed, reverting ./composer.json to its original content.
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command.
Some other posts say that the error about the cache has something to do with the file/dir rights. But that doesn't seem to be the problem, because when the configuration of the geo driver is removed, these kind of errors do not appear.
I'm running Symfony 2.5
[EDIT: Added parameters.yml file]
My parameters.yml looks like this:
# This file is auto-generated during the composer install
parameters:
# Default database
database_driver: pdo_mysql
database_host: ***
database_port: ***
database_name: ***
database_user: ***
database_password: ***
# Geo database
database_driver_geo: pdo_mysql
database_host_geo: ***
database_port_geo: ***
database_name_geo: ***
database_user_geo: ***
database_password_geo: ***
mailer_transport: ***
mailer_host: ***
mailer_user: ***
mailer_password: ***
locale: ***
secret: ***
[EDIT: Added config.yml file]
The doctrine section in the config.yml file:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
mapping_types:
enum: string
bit: integer
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: %kernel.root_dir%/data/data.db3
# path: %database_path%
geo:
driver: %database_driver_geo%
host: %database_host_geo%
port: %database_port_geo%
dbname: %database_name_geo%
user: %database_user_geo%
password: %database_password_geo%
charset: UTF8
mapping_types:
enum: string
bit: integer
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: %kernel.root_dir%/data/data.db3
# path: %database_path%
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
***CoreBundle: ~
geo:
connection: geo
mappings:
***GeoBundle: ~
auto_generate_proxy_classes: %kernel.debug%
I hope there's someone that can help me fix this problem.
Kind regards,
Malcolm
As mentioned in comments, parameters.yml file is autmatically rebuilt after composer update or install command. You can see that in your composer.json file in scripts section:
"scripts": {
"post-install-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
// other commands...
],
"post-update-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
// other commands...
]
},
You can of course turn off this feature if you don't like it. But it may be useful when used properly.
Therefore when you install some package via composer you're losing parameters that you put directly into parameters.yml.
What you should do is to make use of parameters.yml.dist file which is used to build parameters.yml. It should provide application parameters values (if they are the same for every instance of app) or default values if parameters are different for every environment (prod/dev).
In your case it's the second use case (default values), since DB credentials will change for every server. It's actually exactly the same as configuration of default DB connection. parameters.yml.dist contains some default values for these params.

Symfony docker container can't connect to Mysql

My stack is quite common, one container for my Symfony app, another one for Mysql.
For some reason, Symfony can't connect to Mysql container with following error :
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
A simple php file to test the MySQL connection, on the Symfony container works with this
<?php
$dbh = new PDO('mysql:host=database;dbname=mysite', 'mysite', 'password');
Here is my Symfony parameters file:
database_driver: pdo_mysql
database_host: database
database_port: 3306
database_name: mysite
database_user: mysite
database_password: password
And here is my docker-compose file :
site:
build: webapp
ports :
- "80:80"
volumes:
- /home/myuser/dev/mysite:/var/www/html/
links:
- database
database:
image: mysql:5.5
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=mysite
- MYSQL_USER=mysite
- MYSQL_PASSWORD=password
I also tried with a fresh Symfony project without error.
Remove the database driver and port to use. As you didn't use port forwarding, you don't have to specify the port for MySQL. Also, there's a mistake into your driver, it's not just pdo but pdo_mysql (documentation).
database_host: database
database_name: mysite
database_user: mysite
database_password: password
Also, why do you specify a database_path ? MySQL doesn't need because you call it by TCP/IP. Old SQLite database ?
You could also checkout your config.yml file :
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 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

Connect Doctrine to a MS SQL Database

Just before i get started, I have been trying to figure this out and have clicked nearly every google link there is and I have read nearly all the other questions on this. But I'm stuck because the bundles that are being suggested for this are out of date.
I am creating a website. Tt is a symfony2 application that is being hosted on Microsoft azure. What I want to do is be able to use doctrine of course to fetch and create users to and from the database.
Now from what I've been reading, to connect to this type of database, I have to use the driver called "PDO_dblib".
I have installed this bundle as it seems to be the only one thats still active, don't quote me on that.
https://github.com/realestateconz/MssqlBundle
Now, I installed this in my vendor folder, is this the correct place to store it? Like so:
Project/
app/
src/
vendor/
realestate/
Of course i added the bundle to the AppKernel like so:
new Realestate\MssqlBundle\RealestateMssqlBundle();
and last but not least here is what i have in my config file:
doctrine:
dbal:
default_connection: default
connections:
default:
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
host: %database_host%
dbname: %database_prefix%%database_name%
user: %database_user%
password: %database_password%
so what im thinking im doing here is telling doctrine to use this driver? dont see what else it could be.
I have also declared my parameters.yml for the connection settings.
PS: I am doing my Dev on Linux Mint!
before i tried the steps of this bundle i also ran throught this websites steps: https://dunglas.fr/2014/01/connection-to-a-ms-sql-server-from-symfony-doctrine-on-mac-or-linux/
But again it was throwing errors, I'll post the errors I get down below!
So for the record I have installed freetds and php5-sybase.
The errors that I am getting are such:
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: class_implements(): Class Realestate\MssqlBundle\Driver\PDODlib\Driver does not exist and could not be loaded
and also this when i try to do a:
php app/console doctrine:database:create
(I do have my entity set up)
but i get the following error from the command of create:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database_prefix". Did you mean one of these: "database_port", "database_user"?
I have been trying to get this working for the past few days and any help would be fantastic! Any more information needed feel free to ask of course!
According your error code:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database_prefix". Did you mean one of these: "database_port", "database_user"?
It seems that you miss configuring the parameter “database_prefix”.
If the tables in your database have the unitized prefix e.g. “core_user”,”core_tasks”… You can configure this parameter in the file parameters.yml if not, you can just remove the parameter % database_prefix % in the dbname line in file config.yml.
Here are my code snippets for your reference:
Config.yml:
doctrine:
dbal:
default_connection: default
connections:
default:
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
parameters.yml:
parameters:
database_host: {your_sql_server_name}.database.windows.net
database_port: 1433
database_name: {database_name}
database_user: {username}
database_password: {password}
And the test query in controller:
$conn = $this->get('database_connection');
$data = $conn->fetchAll('SELECT * FROM Testtable');
var_dump($data);

Entity Generation for Single Table

So I want to generate entities for specific tables in a database that has a huge amount of tables.
Here is the command I'm trying:
php app/console doctrine:mapping:convert yml ./src/MyNamespace/Bundle/MyNamespaceBundle/Resources/config/doctrine/metadata/orm --from-database --em=my_manager --filter=TblReports --verbose
Here is the error:
[Doctrine\DBAL\DBALException]
Unknown database type unknown requested, Doctrine\DBAL\Platforms\PostgreSqlPlatform may not support it.
Now I can run this command on a smaller database with only a few tables and it works fine, so it has to be the database.
Yes I want to filter this one table:
--filter=TblReports
If I remove the filter it generates entities for the entire database, which is what I don't want.
I'm running PostgreSQL 8.4 and 9.1 if that matters.
Anyone else have or know how to fix this issue?
Unknown database type unknown requested
Doctrine Docs
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/tools.html#reverse-engineering
Releated:
Generating a single Entity from existing database using symfony2 and doctrine
UPDATE: Adding my_manager ( config.yml )
# Doctrine Configuration
doctrine:
dbal:
default_connection: my_database
connections:
my_database:
driver: pdo_pgsql
port: 5432
dbname: tbl_reports
user: foo_user
password: foo_pass
charset: UTF8
mapping_types:
bit: string
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: my_manager
entity_managers:
my_manager:
connection: my_database
mappings:
MyNamespaceBundle:
mapping: true
dir: Entity/Reports
in config_dev.yml ( I use the dev and prod yml files to control the host(s) I can connect to )
# Doctrine Configuration
doctrine:
dbal:
connections:
my_database:
host: 172.0.0.1
I agree with Ziumin that it's likely an issue with the data type in your Postgres database. Possibly an Enum or similar.
What I would do is try to convert the tables one by one until you narrow it down to the problem table, and then look at the data types and it should be fairly obvious.

Categories