Search code examples
phpmysqldoctrine-1.2dql

PHP / Doctrine 1.2 / MySQL - Identifying one value depending on another value


I'm working on an online warehouse/shopping system (php + doctrine + mysql), where each product can have more than one price depending on the quantity. I have two tables (1) Product and (2) Price:

Product

id    name
1     product1  
2     product2
3     product3

Price

id  product_id     from    to      price
1   1              1       10      90
2   1              11      20      80
3   1              21      30      70

As you can see, product1 has 3 different prices. For instance, if the customer buy between 1 and 10 product1, he should get the first price - 90, between 11 and 20 the next price - 80 and so on.

Since the calculation is progressive the total result will be:

Invoice

id  product_id   quantity    price_id
1   1            10          1
2   1            10          2
3   1            10          3

The question is what is the best way to identify the price level depending on the product quantity using Doctrine.

Thanks in advance!


Solution

  • Create a getPrice() method in your Product model and have it do the calculations. Then you can invoke it with: $product->price or $product->getPrice(). I would avoid trying to make the calculations with your DQL/SQL queries