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 ?
Related
My multi-tenant-app uses a master database, that holds information about tenants (like name, etc.) and a app-specific database per tenant.
I configured a master and some_tenant connection and entity manager in the doctrine section inside config.yml.
This gives me access to the master database from a controller (eg. for validating and getting tenant information for some_tenant based on the subdomain some_tenant.my-app.com). And it lets me use a tenant-specific database and entity manager during the application life-cycle.
The doctrine section in my config looks like this:
doctrine:
dbal:
default_connection: 'master'
connections:
master:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
some_tenant:
driver: pdo_mysql
host: "%database_host_some_tenant%"
port: "%database_port_some_tenant%"
dbname: "%database_name_some_tenant%"
user: "%database_user_some_tenant%"
password: "%database_password_some_tenant%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
master:
connection: master
mappings:
BEMultiTenancyBundle: ~
some_tenant:
connection: some_tenant
mappings:
AppBundle: ~
Here comes the part, which I am unhappy with and cannot find a solution:
First of all, tenants will be more than 20. And it starts to get messy, altering the doctrine config this way.
Then there is another config file, called tenants.yml which holds more information like enabled/disabled app-features, tenant-specific themes, etc.
The file is loaded, validated using the Config Component, and a container parameter is set, so that tenants configurations are available app-wide.
I would like to store the database credentials also in that file.
I want to create connections and entity managers based on that config. One per each tenant, which can be used during the app life-cycle.
I need to have them available also at the console command bin/console doctrine:schema:update --em=some_tenant for updating each tenant's database schem and bin/console doctrine:schema:update --em=master for updating the master database scheme.
By now I guess, the only way to achieve this is to add configuration parameters to the doctrine section programmatically after the AppBundle is loaded, and before the doctrine registry is constructed with the given managers and connections.
But I cannot even find a point, where I could achive this.
Is there another way to get to point 1 and 2?
Even though there is a similiar question, which I am not sure if it is exactly about the same problem and is about 3 years old, I wanted to post this question with a bit more explanation.
The solution in comments is a straight way easy solution and it will work. The other solution I've ever meet is to dynamically replace tenant database credentails using some external conditions, i.e request HOST.
You can decorate or extend the connection factory with a service in a way, that having the request stack available (or providing the domain with ENV or console argument for other SAPI's) you can have the same entity manager (even default!) being configured on demand.
on a brief look this would look like
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Symfony\Component\HttpFoundation\RequestStack;
class DynamicConnectionFactory extends Factory
{
/** #var RequestStack */
private $requestStack;
public function __construct(array $types, RequestStack $stack)
{
parent::__construct($types);
$this->requestStack = $stack;
}
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = array())
{
$host = $this->requestStack->getMasterRequest()->getHost();
$params = $this->replaceParamsForHost(array $params, $host);
return parent::createConnection($params, $config, $eventManager, $mappingTypes);
}
private function replaceParamsForHost(array $params, $host)
{
//do your magic, i.e parse config or call Memcache service or count the stars
return array_replace($params, ['database' => $host]);
}
}
I think that, To manage multi-tenant with symfony 2/3.
We can config auto_mapping: false for ORM of doctrine.
file: config.yml
doctrine:
dbal:
default_connection: master
connections:
master:
driver: pdo_mysql
host: '%master_database_host%'
port: '%master_database_port%'
dbname: '%master_database_name%'
user: '%master_database_user%'
password: '%master_database_password%'
charset: UTF8
tenant:
driver: pdo_mysql
host: '%tenant_database_host%'
port: '%tenant_database_port%'
dbname: '%tenant_database_name%'
user: '%tenant_database_user%'
password: '%tenant_database_password%'
charset: UTF8
orm:
default_entity_manager: master
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
master:
connection: master
auto_mapping: false
mappings:
AppBundle:
type: yml
dir: Resources/master/config/doctrine
tenant:
connection: tenant
auto_mapping: false
mappings:
AppBundle:
type: yml
dir: Resources/tenant/config/doctrine
After that, we cannot handle connection of each tenant by override connection info in request_listener like article: http://mohdhallal.github.io/blog/2014/09/12/handling-multiple-entity-managers-in-doctrine-the-smart-way/
I hope that, this practice can help someone working with multi-tenant
Regards,
Vuong Nguyen
I have a problems with Doctrine on Sf2.7, when I run a command such as "doctrine: schema: update" I get:
[Doctrine\Common\Persistence\Mapping\MappingException] File mapping drivers must have a valid directory path, however the given path 0 seems to be incorrect!
This worked well until I make a drop of a database to recreate it in "clean" after a lot changed entities ...
Here is an excerpt from my config.yml
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
dbname: "%database_name%"
host: "%database_host%"
port: "%database_port%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
auto_mapping: true
all entities begin with this type of annotations:
/**
* User
*
* #ORM\Entity
* #ORM\Table(name="usr_user")
* #ORM\Entity(repositoryClass="Acme\UserBundle\Repository\UserRepository")
*/
I searched our friend Google, OpenClassRooms and here of course but not found ... someone an idea ?
Got the same error when I imported my project to another pc. The solution worked for me is to try clearing configuration cache using php artisan command.
php artisan config:cache
then try to create or update doctrine schema.
For Symfony, you need to clear var/cache directory.
It solved problem for me.
NOTE: Its not a Duplicate issue cause I have tried everything I get on google but nothing have helped us.
I am trying to import Tables using Doctrine Reverse Engineering tool, but I m getting this message:
Database does not have any mapping information.
My Connection Details in Config.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
port: null
dbname: pixel_ashish
user: root
password: abc123
charset: UTF8
schema_filter: ~^(?!some_table1|some_table2)~
orm:
default_entity_manager: default
auto_generate_proxy_classes: true
proxy_dir: "%kernel.cache_dir%/doctrine/orm/Proxies"
proxy_namespace: Proxies
resolve_target_entities: []
What I have tried So Far:
Running php app/console doctrine:mapping:import --force AcmeBlogBundle xml
gives same error
Tried to convert mapping also which does not make any sense cause mappings are not there but still tried didn't worked out.
Created a new project and tried above given configuration didn't worked.
Now I am out of ideas please help me to solve this.
You need to create some mapping information in your config.
You can do it by passing auto_mapping: true under orm section:
doctrine:
orm:
auto_mapping: true
or by manually defining it under orm section:
doctrine:
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
YourBundleName:
type: "xml"
dir: "Entity"
prefix: "Your\BundleName\Entity"
I have setup a new project with multiple entity managers, when I try to load the data fixtures I get an MappingException as the console tries to load Fixtures for everything rather than the entity manager I specified.
Here is the doctrine section from my config.yml
doctrine:
dbal:
connections:
default:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: %database_charset%
symblog:
driver: %database_driver_blog%
host: %database_host_blog%
port: %database_port_blog%
dbname: %database_name_blog%
user: %database_user_blog%
password: %database_password_blog%
charset: %database_charset_blog%
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
connection: default
mappings:
IncompassAuthBundle: ~
IncompassUserBundle: ~
IncompassApiBundle: ~
IncompassSurgeryBundle: ~
IncompassVendorBundle: ~
IncompassHospitalBundle: ~
dql:
datetime_functions:
date: Mapado\MysqlDoctrineFunctions\DQL\MysqlDate
symblog:
connection: symblog
mappings:
IncompassBlogBundle: ~
dql:
datetime_functions:
date: Mapado\MysqlDoctrineFunctions\DQL\MysqlDate
As you can see I've setup a separate connection and entity manager for the symblog tutorial stuff.
When I try
php app/console doctrine:fixtures:load --em=default
I get this
Careful, database will be purged. Do you want to continue Y/N ?Y
> purging database
> loading [1] Incompass\BlogBundle\DataFixtures\ORM\BlogFixtures
[Doctrine\Common\Persistence\Mapping\MappingException]
The class 'Incompass\BlogBundle\Entity\Blog' was not found in the chain configured namespaces Incompass\AuthBundle\Entity, Incompass\UserBundle\Entity, Incompass\
SurgeryBundle\Entity, Incompass\VendorBundle\Entity, Incompass\HospitalBundle\Entity, FOS\UserBundle\Model
doctrine:fixtures:load [--fixtures[="..."]] [--append] [--em="..."] [--purge-with-truncate]
When I try
php app/console doctrine:fixtures:load --em=symblog
I get
Careful, database will be purged. Do you want to continue Y/N ?Y
> purging database
> loading [1] Incompass\BlogBundle\DataFixtures\ORM\BlogFixtures
> loading [1] Incompass\SurgeryBundle\DataFixtures\ORM\SurgeryStatusFixtures
[Doctrine\Common\Persistence\Mapping\MappingException]
The class 'Incompass\SurgeryBundle\Entity\SurgeryStatus' was not found in the chain configured namespaces Incompass\BlogBundle\Entity
doctrine:fixtures:load [--fixtures[="..."]] [--append] [--em="..."] [--purge-with-truncate]
So the console command appears to be ignoring the "--em=foobar" option and is trying to load up all the data fixtures it finds.
How can I get doctrine:fixtures:load to only use the specified entity manager?
After Phils comment I moved all my Fixtures into a FixturesBundle and did this
php app/console doctrine:fixtures:load --fixtures=src/Incompass/FixturesBundle/DataFixtures/ORM
For the Blog Fixtures I also had to specify the entity manager
php app/console doctrine:fixtures:load --fixtures=src/Incompass/BlogBundle/DataFixtures/ORM --em=symblog
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.