Symfony2 mapping_types - symfony

I'm using Symfony2.0 and add mapping_types in config.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
dbname: work_contactbee
user: devuser
password: devuser
mapping_types:
enum: string
But when I try to update schema: php app/console doctrine:schema:update
I get an exeption *Unrecognized options "mapping_types" under "doctrine.dbal.connections.default*
Anybody can help with that?

For anyone still looking. I solved this for my situation by removing the connections and default_connections part.
So:
doctrine:
dbal:
driver: pdo_mysql
host: localhost
dbname: work_contactbee
user: devuser
password: devuser
mapping_types:
enum: string
Either the docs are wrong, or (more likely) I've misinterpreted them. I'm not sure why your example didn't work tbh.

Which version of Symfony are you using? From 2.1 the mapping_types are defined as an array of mapping_types so if you are using the latest version (2.2), enum: string is no longer valid. See full list of config options for latest version: http://symfony.com/doc/current/reference/configuration/doctrine.html

Related

How can I setup a second db connection with doctrine2 and symfony?

I use doctrine ORM in a Symfony 2.8 project.
My project contains several Bundles. For one Bundle, which generates Reports I want to use the db server with the slave replication as not to stress the master db server.
How to set this up?
What I tried so far:
In the 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
slave:
driver: pdo_mysql
host: '%database_host_slave%'
port: '%database_port_slave%'
dbname: '%database_name_slave%'
user: '%database_user_slave%'
password: '%database_password_slave%'
charset: UTF8
Here I created my second db connection with the values stored in my parameters.yml.
I seem to get the orm configuration I tried to setup in the same file not correctly.
Let my first explain what I need:
I have a "ReportingBundle" which runs a console command. The entity manager is only needed to provide the proper authorization to the needed db server. The queries itself are oure SQL and I don't use the entities.
my service.yml for this bundle:
services:
myproject.reporting.service.csv_report_attachment:
class: Myproject\ReportingBundle\Service\DefaultCsvReportAttachmentService
arguments: ['#doctrine.orm.slave_entity_manager', '#logger', '#myproject.reporting.service.php_template_engine', 'reportingHtmlMailTemplate.php']
Now my non functioning orm setup in the config.yml:
ORM configuration
orm:
auto_generate_proxy_classes: '%kernel.debug%'
# naming_strategy: doctrine.orm.naming_strategy.underscore
default_entity_manager: default
entity_managers:
slave:
connection: slave
mappings:
MyprojectReportingBundle: ~
default:
connection: default
auto_mapping : true
metadata_cache_driver: redis
query_cache_driver: redis
result_cache_driver: redis
This results in
[Doctrine\ORM\ORMException]
Unknown Entity namespace alias 'MyprojectReportingBundle'.
I tried to follow the documentation here:
http://symfony.com/doc/current/reference/configuration/doctrine.html#custom-mapping-entities-in-a-bundle
Question:
What is the correct syntax, so that my query is run on the slave server instead of the default server?
I believe you have everything configured correctly. However, you need to make sure the Entity classes are defined in the Bundle namespace. So for example you'd want to have all the entities defined within Myproject\ReportingBundle\Entity in your example.

symfony2.6 doctrine2 schema_filter parameter not exists

I am using symfony 2.6 (composer.json equal to its github repo) and I am trying to use the schema filter of DBAL.
in config.yml
# Doctrine Configuration
doctrine:
dbal:
schema_filter: ^sf2_
but error returned on shell:
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
Unrecognized option "schema_filter" under "doctrine.dbal"
What am I missing?
EDIT:
config.yml (doctrine part only)
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database.driver%"
host: "%database.master.host%"
port: "%database.master.port%"
dbname: "%database.master.dbname%"
user: "%database.master.user%"
password: "%database.master.password%"
charset: UTF8
options:
1002: "SET NAMES '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%"
slaves:
slave1:
host: "%database.slave1.host%"
port: "%database.slave1.port%"
dbname: "%database.slave1.dbname%"
user: "%database.slave1.user%"
password: "%database.slave1.password%"
mapping_types:
enum: string
set: string
bit: boolean
types:
# some types
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
(POST) CONSIDERATION:
Anyway the schema_filter does not fit my requirements it is too vague to define with a reg exp (I mean for my actual schema is required a too complex reg exp and it is not pratical at all). I posted in doctrine2 groups a request for "enhancing" this option.
https://groups.google.com/forum/#!topic/doctrine-user/Tr4kkpIxwRk
What is your exact symfony version? I tried this config option with Symfony 2.6.5 and everything works fine.
Do you happen to have multiple connections? There is a note in documentation about this, at the end of the page:
Note that if you have multiple connections configured then the schema_filter configuration will need to be placed per-connection.
Manual tables
The schema_filter parameter is part of the Doctrine Migrations bundle. Do you have that installed?

