CakePHP:不确定如何处理半困难的关系

发布于 2024-10-31 08:11:52 字数 487 浏览 15 评论 0原文

我正在开发我的第一个 CakePHP 应用程序,一个订单/发票系统。订单部分进展顺利,但对于发票我有点需要一些帮助。

我正在使用的数据库结构送货单由多个产品组成,而这些产品又包含多个可重复使用的元素(例如多个托盘和产品本身) 。现在,由于一些客户订购的数量较大,他们会获得较低的费率,这是基于时间的(元素 y 从第 1 周到第 9 周为 1 欧元,从第 10 周到第 8 周为相同元素为 1.20 欧元)。当然,有些客户只需要使用每日价格,该价格将以相同的方式存储,只是使用 nulled customer_id。

现在我的问题是;我完全不知道应该如何处理发票视图,或更具体地说;获取数据的最佳方法是什么,或者我是否应该去练习我的 SQL 编写技能。

I'm working on my first CakePHP app, an order/invoice system. The order-part are coming along nicely, but for the invoices I kinda need some help.

The database structure I'm using; A delivery note consists of multiple products, which in turn exist of multiple re-usable elements (like a number of trays and the product itself). Now because some customer order larger quantities, they get lower rates, which are time-based (from week 1 to 9 €1 for element y, and from week 10 to 8 €1.20 for the same element). Of course some customers just have to use the daily prices, which will be stored the same way, just witha nulled customer_id.

Now my problem; I have absolutely no idea how I should tackle the invoice view, or more specifically; what the best way is to get the data, or if I should just go and practice my SQL-writing skills.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2024-11-07 08:11:52

我没有发现您的架构有任何重大问题。 Containable 行为应该会让事情变得简单。将其添加到您的发票模型中:

var $actsAs = array('Containable');

通过为每个元素添加 DefaultPrice 关系,让您的生活更轻松。在您的元素模型中:

var $hasOne = array(
    'DefaultPrice' => array(
        'className' => 'Price',
        'foreignKey' => 'element_id',
        'conditions' => array('DefaultPrice.customer_id IS NULL')
    )
);

现在要构建发票,设置包含参数并将其传递给查找操作。 (我假设您想要显示元素成本的细分,对吧?)

$contain  = array(
    'DeliveryNote' => array(
        'Product' => array(
            'Element' => array(
                 'DefaultPrice',
                 'Price' => array(
                     'conditions' => array('Price.customer_id' => $customer_id)
                 )
             )
         )
     )
);
$this->Invoice->find('first', array('conditions' => ..., 'contain' => $contain));

这应该会导致每个元素记录都包含一个 DefaultPrice,并且如果客户收到特殊定价,则还将包含 Price 记录。

注意:您可能需要考虑在“元素”字段中包含 default_price 字段,并避免执行上面的额外连接。每个元素都会有一个默认价格,对吧?

I'm not seeing any major problems with your schema. The Containable behavior should make things easy here. Add it to your Invoice model:

var $actsAs = array('Containable');

Make your life easier by adding a DefaultPrice relationship for each element. In your Element model:

var $hasOne = array(
    'DefaultPrice' => array(
        'className' => 'Price',
        'foreignKey' => 'element_id',
        'conditions' => array('DefaultPrice.customer_id IS NULL')
    )
);

Now to build your invoice, set up and pass a contain parameter to your find operation. (I assume you want to show a breakdown of element costs, right?)

$contain  = array(
    'DeliveryNote' => array(
        'Product' => array(
            'Element' => array(
                 'DefaultPrice',
                 'Price' => array(
                     'conditions' => array('Price.customer_id' => $customer_id)
                 )
             )
         )
     )
);
$this->Invoice->find('first', array('conditions' => ..., 'contain' => $contain));

This should result in each Element record including a DefaultPrice, and if the customer is receiving special pricing, the Price record will also be included.

Note: You may want to consider including a default_price field in the Element field, and avoid having to do the additional join above. Every Element is going to have a default price, right?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文