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
Related
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
I am trying to upgrade a Symfony 3 application to Symfony 4, but I am having trouble with Doctrine's namespace aliases. My entities are located under AppBundle\Entity (as was the default in older Symfony versions) and there was an alias for that, so my entities are referred to via AppBundle:SomeEntity all over my codebase. I understand that the new way to do this is to use the FQCN instead, but I would like to avoid having to do a massive find&replace operation right now.
I modified config/packages/doctrine.yaml with the following mapping:
mappings:
AppBundle:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/AppBundle/Entity'
prefix: 'AppBundle\Entity'
alias: AppBundle
However, I only get a ReflectionException saying Class AppBundle:SomeEntity does not exist. As far as I can tell from the stack trace, the alias is never resolved, just passed directly down to AbstractManagerRegistry as a class name, which then just passes it to the ReflectionClass constructor. The calling code, e.g., looks like this (inside a controller): $this->getDoctrine()->getRepository("AppBundle:SomeEntity")->...
I have now idea what the problem is. It looks like there is just no logic that would resolve aliases in this code path. It is probably a configuration issue. However, the configuration is mostly the default that comes with Symfony 4. I basically just replaced "App" with "AppBundle". Can anyone help me out here?
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.
I am building a company Symfony2 library that includes several bundles and a library of classes that can be used by any code. The plan is to eventually turn this into an internal package and install it into projects via composer so that it will live in the vendor directory.
One component of the library has a pair of database entity classes and a single repository class associated with it.
Because the only logic associated with these classes is contained in the repository class, it doesn't seem to make sense to to surround it with a bundle.
I have, as yet, been unable to get Doctrine to load this repository. It initially failed with a
The class 'Dplh\Library\DplhEnum\Entity\EnumGroupRepository' was not found in the chain configured namespaces Dplh\DplhSecurityBundle\Entity
exception. (The reference to my security bundle was initially rather confusing. Turns out that this is Doctrine helping out by listing all of the known entity namespaces (see Symfony error The class XXX was not found in the chain configured namespaces XXX)).
As I understand it, this happens because Doctrine is configured to use auto-mapping and expects all entities to be defined in src/WhateverBundle/Entity directories.
This can be changed with additional Doctrine configuration for specific mappings in config.yml (http://zalas.eu/how-to-store-doctrine-entities-outside-of-a-symfony-bundle/).
So far, I have been unable to get this to work either. I expect it's a minor config thing somewhere. I added the following to config.yml:
doctrine:
dbal:
# ...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
mappings:
DplhEnum:
type: annotation
is_bundle: false
dir: %kernel.root_dir%/../src/Dplh/Library/DplhEnum/Entity
prefix: Dplh\Library\DplhEnum\Entity
alias: DplhEnum
I get two different exceptions depending on my call to getRepository().
// Using the fully-qualified class name.
$this->getEntityManager()->getRepository('Dplh\Library\DplhEnum\Entity\EnumGroup' )
This throws
Attempted to load class "EnumGroupRepository" from namespace "Dplh\DplhEnumBundle\Entity".
Did you forget a "use" statement for another namespace?
// Using the alias.
$this->getEntityManager()->getRepository('DplhEnum\EnumGroup' )
this throws
Class 'DplhEnum\EnumGroup' does not exist
I have verified that the EnumGroupRepository.php file is in the src/Dplh/Library/DplhEnum/Entity directory and the EnumGroupRepository class is in the Dplh\Library\DplhEnum\Entity namespace and that they are in the correct directory.
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.