Symfony2 + Doctrine: Create Entities from specific database that is not the default

The Symfony2 Cookbook provides information on how to create entities from existing schemas, but I haven't been able to find a way to import from another database called 'legacy' (see config.yml below) that isn't the default.
Running the following command creates YAML files only from the default database
$ php app/console doctrine:mapping:convert yml ./src/Soapbox/DashboardBundle/Resources/config/doctrine/metadata/orm --from-database --force
If I don't provide any arguments, I receive suggestions, but not sure which ones are applicable.
doctrine:mapping:convert [--filter="..."] [--force] [--from-database] [--extend[="..."]] [--num-spaces[="..."]] [--namespace[="..."]] [--em[="..."]] to-type dest-path
app/config/config.yml
# 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
legacy:
driver: pdo_mysql
host: my.host
port: null
dbname: magazines
user: soapbox
password: XXXXX
charset: UTF8
If I'm thinking clearly which I might not be, you have two EntityManagers, default and legacy.
As such you can use the flag --em "legacy" to get it to import from that specific database.

Doctrine Migrations, problems using custom doctrine types

I'm building a application using Symfony2 + Doctrine2. My application needs to store geospatial data, so I wrote the proper doctrine extensions. All is working pretty good and the app have been running in a production enviroment for a long time.
Now I have to add some new features and I need to update the database without deleting all the data. I thougth about using the DoctrineMigrationBundle but when I run:
$ php app/console doctrine:migrations:status
I got this error:
[Doctrine\DBAL\DBALException]
Unknown database type point requested,
Doctrine\DBAL\Platforms\MySqlPlatform may not
support it.
This is the relevant section of my config.yml:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
types:
point: App\EngineBundle\DoctrineExtensions\PointType
The custom type 'point' has been mapped, so what am I doing wrong?
I answer my own question, it seems the problem is DoctrineMigrations also needs a mapping for the custom type. So the config.yml should look like this:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
types:
point: App\EngineBundle\DoctrineExtensions\PointType
mapping_types:
point: point

Symfony2 entity is trying to use wrong database

I have an app that talks to two different databases. For one of my entities, School, I get the following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pressbox.schools' doesn't exist
That makes sense because there is no pressbox.schools. It's fnt.schools. It's trying to talk to the wrong database.
How do I tell my entity which mapping I want it to use? I would of course rather refer to the mappings than to the database names themselves, which can be different depending on the environment.
First, declare your connections in a config file (config.yml would be fine):
doctrine:
dbal:
default_connection: pressbox # change it as you wish
connections:
pressbox:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: pressbox
user: pressbox_usr
password: pressbox_pwd
charset: UTF8
fnt:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: fnt
user: fnt_usr
password: fnt_pwd
charset: UTF8
Then declare the entity managers:
doctrine:
orm:
default_entity_manager: pressbox
entity_managers:
pressbox:
connection: web
fnt:
connection: fnt
Now, in a controller, you can tell Doctrine which entity manager to use by passing its name to getEntityManager():
$fntEm = $this->getDoctrine()->getEntityManager('fnt');
Assuming the entity manager for the fnt table is called that same name.
i think cross database relations are pretty complicated or you have to code by hand through some plugin.
i found this question about the same topic which might help you:
Multiple database connection in Doctrine2 and Zend framework

Resources