gpt4 book ai didi

php - 使用 CakePHP,保存额外字段(id、值、数量)HABTM 表

转载 作者:行者123 更新时间:2023-11-29 05:39:29 25 4
gpt4 key购买 nike

你好一切都好吗?我的问题如下:

我有 3 个表:

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)
);

products_sales (HABTM) 中,我不得不添加两个额外的字段(amountvalue),因为产品的值(value)是不稳定的另一个是客户购买的数量。

当我放入 add() 页面时:

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

CakePHP 渲染一个 ,直到那时我才明白。

但我的需求是:客户必须选择产品及其数量,或者即使是,产品值(value)也是< strong>发送,不仅 ID(任何东西,我创建一个页面只是为了报告金额,在确认“销售”之后)。

然后:
1 - 如何同时保存商品ID和商品值(value)?
2 - 我是否以正确的方式使用表格/关系?
3 - 有什么更好的方法来做到这一点?

对不起我的英语。我希望你能理解。

最佳答案

我认为您想使用“hasMany through”而不是 HABTM,您可以在其中为 products_sales 创建一个单独的模型。这是 Cake 手册中的描述:

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

Cake 1.3 手册中有更完整的描述:

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

关于php - 使用 CakePHP,保存额外字段(id、值、数量)HABTM 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7997014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com