Doctrine Mapping in Symfony2 using YAML - symfony

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

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

Base table or view not found - symfony2

I have problem with deploying of symfony2 app. I get the following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table '14271425_sjokh.Books' doesn't exist
I have found out that this error is connected with case issue.
The given table exists on server but is called books not Books.
I have tried to solved this by modyfing config.yml, adding to it naming_strategy: doctrine.orm.naming_strategy.underscore (as proposed here: Symfony2: Base table or view not found: 1146) but it did not helped.
doctrine:
orm:
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
Doctrine underscore naming strategy doesn't pluralize entity names, like it's case in RoR for example.
So, for Book entity, underscore strategy expects table named book. For SomeCamelcase entity, it expects some_camelcase table.
You should either rename your table or tell doctrine what table name is through config. If using annotations for example, it will be:
/**
* #ORM\Table(name="books")
*/
class Book
{
// ....
}
It seems that my problem was connected with wrong cases in doctrine annotations.
I have deleted the following code:
* #ORM\Table(name="Books")
and this together with edit of config file adding:
doctrine:
orm:
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
made the script working.

Change document Namespace from Bundle/Document to Bundle/Model

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.

How to define/pass the entity manager for sonata-admin

I followed this tutorial to install SonataAdmin with FOSUserBundle.
Now i keep getting this Error Message:
No entity manager defined for class Application\Sonata\UserBundle\Entity\User
But how do i set/pass the EntityManager?
I haven't found anything about configuring it or any hint on what this error means.
Any help anyone?
Edit #1:
As asked for, here is what i have in my config.yml for sonata so far:
sonata_block:
default_contexts: [cms]
blocks:
sonata.admin.block.admin_list:
contexts: [admin]
sonata.block.service.text:
sonata.block.service.rss:
Edit #2:
I added the entity manager configuration part for Doctrine2 ORM Admin thou it is mentioned in the Documentation that if left null, it should just use the default. Still, it doesn't solve my problem.
sonata_doctrine_orm_admin:
# default value is null, so doctrine uses the value defined in the configuration
entity_manager: '#doctrine.orm.entity_manager'
Edit #3:
I did set auto_mapping to true as well, even thou that also is true by default.
Still no Solution to this Problem.
When you use
php app/console sonata:admin:generate
it requests for fully qualified model class, so you'd write something like:
\Acme\DemoBundle\Entity\DemoEntity
the problem is, that when generator creates service for you it adds line:
arguments: [~, \Acme\DemoBundle\Entity\DemoEntity, AcmeDemoBundle:DemoEntityAdmin]
That breaks it.
It works with:
arguments: [~, Acme\DemoBundle\Entity\DemoEntity, AcmeDemoBundle:DemoEntityAdmin]
Notice missing "\" before Acme\Demo....
What was missing in my config.yml was:
doctrine:
[...]
orm:
[...]
entity_managers:
default:
mappings:
ApplicationSonataUserBundle: ~
SonataUserBundle: ~
Just had this problem.
Make sure you easy-extended the SonataUserBundle:
php app/console sonata:easy-extends:generate SonataUserBundle
and are loading it in AppKernel.php
new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
See documentation here:
https://github.com/sonata-project/SonataUserBundle/blob/master/Resources/doc/reference/installation.rst
Then the automapping is enough and you don't need to define manual mapping.

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