I am working on an Guard authentication with 2 separate entity managers.
Here my config:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
backoffice:
driver: pdo_mysql
host: '%database_backoffice_host%'
port: '%database_backoffice_port%'
dbname: '%database_backoffice_name%'
user: '%database_backoffice_user%'
password: '%database_backoffice_password%'
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
CarBundle: ~
backoffice:
connection: backoffice
mappings:
BackofficeBundle: ~
This is my security setting
services:
backoffice.form_login_authenticator:
class: BackofficeBundle\Security\FormLoginAuthenticator
arguments: ["#router", "#security.password_encoder", "#doctrine.orm.backoffice_entity_manager"]
After the login form, the user is found and there is an redirect to my main route /backofice , but I become this error:
[2017-05-07 18:32:56] request.INFO: Matched route "backoffice_home". {"route":"backoffice_home","route_parameters":{"_controller":"BackofficeBundle\\Controller\\DefaultController::indexAction","_route":"backoffice_home"},"request_uri":"http://ip/backoffice","method":"GET"} []
[2017-05-07 18:32:56] security.DEBUG: Read existing security token from the session. {"key":"_security_main"} []
[2017-05-07 18:32:56] request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\ORMException: "Unknown Entity namespace alias 'BackofficeBundle'." at /home/metahub/autotrader/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php line 271 {"exception":"[object] (Doctrine\\ORM\\ORMException(code: 0): Unknown Entity namespace alias 'BackofficeBundle'. at /home/metahub/autotrader/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php:271)"} []
The solution is this:
security:
encoders:
BackofficeBundle\Entity\User: bcrypt
providers:
database_users:
entity:
class: BackofficeBundle:User
property: username
manager_name: backoffice
We need to setup the manager "manager_name" for this Entity direct in the security settings.
try using auto_mapping: true
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
auto_mapping: true
mappings:
AppBundle: ~
CarBundle: ~
backoffice:
connection: backoffice
auto_mapping: true
mappings:
BackofficeBundle: ~
Related
I am trying to work with 2 DBs and then 2 entity managers.
Here is my config doctrine part:
doctrine:
dbal:
default_connection: g2s
connections:
main:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
api:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: api
user: '%database_user%'
password: '%database_password%'
charset: UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
entity_managers:
default:
connection: main
auto_mapping: true
naming_strategy: doctrine.orm.naming_strategy.underscore
api:
connection: api
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
ApiBundle: ~
I ran the command "config:dump-reference" and the bundle is mapped.
But when trying to browse any page I got this error:
Unknown Entity namespace alias 'ApiBundle'
According to docs, the proper way is to explicitly set all mappings, and this is what I do in my project as well. https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
There was a thread about this issue, and it should work for Symfony 2.6+ but same as #Cerad, I had no luck in enabling this. https://github.com/doctrine/DoctrineBundle/issues/60
I was able to connect to doctrine in symfony2, this way:
# 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
orm:
auto_generate_proxy_classes: false
proxy_namespace: Proxies
proxy_dir: Proxies
default_entity_manager: default # The first defined is used if not set
entity_managers:
default:
connection: default
mappings: # Required
ApplicationUserBundle:
type: annotation
class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
dql:
datetime_functions:
UNIX_TIMESTAMP: DoctrineExtensions\Query\Mysql\UnixTimestamp
Now my question is that how to have this in configuration above?
$config->setSQLLogger(new Doctrine\DBAL\Logging\EchoSQLLogger());
I mean another way than this. As this is a $config method I guess that should be possible to have it in config.yml, but how?
You need to fill few config parameters
In doctrine.yaml config file enable logging
doctrine:
dbal:
logging: true
In services.yaml config file set logger class
services:
doctrine.dbal.logger:
class: Doctrine\DBAL\Logging\EchoSQLLogger
Do not forget to clean Symfony cache before usage php bin/console cache:clear
I am struggling to define entity mapper found here:
https://github.com/Payum/PayumBundle/blob/master/Resources/doc/capture_funds_with_paypal_express_checkout.md#2-a-configure-doctrine-storage
payum:
contexts:
your_context_name:
doctrine_storage:
driver: orm
model_class: AcmeDemoBundle\Entity\PaypalPaymentInstruction
doctrine:
orm:
entity_managers:
default:
mappings:
payum_paypal_express_checkout_nvp:
is_bundle: false
type: xml
dir: %kernel.root_dir%/../vendor/payum/paypal-express-checkout-nvp/src/Payum/Paypal/ExpressCheckout/Nvp/Bridge/Doctrine/Resources/mapping
prefix: Payum\Paypal\ExpressCheckout\Nvp\Bridge\Doctrine\Entity
My current doctrine configuration is having an autoload to true. Problem I have is to make this mapping work with my autoload: true
I can get mapping to work when i remove my autoload: true but
Any tips would be much appreciated, but question is how do i create a table from this mapping information?
This config works well for me. It added an id feild(it is come from bundle and was automapped) and the rest field come from manually defined mapping.
I added an example to sandbox. It works fine.
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
mappings:
payum_paypal_express_checkout_nvp:
is_bundle: false
type: xml
dir: %kernel.root_dir%/../vendor/payum/paypal-express-checkout-nvp/src/Payum/Paypal/ExpressCheckout/Nvp/Bridge/Doctrine/Resources/mapping
prefix: Payum\Paypal\ExpressCheckout\Nvp\Bridge\Doctrine\Entity
I am trying to set up my symfony to use two databases connections. The problem is that Symfony won't map the Entities to the right EntityManager. So perform a query, I have to tell Symfony what manager it should use.
My config:
# 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
lookup:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "Lookup"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MbMyAppBundle: ~
mvibes:
mappings:
MbLookupBundle: ~
connection: mvibes
So, to query from my Lookup, i have to do: $this->getDoctrine()->getRepository('MbLookupBundle:Country', 'lookup');
Instead, I was hoping I could leave out the second parameter. This way, my bundle would be independent. The project manager can decide what database config he will implement in his project. He'll just have to make sure that the mapping is correct.
How does this work? What are the mappings used for, if this is not possible?
You have mistakes in connections naming :
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
# ...
mvibes: #This is the name of the connection
# ...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MbMyAppBundle: ~
mvibes:
mappings:
MbLookupBundle: ~
connection: mvibes #must refre to a connection's name defined above
Official doc : http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html
A blog post I wrote about it : http://blog.alterphp.com/2011/10/configuration-trick-for-multiple-entity.html
I had 5 minutes ago a full setup with FOS userBundle which was working. Then i decided to use multiple / shared db connections. And now i've got :
MyNamespace\UserBundle\Entity\User is not a valid entity or mapped super class.
Here's the setting.
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
shared:
driver: %database_shared_driver%
host: %database_shared_host%
port: %database_shared_port%
dbname: %database_shared_name%
user: %database_shared_user%
password: %database_shared_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MyNonSharedBundle: ~
shared:
connection: shared
mappings:
MyUserBundle: ~
FOSUserBundle: ~
PS: If i Move MyUserBundle and FosUserBundle under the default connection, it works.
That means something is trying to work with your entity through default entity manager. Did you configure FOSUserBundle?
fos_user:
model_manager_name: "shared"