Patrick Maciel
Patrick Maciel

Reputation: 4944

Using CakePHP, save extra field (id, value, amount) HABTM tables

Hello Everything is right? My problem is the following:

I have 3 tables:

create table products (
    id int not null auto_increment primary key,
    name varchar(80) not null,
    mold varchar(80),
    amount int,
    value decimal(10,2),
    real_value decimal(10,2),
    created datetime,
    modified datetime,
    active boolean default 1
);

create table sales (
    id int not null auto_increment primary key,
    customer_id int not null,
    method_payment_id int not null,
    portions smallint(4),
    entry decimal(10,2),
    subtotal decimal(10,2),
    total decimal(10,2),
    debtor decimal(10,2), 
    expiration_date datetime,
    created datetime,
    modified datetime,
    active boolean default 1
);

create table products_sales (
    product_id int not null,
    sale_id int not null,
    amount smallint(4),
    value decimal(10,2)
);

In products_sales (HABTM) I had to add two extra fields (amount and value), because the value of a product is unstable and the other is the quantity purchased by the customer.

When I put in add() page:

  echo $this->form->input('Product');

CakePHP renders a , until then I understand.

But my need is: the customer must choose the product and the amount of it, or even if it is, the product value is also sent, not only the id (anything, I create a page just to report the amount, after confirmation 'the sale').

Then:
1 - How do I save the ID and value of the product at the same time?
2 - I am using the tables / relationships the right way?
3 - what better way to do this?

Sorry for my English. I hope you understand.

Upvotes: 1

Views: 1893

Answers (1)

bjudson
bjudson

Reputation: 4083

Rather than HABTM, I think you want to use "hasMany through", where you create a separate model for products_sales. Here's a description from the Cake manual:

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model

There's a more complete description in the Cake 1.3 manual:

http://book.cakephp.org/view/1650/hasMany-through-The-Join-Model

Upvotes: 3

Related Questions