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.
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.
My Symfony4 app is working fine in dev environment but not in production, i can't login (using FosUserBundle). After enabling debug and profiler toolbar in production (beacause it doesn't write any log) i've found the error
The class 'App\Entity\FintelUtility\User\User' was not found in the
chain configured namespaces FOS\UserBundle\Model
The class was not found in the chain configured namespaces
I've cleared the cache and i've checked mapping info of doctrine
php bin/console doctrine:mapping:info --env=prod
Found 2 mapped entities:
[OK] FOS\UserBundle\Model\Group
[OK] FOS\UserBundle\Model\User
wwwfintel#fnt-srvweb01:~/s4fgel$ php bin/console doctrine:mapping:info --env=dev
Found 5 mapped entities:
[OK] App\Entity\FintelUtility\QuoteOfTheDay
[OK] App\Entity\FintelUtility\User\Group
[OK] App\Entity\FintelUtility\User\User
[OK] FOS\UserBundle\Model\Group
[OK] FOS\UserBundle\Model\User
I don't know how to map those three entities that are missing.
config/packages/doctrine.yaml
# Doctrine Configuration
doctrine:
dbal:
default_connection: mssql_fgel_utility
connections:
mssql_fgel_utility:
driver: ~
driver_class: "%fgelutil_database_driver_class%"
host: "%fgelutil_database_host%"
dbname: "%fgelutil_database_name%"
user: "%fgelutil_database_user%"
password: "%fgelutil_database_password%"
types:
datetime_key: 'App\Type\DateTimeKeyType'
orm:
default_entity_manager: mssql_fgel_utility
entity_managers:
#################################
# Update schema only with this em
#################################
mssql_fgel_utility:
connection: mssql_fgel_utility
mappings:
FintelUtility:
type: "annotation"
# The directory for entity (relative to bundle path)
dir: '%kernel.project_dir%/src/Entity/FintelUtility'
prefix: 'App\Entity\FintelUtility'
is_bundle: false
Ther's no config/packages/dev/doctrine.yaml but prod
config/packages/prod/doctrine.yaml
doctrine:
orm:
metadata_cache_driver:
type: service
id: doctrine.system_cache_provider
query_cache_driver:
type: service
id: doctrine.system_cache_provider
result_cache_driver:
type: service
id: doctrine.result_cache_provider
services:
doctrine.result_cache_provider:
class: Symfony\Component\Cache\DoctrineProvider
public: false
arguments:
- '#doctrine.result_cache_pool'
doctrine.system_cache_provider:
class: Symfony\Component\Cache\DoctrineProvider
public: false
arguments:
- '#doctrine.system_cache_pool'
framework:
cache:
pools:
doctrine.result_cache_pool:
adapter: cache.app
doctrine.system_cache_pool:
adapter: cache.system
I've tried also php bin/console cache:warmup --env=prod --no-debug but it didn't help
Any idea?
EDIT
I've changed "mssql_fgel_utility" entity manager to "default" and it worked for prod but then it doesn't work for dev
orm:
default_entity_manager: mssql_fgel_utility
entity_managers:
#################################
# Update schema only with this em
#################################
# THIS WAS WRONG EVEN IF I DON'T KNOW WHY
# mssql_fgel_utility:
#################################
default: #changed to this
I've changed "mssql_fgel_utility" entity manager to "default" and it worked
orm:
default_entity_manager: default
entity_managers:
#################################
# Update schema only with this em
#################################
# THIS WAS WRONG EVEN IF I DON'T KNOW WHY
# mssql_fgel_utility:
#################################
default: #changed to this
You wrote:
I've cleared the cache and i've checked mapping info of doctrine
Is it the Symfony cache or Doctrine cache? if it's not the Doctrine cache, clear it and try the command php bin/console do:sc:va to check the mapping and check if the database is in sync with the mapping and make the php bin/console do:sc:up --force --env=prod in the case it's not in sync.
You have to change "mssql_fgel_utility" entity manager to "default" and delete "default_entity_manager: mssql_fgel_utility" and it will work for dev and prod.
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'm trying to set a different entity listener resolver because I want to use a couple of services among a few lifecycle callbacks. I googled out some answers that say it should be set in config the way similar to this one:
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
entity_listener_resolver: {HERE!}
However, when I do it this way, I get the following error:
Unrecognized options "entity_listener_resolver" under "doctrine.orm.entity_
managers.default"
Moreover, when I look at the configuration reference, I don't see such option anywhere or an option to set it: http://symfony.com/doc/current/reference/configuration/doctrine.html
So how do I change the entity listener resolver?
I'm using the newest Sf 2.5 version.
I'm running into the same issue, when using "doctrine/doctrine-bundle": "~1.2".
You have two options.
1) Use doctrine/doctrine-bundle 1.3 Beta, where the key entity_listener_resolver exists.
2) Or use the following gist to get it working without using the key: https://gist.github.com/vadim2404/9538227
More infos: http://egeloen.fr/2013/12/01/symfony2-doctrine2-entity-listener-as-serice/
Thx to the guy from the irc channel.
You can use it like this only in the services.yml for a specific bundle:
orm:
entity_listener_resolver: {your_entity_listener_resolver}
But in the config.yml file you need to set the entity listener resolver for a specific entity manager for example the default one:
orm:
entity_managers:
default:
entity_listener_resolver: {your_entity_listener_resolver}
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.