映射 //Entity 和 //Entity 彼此不一致 - Symfony2/Doctrine2

2024-03-24

最近花了 2 个小时试图解决这个问题。我确信我忽略了一些愚蠢的事情,但它确实让我陷入了困境。

当我尝试验证我的数据库时出现此错误:

 [Mapping]  FAIL - The entity-class 'BC\InventoryBundle\Entity\ProductRecipe' mapping is     invalid:
 * The mappings BC\InventoryBundle\Entity\ProductRecipe#products and BC\InventoryBundle\Entity\Products#recipes are incosistent with each other.
 * The mappings BC\InventoryBundle\Entity\ProductRecipe#recipes and BC\InventoryBundle\Entity\Recipes#products are incosistent with each other.

[Mapping]  FAIL - The entity-class 'BC\InventoryBundle\Entity\Products' mapping is invalid:
* The mappings BC\InventoryBundle\Entity\Products#recipes and BC\InventoryBundle\Entity\ProductRecipe#recipes are incosistent with each other.

[Mapping]  FAIL - The entity-class 'BC\InventoryBundle\Entity\Recipes' mapping is invalid:
* The mappings BC\InventoryBundle\Entity\Recipes#products and BC\InventoryBundle\Entity\ProductRecipe#products are incosistent with each other.

我以为我的逆和映射是错误的。所以(我认为)我已经尝试了所有可能的组合,但没有成功。

这是我的映射文件。

//Recipe.orm.yml
   oneToMany:
    products:
      mappedBy: productsProductRecipe
      cascade: ["all"]

//Products.orm.yml
   oneToMany:
    recipes:
      targetEntity: ProductRecipe
      mappedBy: recipes
      cascade: ["all"]

//ProductRecipe.orm.yml
BC\InventoryBundle\Entity\ProductRecipe:
type: entity
table: ProductRecipe
repositoryClass: BC\InventoryBundle\Entity\ProductRecipeRepository

id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    ammount:
        type: decimal
        presision: 10
        scale: 2

   manyToOne:
    products:
      targetEntity: Products
      inversedBy: recipes
      joinColumn:
        name: product_id
        referencedColumnName: id
    recipes:
      targetEntity: Recipes
      inversedBy: products
      joinColumn:
        name: recipe_id
        referencedColumnName: id

我一直在为我的实体使用 Doctrine:Generate:Entities,因此除非要求,否则我不会将它们粘贴到此处。所有的 setter 和 getter 都在那里。


食谱.orm.yml

   oneToMany:
        products:
            targetEntity: ProductRecipe // Not present before
            mappedBy: recipes // Previously "productsProductRecipe"
            cascade: ["all"]

产品.orm.yml\\ Should rename for singular, also your relation is for Product

    oneToMany:
        recipes:
            targetEntity: ProductRecipe
            mappedBy: products // Previously "recipes"
            cascade: ["all"]

产品配方.orm.yml

BC\InventoryBundle\Entity\ProductRecipe:
    type: entity
    table: ProductRecipe
    repositoryClass: BC\InventoryBundle\Entity\ProductRecipeRepository

    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        amount: // Previously "ammount"
            type: decimal
            presision: 10
            scale: 2

    manyToOne:
        products:
            targetEntity: Product
                // "Products" is named correctly but recipe is singular
                // so for the sake of uniformity 
            inversedBy: recipes
            joinColumn:
                name: product_id
                referencedColumnName: id
        recipes:
            targetEntity: Recipe 
                // Previously "Recipes", incorrect entity name
            inversedBy: products
            joinColumn:
                name: recipe_id
                referencedColumnName: id

粗略地看了一下……但也可能是错误的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

映射 //Entity 和 //Entity 彼此不一致 - Symfony2/Doctrine2 的相关文章

随机推荐