Flask
Flask

Reputation: 4996

properties of a n:m relation in doctrine

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?

Upvotes: 2

Views: 105

Answers (1)

jgallant
jgallant

Reputation: 11273

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.

Upvotes: 1

Related Questions