开发者

properties of a n:m relation in doctrine

开发者 https://www.devze.com 2023-03-04 12:35 出处:网络
Hy guys I\'ve got the following schema for my objects: Product: columns: name:{ type: string(255) } Basket:

Hy guys

I've got the following schema for my objects:

Product:
  columns:
    name:          { type: string(255) }

Basket:
  columns:
    current_status: { type: integer }
  relations:
    Products:     { class: Product, refClass: BasketProducts, onDelete: CASCADE }

BasketProducts:
  columns:
    product_id:   { type: integer, primary: true }
    basket_id:    { type: integer, primary: true }
    quantity:     { type: integer(4) }
  relations:
    Product:      { local: product_id, onDelete: CASCADE }
    Basket:       { local: basket_id, onDelete: CASCADE }

Now in the frontend I 开发者_如何学编程try to show the users basket, getting the products by

foreach($basket->getProducts() as $product) {
  echo $product->getId();
  echo $product->getName();
}

The question now, how can i access the quantity field from the BasketProducts?


You will need to query the middle table directly in order to do this.

A good way to do this, is to add a function in your Basket.class.php that will retrieve the data you need based on a BasketID.

You could also create the function in your BasketTable.class.php if you'd like to include the data when fetching a particular basket (ie. getBasketWithProductQuantities())

I don't have any Doctrine code handy at this time.

0

精彩评论

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