Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2/Doctrine2 Invalid Mapping File Exception when trying to generate entities

I have a very simple database that I am trying to import, and create Entities from. Doctrine (Symfony) is able to generate the YML mapping files from the database. But when I subsiquently try to generate entities, I get the following error:

[Doctrine\Common\Persistence\Mapping\MappingException]
Invalid mapping file 'SandboxBundle.Entity.Product.orm.yml' for class
'SandboxBundle\Entity\Product'.

The yml file looks fine to me, as we would expect being that it was generated by Doctrine. Just to be sure, I checked it against an online yml validator which said it was OK. The command I used to attempt to generate the entities was:

app/console generate:doctrine:entities sandbox

The .yml files follow. Please excuse any yml spacing errors that are a result of pasting the file here. As I said, the yml files were generated by doctrine, and did pass an online verification.

Product:
  type: entity
  table: product
    indexes:
      category_id:
          columns:
              - category_id
  id:
      id:
          type: integer
          nullable: false
          unsigned: false
          comment: ''
          id: true
          generator:
              strategy: IDENTITY
  fields:
      productname:
          type: string
          nullable: true
          length: 10
          fixed: false
          comment: ''
      categoryId:
          type: integer
          nullable: true
          unsigned: false
          comment: ''
          column: category_id
 lifecycleCallbacks: {  }

And for completeness, here is the Category yml file. The error was on Product, but I presume it is because Product was processed first.

Category:
   type: entity
   table: category
       id:
          id:
              type: integer
              nullable: false
              unsigned: false
              comment: ''
              id: true
              generator:
                  strategy: IDENTITY
      fields:
          categoryname:
              type: string
              nullable: true
              length: 50
              fixed: false
              comment: ''
      lifecycleCallbacks: {  }

I searched the web for any resources pertaining to diagnosing Mapping Exceptions, but have not found any. I presume that there is something in the YML files that is causing the entity generator to choke. But the error message give no indication as to what that might be. I see there are lots of instances of this kind of question on Stack Overflow. It would be great to get information on HOW to diagnose these types of errors, and thus be able to figure it out for ourselves.

like image 565
Don Briggs Avatar asked Apr 03 '15 18:04

Don Briggs


2 Answers

Has described in the doc:

class-names specified in the YAML files should be fully qualified.

So try change the product yaml definition as follow:

SandboxBundle\Entity\Product:
  type: entity
  table: product
    indexes:
  .....

Do the same in the other mapping files.

Hope this help

like image 136
Matteo Avatar answered Sep 22 '22 17:09

Matteo


Try to rename the file

SandboxBundle.Entity.Product.orm.yml

to

Product.orm.yml

and make sure to fully qualify your name in the yaml

SandboxBundle\Entity\Product:
type: entity
table: product
indexes:
.....

you might get an error because of double namespace. Symfony adds the dotted part of the file to the namespace.

[Doctrine\Common\Persistence\Mapping\MappingException]
Invalid mapping file 'SandboxBundle.Entity.SandboxBundle.Entity.Product.orm.yml' for class
'SandboxBundle\Entity\SandboxBundle\Entity\Product'.

Good luck :)

like image 25
Stéphan Champagne Avatar answered Sep 24 '22 17:09

Stéphan Champagne