I try to use Doctrine in Symfony 2.5 with an XML configuration for the entity mapping.
I have a namespaced class Bar: ACME\TestBundle\Entity\Foo\Bar
As I have many entities they can't all reside in the ACME\TestBundle\Entity namespace, but must be put into sub-namespaces.
Creating the entities is no problem, but I can't figure out where to put the ORM XML configuration files.
I tried Resources/config/doctrine/Foo/Bar.orm.xml, which doesn't find the mapping file:
$ php app/console doctrine:schema:create --dump-sql
No Metadata Classes to process.
I tried Resources/config/doctrine/Bar.orm.xml, which ignores the additional Foo namespace unter Entity, although the full namespace is correctly given in Bar.orm.xml in the name element.
$ php app/console doctrine:schema:create --dump-sql
[Doctrine\Common\Persistence\Mapping\MappingException]
Class 'ACME\TestBundle\Entity\Bar' does not exist
What am I missing? What is the correct place for the XML mapping file to reside for these namespaced classes?
Using #user3749178 suggestion of Foo.Bar.orm.xml works and is the easiest way to solve the problem though all the mapping files end up in one directory.
It's also possible to have individual directories for everything based on:
http://symfony.com/doc/current/reference/configuration/doctrine.html#mapping-configuration
Here is an example configuration:
doctrine:
orm:
default_entity_manager: default
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
connection: default
mappings:
foo1:
prefix: Cerad\Bundle\ProjectGameBundle\Doctrine\Entity\Foo1
type: yml
dir: src/ProjectGameBundle/Doctrine/EntityMapping/Foo1
is_bundle: false
foo2:
prefix: Cerad\Bundle\ProjectGameBundle\Doctrine\Entity\Foo2
type: yml
dir: src/ProjectGameBundle/Doctrine/EntityMapping/Foo2
is_bundle: false
You basically specify one mapping for each directory containing entities.
Related
I have created multiple modules folder inside my symfony project. Therefore each Entity and it's Repository class are kept in their own module folder instead keeping them in default src/Entity folder like this.
src
- Post
- Persistence
- Entity
- Post.php
- Repository
- PostRepository.php
- PostComment
- Persistence
- Entity
- PostComment.php
- Repository
- PostCommentRepository.php
Now to make this work in conig/doctrine.yaml, I had to create multiple entity_managers like this
orm:
entity_managers:
vouchers:
mappings:
Post:
is_bundle: false
type: attribute
dir: '%kernel.project_dir%/src/Modules/Post/Persistence/Entity'
prefix: 'App\Modules\Post\Persistence\Entity'
alias: Post
PostComment:
is_bundle: false
type: attribute
dir: '%kernel.project_dir%/src/Modules/PostComment/Persistence/Entity'
prefix: 'App\Modules\PostComment\Persistence\Entity'
alias: PostComment
This works however as you noticed, this makes me create new entity manager line in the doctrine.yaml configuration file every time I add new entity. There are going to more than 100 tables and I don't feel this is a right way to do it even though it works.
Question: Is there a way to make this dynamic so that I don't need to repeat myself?
More or less, I was hoping it would support regex like this but of-course this does not work.
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
dir: '%kernel.project_dir%/src/Modules/*/Persistence/Entity'
prefix: 'App\Modules\*\Persistence\Entity'
alias: App
New to Symfony here
I am following this tutorial about mapping db tables to entities. I created the blog_post and blog_comment tables in my db.
I tried running this php bin/console doctrine:mapping:import --force AppBundle xml which spit out the following error.
Bundle "AppBundle" does not exist or it is not enabled. Maybe you forgot to
add it in the registerBundles() method of your App\Kernel.php file?
My doctrine.yaml is pretty much out of the box.
Doctrine.yaml
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: true
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
I created the App.php Class under src/Entity/ and added a line under bundles.php but still get an error. The line I added on bundles.php is
App\Entity\App::class=>['all' => true], since that is the defined namespace of my App.php class.
Another error I get is
PHP Fatal error: Uncaught
Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load
class "App" from namespace "App\Entity\App".
Did you forget a "use" statement for another namespace? in
/var/www/html/quick_tour/src/Kernel.php:32
you must use App rather than AppBundle on Symfony 4
Found similar situation to mine in here as well.
Since Symfony 4 Use App instead of Appbundle
I'm trying to update a Symfony 3.4 application to Symfony 4. On my current applications I always share a local AdminBundle folder inside src. I know Symfony 4 recommends to be "bundle-less" now. But this AdminBundle is the base for most of my projects, and sometimes I make some updates to it that can be deployed to all my projects just pushing to the repository.
I tried to move by AdminBundle inside src but obviously that's not working. Could anyone detail the recipe or configuration needed to make this Bundle work under Symfony 4 in a generic way?
If this is not possible what's the best way to create a reusable code in symfony 4?
I'm currently using a similiar approach:
I've got a "CoreBundle" for shared Services, Entites, Subscribers etc under "src".
In order to make this usable i had to edit the following:
config/maker.yaml -> to use make:entity and create under CoreBundle
maker:
root_namespace: 'App\CoreBundle'
config/packages/doctrine.yaml modify -> to get the Entities from CoreBundle
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/CoreBundle/Entity'
prefix: 'App\CoreBundle\Entity'
alias: App
config/services.yaml add -> to import all SymfonyLike Classes
###> CoreBundle ###
App\CoreBundle\:
resource: '../src/CoreBundle/*'
exclude: '../src/CoreBundle/{DependencyInjection,Entity,Model,Migrations,Tests}'
App\CoreBundle\Controller\:
resource: '../src/CoreBundle/Controller'
tags: ['controller.service_arguments']
###< CoreBundle ###
config/* add -> where i wanted to import a separate config file
imports:
- { resource: '../../src/CoreBundle/Resources/config/*.yaml' }
I have custom mapping settings in my Symfony3 project for Doctrine entities like this:
MyModel:
type: yml
dir: %kernel.root_dir%/../src/AppBundle/Resources/config/doctrine/MyModel
prefix: MyProject\MyModel\Model
is_bundle: false
Let's assume I have an entity MyProject\MyModel\Model\SubNamespace\MyEntity. Now, I have to put its yml mapping in %kernel.root_dir%/../src/AppBundle/Resources/config/doctrine/MyModel/SubNamespace.MyEntity.orm.yml, and it works fine.
Can I configure Doctrine to be able to organize mapping files in subfolders instead of file names being a concatenation of parts of namespace after prefix?
In this case if would be %kernel.root_dir%/../src/AppBundle/Resources/config/doctrine/MyModel/SubNamespace/MyEntity.orm.yml
The reason is that mapping directory is growing and it's getting hard to find any particular file inside.
Of course, it's not a solution to make a configuration for every subfolder. ;-)
Looking at the source code, there's nothing to handle subfolders. But then the trick could be to add each folder in a different "mapping", as far I know those don't have to stick to your Symfony bundles:
doctrine:
# ...
orm:
# ...
mappings:
AcmeBundleFoo:
type: yml
dir: AcmeBundle/Resources/doctrine/Foo
AcmeBundleBar:
type: yml
is_prefix: false # you are free to let Symfony guess it or to be explicit
dir: AcmeBundle/Resources/doctrine/Bar
A little verbose, but it should work given yout folder structure is src/AcmeBundle/Resources indeed.
yes it's in conf files :
doctrine:
dbal:
driver: "%database_driver%"
#etc
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: false //default was true
mappings:
MySuperBundleName:
type: yml
dir: Resources/somewhereElse/doctrine // your specific directory
I use doctrine in my symfony 2.3 application. I want to use a folder structure like
/MyBundleName/User/User.php
for my Entities.
Question:
Is there anyway that I can explicitly map doctrine ORM directly to use an explicit directory instead of defaulting to the Entity Directory of my Bundle?
I would like to keep all related files in their respective directory such as ProductProvider in
/MyBundleName/Product/ProductProvider.php
Any help would be greatly appreciated.
Just to follow up a bit on #Imanol's correct answer, it is possible to have your entities in multiple directories under one entity manager:
doctrine:
orm:
default_entity_manager: default
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
connection: default
mappings:
test01:
connection: test01
mappings:
product:
type: yml
dir: %kernel.root_dir%/../src/Cerad/Bundle/Test01Bundle/Product
prefix: Cerad\Bundle\Test01Bundle\Product
alias: Product
is_bundle: false
user:
type: yml
dir: %kernel.root_dir%/../src/Cerad/Bundle/Test01Bundle/User
prefix: Cerad\Bundle\Test01Bundle\User
alias: User
is_bundle: false
Don't worry about the is_bundle: false entries. The entities can still live in a bundle. Doctrine does not care. And in case you are wondering, the alias parameter lets you do things like:
$repo = $em->getRepository("Product:Product");
you can tell Doctrine the directory where is your entities
doctrine:
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: false
mappings:
name:
type: php
dir: %kernel.root_dir%/../src/Company/CartoDBBundle/Tests/CartoDB/Entity
Here you have the full documentation Doctrine configuration
I made a similar question a few days ago, there you can read the full answer Cedar gave me
Similar post
I've spent some time trying to figure out the simplest case. This is how I made it work:
doctrine:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
mappings:
AppBundle:
mapping: true
type: annotation
dir: Model
alias: AppBundle
prefix: 'AppBundle\Model'
is_bundle: true
I simply wanted to store my entities in a directory called 'Model' inside my bundle, instead of the default 'Entity'.