0

Before any advice to read google and etc, i must write that i read a lot of articles and blogs, and for now i cant figure out how to use HABTM in my case... ;]

I have order system with 3 tables and 3 models:

  1. ORDERS (order_id, customer_id, paymenyOption, etc...)
  2. ITEMS (item_id, size, color, etc...)
  3. CUSTOMERS (customer_id, name, telephone, etc...)

For now i have relationship between ORDERS<->CUSTOMERS (which works fine), but i need to have 'link table' in which will be order_id, product_id, quantity.

To be clear,

  • one order can have many items

  • one items can be in many orders

  • one customer can have many orders (but one order can have one customer)

    (normal order-system philoshopy)

In order view i must have details for 'item', 'customer', and 'order', something like:

Name: Johny Deep (from CUSTOMERS model)
Date ordered: 13-08-2013 (from ORDERS model)
Products: 2x apple, 2x watermelon (from ITEMS model)

I dont know if i must use hasMany or hasAndBelongsTo, and make another model for 'link table'?

Can anyone explain me this beautiful sh** in cakephp 2.x ? ;-)

Community
  • 1
  • 1
masscrx
  • 1
  • 4

2 Answers2

0

There is nothing unique about this situation that you couldn't find elsewhere.

Using model name instead

Order (order_id, customer_id, paymentOption, etc...)
Item (item_id, size, color, etc...)
Customer (customer_id, name, telephone, etc...)
OrderItem (order_id, item_id)

Relationships

Order belongsTo Customer
Order hasMany OrderItem
Item hasMany OrderItem
Customer hasMany Order
OrderItem belongsTo Order
OrderItem belongsTo Item

If you are looking up details on an order you can do

find('first', array(
    'conditions' => array(
        'Order.id' => $order_id // which would have to be defined earlier, perhaps from a param passed in the route
    ),
    'contain' => array(
        'Customer',
        'OrderItem => array(
            'Item'
        )
    )
));

Likewise, it is very easy to pull up all orders by Customer, all order by specific Item, the sum of times a customer has placed an order, the sum of times an Item has been ordered, and the sum of times a customer has ordered a specific item, etc etc etc

bowlerae
  • 924
  • 1
  • 14
  • 37
0

@bowlerae: I found this solution earlier, and it works in 50% in my query, i have code:

$products = $this->Order->find('first', array(

            'contain' => array (
                'Customer',
                'OrderItem' => array(
                    'Item'
                )
            )
                 ));

above (i omit array 'Order' and 'Customer' - are no important now):

array(

'OrderItem' => array(
        (int) 0 => array(
            'order_id' => '1',
            'item_id' => '4',
            'quantity' => '2',
            'Item' => array()
        ),
        (int) 1 => array(
            'order_id' => '1',
            'item_id' => '6',
            'quantity' => '1',
            'Item' => array()
        ),
        (int) 2 => array(
            'order_id' => '1',
            'item_id' => '7',
            'quantity' => '1',
            'Item' => array()
        ),
        (int) 3 => array(
            'order_id' => '1',
            'item_id' => '8',
            'quantity' => '1',
            'Item' => array()
        )
    )
)

1.So how can I get all 'products' now ? i started with something similar to this but i know that it is bad because i must write ['0']...['1]... which is dynamic variable

<?php echo h($products['OrderItem']['0']['item_id']); ?>

2.As you can see

'Item' => array()

is null ;-) so i cant get for example the name of the item from a Item table/model

masscrx
  • 1
  • 4