I have a problem. I was able to configure symfony to connect to two databases.
config.yml:
# Doctrine Configuration
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
customer:
driver: pdo_mysql
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:
AppBundle: ~
customer:
connection: customer
mappings:
AppBundle: ~
And here my question appears.
How create new entity in second database?
you can do this into a controller:
$customerEntityManager = $this->getDoctrine()->getManager('customer');
or this:
$customerEm = $this->get('doctrine.orm.customer_entity_manager');
and then:
$yourEntity = new YourEntity();
$customerEm->persist(yourEntity);
$customerEm->flush();
Related
When is declared schema that not equal to conection database name it ignored. For example i have 2 Entity:
Users:
namespace App\Entity\DbPublic;
/**
* Users
*
* #ORM\Table(name="users", schema="public")
* #ORM\Entity(repositoryClass="App\Repository\DwPublic\UsersRepository")
*/
class Users
{
UsersData:
namespace App\Entity\DbPrivate;
/**
* UsersData
*
* #ORM\Table(name="users_data", schema="private")
* #ORM\Entity(repositoryClass="App\Repository\DbPublic\UsersDataRepository")
*/
class Users
{
When i try to migrate its don't trow error but its also generate script for schema="public" and not for schema="private", how to generate scrip and relations betwen 2 tables
My doctrine.yaml was:
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
and transformed into:
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
driver: pdo_mysql
host: '%env(DATABASE_HOST)%'
port: '%env(DATABASE_PORT)%'
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PASS)%'
dbname: '%env(DATABASE_NAME_MASTER)%'
charset: utf8mb4
db_public:
# configure these for your database server
driver: pdo_mysql
host: '%env(DATABASE_HOST)%'
port: '%env(DATABASE_PORT)%'
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PASS)%'
dbname: '%env(DATABASE_NAME_MASTER)%'
charset: utf8mb4
db_private:
# configure these for your database server
driver: pdo_mysql
host: '%env(DATABASE_HOST)%'
port: '%env(DATABASE_PORT)%'
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PASS)%'
dbname: '%env(DATABASE_NAME_PRIVATE)%'
charset: utf8mb4
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
db_public:
connection: db_public
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
db_private:
connection: db_private
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
I have setup a Custom Authenticator using guard and auto wired the service. This is tested and works fine with just MySQL configured.
I have now specified a second database connection (oracle), but Symfony will now not allow autowiring in my service configuration, because it does not know which database connection to use when injecting EntityManager in to the custom Authenticator class.
Any idea how I can Configure the Dependency Injection to use a specific database connection so I can continue to use AutoWire.
Unable to autowire argument of type "Doctrine\ORM\EntityManager" for the service "user.security.login_form_authenticator". Multiple services exist for this class (doctrine.orm.prism_entity_manager, doctrine.orm.baan_entity_manager).
Here is my Doctrine config in config.yml
doctrine:
dbal:
connections:
prism:
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%/../var/data/data.sqlite"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
#path: "%database_path%"
baan:
driver: oci8
host: "%baan_host%"
port: "%baan_port%"
dbname: "%baan_db_name%"
user: "%baan_user%"
password: "%baan_password%"
charset: AL32UTF8
orm:
default_entity_manager: prism
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
auto_mapping: true
prism:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: prism
mappings:
UserBundle:
type: annotation
baan:
connection: baan
mappings:
BaanBundle:
type: annotation
Here is the constructor in my Authenticator class
private $formFactory;
private $em;
private $router;
public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router)
{
$this->formFactory = $formFactory;
$this->em = $em;
$this->router = $router;
}
You can extend Doctrine's EntityManagerDecorator, which implements EntityManagerInterface and accepts an instance of EntityManager in its constructor.
First extend the EntityManagerDecorator class once for each of your connections.
namespace MyBundle\Service\Database;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
class PrismEntityManager extends EntityManagerDecorator {}
class BaanEntityManager extends EntityManagerDecorator {}
Then in your service configuration, you need to manually wire these two services.
MyBundle\Service\Database\PrismEntityManager:
arguments:
$wrapped: '#doctrine.orm.prism_entity_manager'
MyBundle\Service\Database\BaanEntityManager:
arguments:
$wrapped: '#doctrine.orm.baan_entity_manager'
Now you just have to type-hint for one of these services.
public function __construct(FormFactoryInterface $formFactory, PrismEntityManager $em, RouterInterface $router)
{
$this->formFactory = $formFactory;
$this->em = $em;
$this->router = $router;
}
I don't know if i understood your question correctly or not but you can set different configurations for different database connection as below:
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
mapping_types:
enum: smallint
custom:
driver: pdo_mysql
host: '%database_host2%'
port: '%database_port2%'
dbname: '%database_name2%'
user: '%database_user2%'
password: '%database_password2%'
charset: UTF8
mapping_types:
enum: smallint
orm:
default_entity_manager: default
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
auto_mapping: true
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: default
mappings:
EntityBundle:
type: annotation
alias: DBAlias
custom:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: custom
mappings:
EntityBundle:
type: annotation
alias: DBAlias
Now you can passed your custom EntityManager using doctrine.orm.custom_entity_manager.
I think, I had the same issue with DBAL Connections as you have with EntityManager. I have solved this issue with some proxy class - described in this answer:
https://stackoverflow.com/a/46265170/6357312
Any question let me know.
I'm trying to use softdelete option of gedmo/doctrine-extensions but for some reason when I call romove(), the record in database gets removed instead of updating deletedAt field.
In here, doc tells us to update config with:
$config->addFilter('soft-deleteable',
'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter');
This is just one of the examples I tried:
# app/config/config.yml
doctrine:
orm:
entity_managers:
default:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
References (just a few of them):
DoctrineExtensions SoftDeleteable
http://knplabs.com/en/blog/gedmo-doctrine-extensions-on-symfony2
Can't enable SoftDeleteable in Symfony2 - Unrecognized options "filters"
So the question in simple terms, how do I configure it in config.yml?
CONTROLLER
public function delete($id)
{
$profile = $this->profileRepository->findOneBy(['id' => $id]);
if (!$profile instanceof Profile) {
throw new ........
}
$this->entityManager->remove($profile);
$this->entityManager->flush();
return true;
}
ENTITY
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Entity()
* #ORM\Table(name="profile")
* #Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
class Profile
{
/**
* #ORM\Column(name="deletedAt", type="datetime", nullable=true)
*/
private $deletedAt;
......
}
COMPOSER.JSON
"require": {
"symfony/symfony": "2.6.*",
"doctrine/orm": "~2.2,>=2.2.3",
"doctrine/doctrine-bundle": "~1.2",
"gedmo/doctrine-extensions": "2.3.*#dev",
......
},
CONFIG.YML
doctrine:
dbal:
default_connection: front
connections:
front:
driver: %database_driver%
host: %database_host%
........
back:
driver: %database_driver%
host: %database_host%
........
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: front
entity_managers:
front:
connection: front
mappings:
MyWebsiteBundle:
dir: Entity
FOSUserBundle: ~
back:
connection: back
MAPPING INFO:
inanzzz#inanzzz:/var/www/html/local$ php app/console doctrine:mapping:info
Found 8 mapped entities:
[OK] My\Bundle\Entity\AbstractMerchantProfile
[OK] My\Bundle\Entity\AbstractIntegration
[OK] My\Bundle\Entity\APIConsumer
[OK] My\Bundle\WebsiteBundle\Entity\User
[OK] My\Bundle\WebsiteBundle\Entity\Profile
[OK] My\Bundle\WebsiteBundle\Entity\Integration
[OK] FOS\UserBundle\Model\Group
[OK] FOS\UserBundle\Model\User
This is how I configured it
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
Solution:
Included stof/doctrine-extensions-bundle in composer.json
"stof/doctrine-extensions-bundle": "1.2.*#dev",
Package is here. Documentation is here.
Enable bundle in AppKernel:
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle()
Since I have more than one entity managers in config.yml I did:
stof_doctrine_extensions:
orm:
em1:
softdeleteable: true
doctrine:
dbal:
default_connection: em1
connections:
em1:
driver: %database_driver%
host: %database_host%
.......
em2:
driver: %database_driver%
host: %database_host%
.......
em3:
driver: %mws_database_driver%
host: %mws_database_host%
.......
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: em1
entity_managers:
em1:
connection: em1
mappings:
MyWebsiteBundle:
dir: Entity
FOSUserBundle: ~
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
em2:
connection: em2
em3:
connection: em3
Somehow I need to use aux connection / entity manager...
security.yml:
security:
providers:
administrators:
entity: { class: Hoax\PartnerBundle\Entity\Partner, property: username }
config.yml:
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
mapping_types:
enum: string
aux:
driver: %database2_driver%
host: %database2_host%
port: %database2_port%
dbname: %database2_name%
user: %database2_user%
password: %database2_password%
charset: UTF8
mapping_types:
enum: string
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
HoaxNotificationsBundle: ~
HoaxPartnerBundle: ~
vpnserver:
connection: aux
mappings:
HoaxPartnerBundle: ~
vpnpayment:
connection: payment
mappings:
HoaxPartnerBundle: ~
Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php:
class EntityUserProvider implements UserProviderInterface
{
private $class;
private $repository;
private $property;
private $metadata;
public function __construct(ManagerRegistry $registry, $class, $property = null, $managerName = null)
I tried setting it like this: { class: Hoax\PartnerBundle\Entity\Partner, property: username, managerName: aux }
But having error: InvalidConfigurationException: Unrecognized options "managerName" under "security.providers.administrators.entity"
Read a little bit further: symfony.com/doc/current/cookbook/doctrine/…. If you still have trouble then update your question with the orm section of doctrine.
You also need to add the entity manager name to the provider section of the security file: symfony.com/doc/current/reference/configuration/security.html
Make sure you use manager_name and not managerName
In my config i declare 2 connections:
doctrine:
dbal:
default_connection: pay
connections:
pay:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
mein:
driver: %database_driver%
host: somehost
port: 3306
dbname: dbname
user: dbuser
password: dbpassword
charset: UTF8
getEntityManager() without any paramenerts works fine, but if i will use 'pay' or 'mein', doctrine show error "EntityManager with name %name% does not exists."
Modify Registry.php
public function getEntityManager($name = null)
{
var_dump($this->entityManagers);die;
return
array(1) { ["default"]=> string(35) "doctrine.orm.default_entity_manager" }
Why it doesn't see actual config?
Your definition is only the DBAL connection definition. You need to define two Entity managers too. Please take a look