Change document Namespace from Bundle/Document to Bundle/Model - symfony

I'm using DoctrineMongoDBBundle, normally i put the documents into the MyBundle/Document directory and works fine but i want change to MyBundle/Model
after move my documents to new namespace i get a error message
[Exception]
You do not have any mapped Doctrine MongoDB ODM documents for any of your bundles...
currently i'm using annotations for set the configuration, i dont want use xml or yml. this is the current config, how i can achieve this?
# app/config/config.yml
doctrine_mongodb:
connections:
default:
server: mongodb://localhost:27017
options: {}
default_database: "%database_name%"
document_managers:
default:
auto_mapping: true

Assuming your document is Acme/Model/Invoice.
You can adjust the mapping like this:
doctrine:
odm:
# ...
mappings:
Acme:
type: annotation
is_bundle: false
dir: %kernel.root_dir%/../src/Acme/Model
prefix: Acme\Model
alias: MyDocuments
Now use the the mapping like this:
$documentManager->getRepository('MyDocuments:Invoice');
If you want to keep the document inside a bundle ...
... just use the bundle's name i.e. AcmeDemoBundle instead of Acme as the mapping-name, set is_bundle to true and the dir option will be seen as relative to the bundle's root directory.
More information on the mapping options can be found in the documentation chapter DoctrineBundle Configuration#Mapping Information.
More information on how to store documents out of bundles can be found in this blog post.
The procedure is the same for Doctrine ORM btw.

Related

Error on symfony 5.4 : Could not find the entity manager for class 'x'

Today i tried to copy/past an entity from a project to an other.
First i tried to create the entity with
php bin/console make:entity
And to copy/past the entity class into the file just created.
Didn't work and got this error message i can't figure out.
My configuration is a new symfony 5.4 project. And symfony 5.4 for the other project.
The only things that change is that in the first projet i used annotation system (#ORM\Table...) and the new one is attributes by default (#[ORM\...). But i used to use both on other projects and worked well.
I tried to change annotation to attribute and it works. But i would like to know how can i use both annotations and attributes on the project, it used to be the default value ?
My doctrine.yaml :
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '14'
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/Entity'
prefix: 'App\Entity'
alias: App
What i tried :
cache clear
remove/create entity
check on internet for some solutions
check if use Doctrine\ORM\Mapping as ORM; was in my file
What i tried :
cache clear
remove/create entity
check on internet for some solutions
check if use Doctrine\ORM\Mapping as ORM; was in my file
Symfony does not support both, you have to choose between of 2, so if you don't have any php version problem, both should fit

Exclude fields from FOSUserBundle using JMS Serializer working on dev but not on prod environment

I would like to exclude fields when exposing my API from my user class that extends FOSUser.
I've setup JMS Serializer on the global config file and created a FOSUB config to only expose the fields I need.
Global Config:
app/config/config.yml
jms_serializer:
metadata:
directories:
FOSUB:
namespace_prefix: "FOS\\UserBundle"
path: "#AppBundle/Resources/config/serializer/fos"
FOS config file:
src/AppBundle/Resources/config/serializer/fos/Model.user.yml
FOS\UserBundle\Model\User:
exclusion_policy: ALL
properties:
id:
expose: true
email:
expose: true
roles:
expose: true
This config is working perfectly on my local machine however it doesn't work when deployed on prod. Both use same stack, my guess is that on prod somehow the serializer can't find FOS config file.
Any help would be much appreciated.
The issue was somehow related to the naming of the config file.
While in local (macos) the file name Model.user.yml was working, in production (centos) it didn't work. So I had to rename the file to Model.User.yml then it worked fine on both.
I tried to find some documentation related to this issue but couldn't find any.
Take away: Make sure that the config file name represent exactly the entity you want to override.

Entity alias in new symfony 2.7 directory structure

after adapting to new recommended structure in Symfony 2.7 I'm not able to access entity alias through $em->getRepository('Bundle:Entity').
This is my directory structure:
MyBundle
- Component
- Catalog
- Model
Product.php
And my mapping definitition in config.yml:
mappings:
mybundle:
type: annotation
dir: %kernel.root_dir%/../src/mybundle/Component/Product/Model
prefix: MyBundle\Component\Product\Model
alias: ??? # I tried different things
What should I write in $em->getRepository('MyBundle:Product') instead of MyBundle:Product to succesfully accesing the entity?
If possible I would like to use default Symfony alias, so I don't need to specify an alias for every entity in config.yml
Actually I didn't need generate:bundle as it's not a real bundle but a namespace and a logical organization of files.
The solution is to specify the complete namespace in getRespository():
$em->getRepository('MyBundle\\Component\\Product\\Model\\CFGProduct)
Thanks

Doctrine Mapping in Symfony2 using YAML

I have a question regarding YAML configuration of Doctrine in Symfony2.
I have created an entity via "doctrine:generate:entity", and chose YAML as the mapping format.
This didn't add any metadata on ../Entity/"MyEntity".php, which would allow me to update or create my schema.
As an example, if I run
./app/console doctrine:schema:create
it fails, saying:
[RuntimeException]
Bundle "MySuperBundle" does not contain any mapped entities.
My automapping is already set to "true".
If I choose to use annotation config this would not be a problem.
Did I miss something? Are there any extra steps that I should take?
Thank you in advance, regards,
Ivan
I just had a fun time looking at the Doctrine config initialisation code. What I found was:
Using auto_mapping results in various defaults being set for the single default entity manager; it leaves the type value as false
If type is false the config code looks into the default directory for likely config files, and as soon as it finds a file of a valid extension it decides that that's the way config is being done, in the order xml, yml, php
If it doesn't find any of those it assumes annotation
Do you have anything else at all in the Bundle/Resources/config/doctrine folder? If so, it might be throwing off the auto-detection.
That aside, basically if you've used defaults, and have some entity classes and valid config, what you're doing should be working without any additional config. You've said "auto_mapping" is true, but have you changed any other bit of Doctrine config?
It might be an idea to try configuring stuff explicitly, e.g. as described in the Symfony Doctrine docs, go from default config
doctrine:
dbal:
driver: "%database_driver%"
#etc
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
to explicit
doctrine:
dbal:
driver: "%database_driver%"
#etc
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: false
mappings:
MySuperBundleName:
type: yml
dir: Resources/config/doctrine

can be entities stored in other location, than in entity directory?

I am trying to store entities in MyBundle/OtherNamespacePart/Entity directory.
If I run doctrine:schema:create, i get this error message:
Class MyBundle/Entity/MyEntity does not exist and could not be loaded.
I don't get, why is "OtherNamespacePart" ignored (should be MyBundle/OtherNamespacePart/Entity/MyEntity). I checked whole project if there is everything namespaced and named correctly and it seems to me ok. If i store entities directly in entity directory, it works (just need to remove all "OtherNamespacePart").
Isn't problem in console command tool, that expects entities in entity directory? Is there any workaround or configuration option?
You can do this by providing custom mappings in your config.yml
e.g.:
doctrine:
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: false
mappings:
name:
type: xml
dir: %kernel.root_dir%/../src/Your/Bundle/Resources/config/doctrine
alias: MyModels
prefix: MyBundle\OtherNamespacePart\Entity
is_bundle: false

Resources