I'm just getting started with Symfony2, currently trying to generate Doctrine entities from an existing database. I'm following the official tutorial on the topic, however, I'd like to have my entities generated in a different namespace other than the default (MyVendor\MyBundle\Model\Entity instead of MyVendor\MyBundle\Entity).
I've edited the config.yml file like this:
doctrine:
orm:
entity_managers:
default:
mappings:
MyBundle:
prefix: MyVendor\MyBundle\Model\Entity
dir: Model/Entity
Unfortunately, I can't get this to work, as entities are still generated in the default namespace and directory. What am I doing wrong?
Change above code as following: Make auto_mapping true...
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
auto_mapping: true
mappings: ~
Try this.. Hope this will work for you.
Related
I'd like to have 2 entity managers, one for a SQLite database for entities defined in a bundle, and the other for the main application. That way, I can load the data that never changes into one database, and load fixtures, tests, etc. into the application database. I'd expect the following to dump the sql for the bundle entities, but it doesn't:
bin/console doctrine:schema:update --dump-sql --em=geonames
[OK] No Metadata Classes to process.
Similarly, I would expect easyadmin to let me define classes to the bundle entities, but it also fails.
# easy_admin.yaml
entities:
Administrative:
class: Bordeux\Bundle\GeoNameBundle\Entity\Administrative
The configured class
"Bordeux\Bundle\GeoNameBundle\Entity\Administrative" for the path
"easy_admin.entities.Administrative" is no mapped entity.
I expect it has something to do with namespaces, or maybe the is_bundle parameter. I've spent a few hours hacking at this, following along with the tutorials and documentation about multiple entity managers, but I can't find anything that shows how to handle entities that come from a third-party bundle.
# doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
geonames:
url: '%env(DATABASE_GEONAMES_URL)%'
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
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
geonames:
connection: geonames
mappings:
BordeuxGeoNameBundle:
is_bundle: true
type: annotation
dir: 'Entity'
prefix: 'Geonames\Entity'
alias: Geonames
I have not configured multiple entity managers for the latest and greatest Symfony versions so I setup a little test case and came up with this for a configuration:
orm:
default_entity_manager: default
auto_generate_proxy_classes: true
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
geonames:
connection: geonames
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
BordeuxGeoNameBundle:
is_bundle: true
type: annotation
dir: 'Entity'
prefix: 'Bordeux\Bundle\GeoNameBundle\Entity'
alias: GeoNames
I tested it using:
bin/console doctrine:mapping:info --em=geonames
And confirmed the entities were being mapped. I did not install EasyAdmin and test it but I don't see any reason why it would not work.
The main difference was using the entity namespace for the prefix attribute.
And just for my own future reference, I committed the test project to github.
Hello I have a strange behavior with Doctrine on Symfony4 (4.0.9), in the console, after the cache was generated by a first run of some command (in a dev environment) the custom types defined in config/packages/doctrine.yml are no longer taken into account.
Why is this happening and how can I fix it ?
Other informations that may be useful: I copied the app code (controllers, entities, etc.) from a Symfony2 application and imported it in a new Symfony 4 app as I'm in the process of making it compatible with Symfony 4. Also, I don't have this behavior on the web version of the app (the custom types are always recognized)
# config/packages/doctrine.yml
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
types:
enumprofilecompletionstep: 'App\DBAL\EnumProfileCompletionStepPossibleValuesType'
enumsection: 'App\DBAL\EnumSectionType'
enumsex: 'App\DBAL\EnumSexType'
enumstatus: 'App\DBAL\EnumStatusType'
phone_number: 'Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
Thanks for your time,
MrPOC
I'm new to Symfony 4
I use Doctrine an I want to use yaml entity mapping.
So i configured the file doctrine.yaml and change type:annotation to type:yml.
And when I tried php bin/console make:entity, there is no yaml mapping file generated linked to this entity
this is my doctrine.yaml file:
parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): ''
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
# 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%/src/Entity'
prefix: 'App\Entity'
alias: App
An example of mapping following your needs:
Change the file name including the "orm" text doctrine.orm.yaml, and take a look at the dir option in the example below (that follows your needs):
App:
is_bundle: false
type: yml
# "dir" in this case must be pointed where are stored your doctrine files (can be anywhere inside the project dir)
dir: "%kernel.project_dir%/config/doctrine"
prefix: App\Entity
Reference: Doctrine yaml mapping (v2.6 current)
I want to be able to use my database just like described in this tutorial:
http://symfony.com/doc/current/book/doctrine.html
Unfortunately, the annotations that were generated through the command
$ php app/console doctrine:generate:entity
are not read as annotations.
php app/console doctrine:schema:update -f
returns
No Metadata Classes to process.
The file looks like this:
<?php
namespace ResBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Page
*
* #ORM\Table()
* #ORM\Entity
*/
class Page
{
... class here with #ORM\ annotations
I think I have found out that this might be because doctrine uses the SimpleAnnotationsReader which only parses annotations like #Column and not #ORM\Column.
The ORM part of my config.yml
orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
default:
auto_mapping: true
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
ResBundle:
type: annotation
is_bundle: false
dir: %kernel.root_dir%/../src/ResBundle/Entity/
prefix: Resbundle\Entity
alias: ResBundle
The problem is that I have no clue how to switch to the normal AnnotationsReader, answers to other similar questions could not enlighten me.
Is there a setting in my config.yml that need to change? What can I do?
First, check that your bundle is activated.
Second, if you're using bundles, there should be no reason for all that complicated stuff in your config. Why don't you just use the default Doctrine ORM configuration?
doctrine:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
That should do the job, given your bundle is properly registered and activated.
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?