I want to build a site where users can log in register and that stuff. For the User management i use FOSUserbundle. Now i want to use a different db connection for FOSUserBundle than for the other bundles. My config.yml file looks like:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database1_driver%"
host: "%database1_host%"
port: "%database1_port%"
dbname: "§database1_name%"
user: "%database1_user%"
password: "%database1_password%"
charset: UTF8
user:
driver: "%database2_driver%"
host: "%database2_host%"
port: "%database2_port%"
dbname: "%database2_name%"
user: "%database2_user%"
password: "%database2_password%"
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MyProjectMainBundle: ~
user:
connection: user
mappings:
MyProjectUserBundle: ~
When i try to load the page i get the error MappingException: The class 'MyProject\UserBundle\Entity\User' was not found in the chain configured namespaces MyProject\MainBundle\Entity, FOS\UserBundle\Model.
I followed the documentation for FOSUserBundle exactly and it is working if i use
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
in config.yml.
The only thing with this is, it only generates the table like defined in the new entity from the documentation. Only an id field is generated, and not the whole fos_user table like it should.
I know some similar questions have been asked before, but I tried using all the solutions from there and it didn't work. So how can I fix this? Is it even possible? I really need to use seperate databases because my project will use a lot of tables and i don't want it to get too messy.
user:
connection: user
mappings:
FOSUserBundle: ~
MyProjectUserBundle: ~
Need to add the FOSUserBundle to your mappings to get rid of the entity error
And make sure you have the model_manager_name set in config.ym;
fos_user:
db_driver: orm
firewall_name: main
user_class: Cerad\Bundle\AccountBundle\Entity\AccountUser
model_manager_name: user
Related
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
host: 127.0.0.1
port: 8889
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PWD)%'
dbname: '%env(DATABASE_NAME)%'
# 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: yml
dir: '%kernel.project_dir%/config/doctrine'
prefix: 'App\Entity'
alias: App
You need to use driver pdo_sqlsrv or sqlsrv, not pdo_mysql.
(See doctrine configuration documentation).
pdo_sqlsrv: A Microsoft SQL Server driver that uses pdo_sqlsrv PDO
sqlsrv: A Microsoft SQL Server driver that uses the sqlsrv PHP
extension.
Available configuration for pdo_sqlsrv/sqlsrv are:
user (string): Username to use when connecting to the database.
password (string): Password to use when connecting to the database.
host (string): Hostname of the database to connect to.
port (integer): Port of the database to connect to.
dbname (string): Name of the database/schema to connect to.
The documentation says that:
The following drivers support automatic database platform detection
out of the box without any extra configuration required:
pdo_mysql
mysqli
pdo_pgsql
pdo_sqlsrv
sqlsrv
So you dont need to specify server_version.
You would have:
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_sqlsrv'
charset: utf8mb4
host: 127.0.0.1
port: 8889
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PWD)%'
dbname: '%env(DATABASE_NAME)%'
# 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: yml
dir: '%kernel.project_dir%/config/doctrine'
prefix: 'App\Entity'
alias: App
I am trying to follow an e-commerce tutorial where I have to create the connection with the database. I am on windows 7 with xampp v3.2.
So I use this : php bin/console generate:doctrine:entity
which gives me this error :
SQLSTATE[HY000] [2002] no connection could be made because the target
machine actively refused it
I closed xampp and I still had the same error. So I understand it comes from the configuration; somehow my shell doesn't communicate with my sql server from xampp.
here is my parameters.yml :
# This file is auto-generated during the composer install
parameters:
database_host: localhost
database_port: 3306
database_name: market
database_user: sebastian
database_password:
mailer_transport: smtp
mailer_host: localhost
mailer_user: null
mailer_password: null
secret:
and here my config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: "#EcommerceBundle/Resources/config/services.yml" }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
handler_id: session.handler.native_file
save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"
fragments: ~
http_method_override: true
assets: ~
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# 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
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
I checked for the extension php_pdo_mysql.dl and it is enabled.
I verified the database name, user and password.
here is a netstat :
netstat
Make sure that your MySQL server is running and that it's using that port (in xampp\mysql\bin\my.ini). Make sure that you're able to connect manually with those credentials as well.
Also, where are you specifying the database driver in your parameters.yml? Normally you should have something like this:
database_driver: pdo_mysql
And lastly, make sure that you don't have a different parameters.yml file included in your config_dev.yml because Symfony commands, by default, use the dev environment.
thank you for your answer.
I have in my.ini 3306 port so it is the good one. I have intalled symfony2.8 and it is working so there is no credentials problems. I also tried to add the line with pdo_mysql but the error message is an pdo_exception, that means pdo works as well.
I also check config.dev but I don't really know what can be wrong inside. So I show you what it looks like :
imports:
- { resource: config.yml }
framework:
router:
resource: "%kernel.root_dir%/config/routing_dev.yml"
strict_requirements: true
profiler: { only_exceptions: false }
web_profiler:
toolbar: true
intercept_redirects: false
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: [!event]
console:
type: console
channels: [!event, !doctrine]
# uncomment to get logging in your browser
# you may have to allow bigger header sizes in your Web server configuration
#firephp:
# type: firephp
# level: info
#chromephp:
# type: chromephp
# level: info
#swiftmailer:
# delivery_address: me#example.com
thank for your help
i cant connect to mysql on xamppp
install mysql 8 on my system on port 3307
and its worked great with laravel.
if you want please install mysql workbench (instead of phpmyadmin)
I work on a project with Symfony2 and for now i can authenticate on my website.
But my problem is now when a user want to authenticate, i want to use a another database for some reasons.
So how can i say to symfony to use for example the db2 for authenticate and the db1 for the website?
If anyone can help me, thanks!
EDIT:
Thank's for your answer, i write this:
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
ContactBundle: ~
users:
connection: general
mappings:
UsersBundle: ~
But i've this error:
The class 'SymagContact\Bundle\UsersBundle\Entity\Users' was not found in the chain configured namespaces SymagContact\Bundle\SymagContactBundle\Entity
Sure you can have more than one EntityManager:
You have to setup your DBAL connections like this:
# 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
db2:
driver: "%db2_database_driver%"
host: "%db2_database_host%"
port: "%db2_database_port%"
dbname: "%db2_database_name%"
user: "%db2_database_user%"
password: "%db2_database_password%"
Then in the ORM section you can map your UserBundle namespace to the db2 connection:
doctrine:
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
AcmeStoreBundle: ~
customer:
connection: db
mappings:
AcmeUserBundle: ~
The rest is just the same: Have a UserProvider registered that follows the UserProviderInterface.
See http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html for more information.
I am using single database connection. Now I need to use two database connection. I need to know the steps to create two databases. I have lot of doubts. Generate entity commands are same what we using for single db connection or not.
Please explain step by step process for creating multiple entities and multiple db connection.
In /Symfony/app/config/config.yml i write the code:
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
customer:
driver: %database_driver2%
host: %database_host2%
port: %database_port2%
dbname: %database_name2%
user: %database_user2%
password: %database_password2%
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
dbBundle: ~
client:
connection: client
mappings:
dbBundle: ~
I found the answer :
Add namespace before the bundle name
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
<Namespace>dbBundle: ~
client:
connection: client
mappings:
<Namespace>dbBundle: ~
I have a problem with Django database configuration with 'postgresql_psycopg2'
If I give any arbitrary name to my database like below,
DATABASES = {
'default': {
'ENGINE': 'postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'xyz', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': '${my_password}', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
'OPTIONS': {
"autocommit": True,
}
}
}
>
OperationalError at /
FATAL: database "xyz" does not exist
I surf a lot and findout same that with SQLite we have to specify absolute path to our database; with PostGRE likewise above.
I would like to know:
1) Why I am getting error message with above specification and
2) How I use my database which i am using with Development server lay out in filesystem (windows).
I surf a lot and findout same that
with SQLite we have to specify
absolute path to our database; with
PostGRE likewise above.
Just to be clear (your wording isn't) with postgresql there is no absolute path, it's just the name of the database.
1: The error message is pretty clear, database xyz does not exist.
Go to the dbshell (python manage.py dbshell) and type in \l. If database xyz isn't in there, type in create database xyz, exit the postgres shell and try syncdb again.
2: For your development server, I recommend using sqlite3 for its ease of use.
sqlite is included in python 2.5+, so no extra settings are required. Simply set your ENGINE to sqlite3, specify an absolute path to where you want the database saved, and python manage.py syncdb