开发者

Unknown related component on class with Doctrine inheritance, Symfony 1.4

开发者 https://www.devze.com 2023-03-16 23:22 出处:网络
I have a symfony 1.4 project to show products in a category product view. Products can be viewed in different categories so I have a many-to-many relationship between product and category.

I have a symfony 1.4 project to show products in a category product view. Products can be viewed in different categories so I have a many-to-many relationship between product and category.

I also have different types of products so have used Doctrine inheritance to define them as most of them share a lot of the same properties.

Now the problem I am having is that when trying to add a product to a category I am getting the error:

Unknown record property / related component "CategoriesFeaturedIn" on "MultiProduct"

CategoriesFeaturedIn is the property on the Product that holds which categories it is in as defined in the Doctrine schema.

MultiProduct inherits from Product so should have this property yes?

I will post below my schema and the code i'm using for adding a product to a category. Hopefully someone can help and I can provide any more information as neccessary.

--

Schema: (not the whole file)

Category:
  actAs:
    Sluggable:
      fields: [title]
  columns:
    id:
      type:             integer
      primary:          true
      notnull:          true
      autoincrement:    true
    title:
      type:             string(255)
      notnull:          true
    image:
      type:             string(255)
    store_id:
      type:             integer
      notnull:          true
    sort_order:
      type:             integer
  relations:
    Store:
      class:            Store
      local:            store_id
      onDelete:         CASCADE
      foreignAlias:     Categories
    Parents:
      class:            Category
      refClass:         CategoryCategory
      foreign:          parent_category_id
      foreignAlias:     Subcategories

CategoryFeaturedProduct:
  columns:
    category_id:
      type:             integer
      primary:          true
    product_id:
      type:             integer
      primary:          true
  relations:
    Category:
      foreignAlias:     FeaturedProducts
      onDelete:         CASCADE
    Product:
      foreignAlias:     CategoriesFeaturedIn
      onDelete:         CASCADE

Product:
  actAs:
    Sluggable:
      fields: [title]
  columns:
    id:
      type:             integer
      primary:          true
      notnull:          true
      autoincrement:    true
    title:
      type:             string(255)
    description:
      type:             string(255)
    link:
      type:             string(255)
    image:
      type:             string(255)
    large_image:
      type:             string(255)
    featured_image:
      type:             string(255)
    sku:
      type:             string(64)
    mpid:
      type:             string(64)
    stock:
      type:             integer
    price:
      type:             decimal
    rrp:
      type:             decimal
    was_price:
      type:             decimal
    rating:
      type:             decimal
    sort_order:
      type:             integer
    promotion_id:
      type:             integer
    manufacturer_id:
      type:             integer
      notnull:          true
  relations:
    Promotion:
      class:            Promotion
      local:            promotion_id
      onDelete:         SET NULL
      foreignAlias:     Products
    Manufacturer:
      class:            Manufacturer
      local:            manufacturer_id
      onDelete:         CASCADE
      foreignAlias:     Products
    Categories:
      class:            Category
      refClass:         ProductCategory
      foreignAlias:     Products
    CategoriesFeaturedIn:
      class:            Category
      refClass:         CategoryFeaturedProduct
      foreignAlias:     FeaturedProducts

MultiProduct:
  inheritance:
    extends:            Product
    type:               column_aggregation
    keyField:           type
    keyValue:     开发者_开发知识库      2

Code:

$product = ProductTable::getInstance()->find($prod_id);
$category = CategoryTable::getInstance()->find($cat_id);
$product->addCategoryFeaturedIn($category);

Product Class:

public function addCategoryFeaturedIn($category)
{
    $this->CategoriesFeaturedIn[] = $category;
}


I actually managed to resolve this issue although I'm not entirely sure on the answer but I did two things and everything worked.

Firstly I reordered my schema file so it reads like so:

Category:
  actAs:
    Sluggable:
      fields: [title]
  columns:
    id:
      type:             integer
      primary:          true
      notnull:          true
      autoincrement:    true
    title:
      type:             string(255)
      notnull:          true
    image:
      type:             string(255)
    store_id:
      type:             integer
      notnull:          true
    sort_order:
      type:             integer
  relations:
    Store:
      class:            Store
      local:            store_id
      onDelete:         CASCADE
      foreignAlias:     Categories
    Parents:
      class:            Category
      refClass:         CategoryCategory
      foreign:          parent_category_id
      foreignAlias:     Subcategories

Product:
  actAs:
    Sluggable:
      fields: [title]
  columns:
    id:
      type:             integer
      primary:          true
      notnull:          true
      autoincrement:    true
    title:
      type:             string(255)
    description:
      type:             string(255)
    link:
      type:             string(255)
    image:
      type:             string(255)
    large_image:
      type:             string(255)
    featured_image:
      type:             string(255)
    sku:
      type:             string(64)
    mpid:
      type:             string(64)
    stock:
      type:             integer
    price:
      type:             decimal
    rrp:
      type:             decimal
    was_price:
      type:             decimal
    rating:
      type:             decimal
    sort_order:
      type:             integer
    promotion_id:
      type:             integer
    manufacturer_id:
      type:             integer
      notnull:          true
  relations:
    Promotion:
      class:            Promotion
      local:            promotion_id
      onDelete:         SET NULL
      foreignAlias:     Products
    Manufacturer:
      class:            Manufacturer
      local:            manufacturer_id
      onDelete:         CASCADE
      foreignAlias:     Products
    Categories:
      class:            Category
      refClass:         ProductCategory
      foreignAlias:     Products
    CategoriesFeaturedIn:
      class:            Category
      refClass:         CategoryFeaturedProduct
      foreignAlias:     FeaturedProducts

MultiProduct:
  inheritance:
    extends:            Product
    type:               column_aggregation
    keyField:           type
    keyValue:           2

CategoryFeaturedProduct:
  columns:
    category_id:
      type:             integer
      primary:          true
    product_id:
      type:             integer
      primary:          true
  relations:
    Category:
      foreignAlias:     FeaturedProducts
      onDelete:         CASCADE
    Product:
      foreignAlias:     CategoriesFeaturedIn
      onDelete:         CASCADE

I also changed my Product method to read as so:

public function addCategory($category)
{
    $this->getCategories()->add($category);
}

I imagine the reordering of the schema file is what actually did the trick and fixed this problem which does suggest that you need to define relationships in the schema after you have defined the actual tables which are related.

The change of the method probably didnt affect this but its worth noting as this is what I believe to be a better way to add to a many-to-many relationship.

0

精彩评论

暂无评论...
验证码 换一张
取 消