如何使用 CakePHP 从估计中获取总价以显示在 index.ctp 中

Posted

技术标签:

【中文标题】如何使用 CakePHP 从估计中获取总价以显示在 index.ctp 中【英文标题】:How to get total price from estimate to show up in index.ctp using CakePHP 【发布时间】:2011-07-21 18:09:53 【问题描述】:

我有 Estimates hasMany EstimateDetails hasMany Items。我要做的是对我的估计索引视图进行总计。

$estimates 对 index.ctp 的调试:

Array
(
[0] => Array
    (
        [Estimate] => Array
            (
                [id] => 1
                [name] => City Base Cinema 10
                [employee_id] => 1
                [created] => 2011-07-12 18:08:37
                [modified] => 2011-07-12 18:08:37
            )

        [Employee] => Array
            (
                [id] => 1
                [first_name] => Edward
                [last_name] => Ramon
                [phone] => 2106959672
                [hourly_pay] => 0.00
                [position_id] => 2
                [created] => 2011-07-12 17:56:42
                [modified] => 2011-07-12 17:56:42
                [fullname] => Edward Ramon
                [Position] => Array
                    (
                        [id] => 2
                        [name] => Estimator
                    )

                [Estimate] => Array
                    (
                        [0] => Array
                            (
                                [id] => 1
                                [name] => City Base Cinema 10
                                [employee_id] => 1
                                [created] => 2011-07-12 18:08:37
                                [modified] => 2011-07-12 18:08:37
                            )

                    )

                [Job] => Array
                    (
                    )

            )

        [EstimateDetail] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [estimate_id] => 1
                        [qty] => 10
                        [item_id] => 1
                        [feet] => 10
                        [inches] => 2
                        [adjustment] => -0.20
                        [Estimate] => Array
                            (
                                [id] => 1
                                [name] => City Base Cinema 10
                                [employee_id] => 1
                                [created] => 2011-07-12 18:08:37
                                [modified] => 2011-07-12 18:08:37
                            )

                        [Item] => Array
                            (
                                [id] => 1
                                [type] => W
                                [eje] => W
                                [description] => 44x335
                                [price unit] => perpound
                                [weight] => 335
                                [price] => 0.80
                                [fulldesc] => W44x335
                            )

                    )

                [1] => Array
                    (
                        [id] => 3
                        [estimate_id] => 1
                        [qty] => 3
                        [item_id] => 1
                        [feet] => 4
                        [inches] => 5
                        [adjustment] => 0.00
                        [Estimate] => Array
                            (
                                [id] => 1
                                [name] => City Base Cinema 10
                                [employee_id] => 1
                                [created] => 2011-07-12 18:08:37
                                [modified] => 2011-07-12 18:08:37
                            )

                        [Item] => Array
                            (
                                [id] => 1
                                [type] => W
                                [eje] => W
                                [description] => 44x335
                                [price unit] => perpound
                                [weight] => 335
                                [price] => 0.80
                                [fulldesc] => W44x335
                            )

                    )

                [2] => Array
                    (
                        [id] => 4
                        [estimate_id] => 1
                        [qty] => 10
                        [item_id] => 1
                        [feet] => 10
                        [inches] => 10
                        [adjustment] => 0.00
                        [Estimate] => Array
                            (
                                [id] => 1
                                [name] => City Base Cinema 10
                                [employee_id] => 1
                                [created] => 2011-07-12 18:08:37
                                [modified] => 2011-07-12 18:08:37
                            )

                        [Item] => Array
                            (
                                [id] => 1
                                [type] => W
                                [eje] => W
                                [description] => 44x335
                                [price unit] => perpound
                                [weight] => 335
                                [price] => 0.80
                                [fulldesc] => W44x335
                            )

                    )

                [3] => Array
                    (
                        [id] => 5
                        [estimate_id] => 1
                        [qty] => 10
                        [item_id] => 3
                        [feet] => 10
                        [inches] => 10
                        [adjustment] => 0.00
                        [Estimate] => Array
                            (
                                [id] => 1
                                [name] => City Base Cinema 10
                                [employee_id] => 1
                                [created] => 2011-07-12 18:08:37
                                [modified] => 2011-07-12 18:08:37
                            )

                        [Item] => Array
                            (
                                [id] => 3
                                [type] => W
                                [eje] => W
                                [description] => 44x290
                                [price unit] => perpound
                                [weight] => 290
                                [price] => 0.80
                                [fulldesc] => W44x290
                            )

                    )

                [4] => Array
                    (
                        [id] => 6
                        [estimate_id] => 1
                        [qty] => 10
                        [item_id] => 6
                        [feet] => 10
                        [inches] => 2
                        [adjustment] => 0.00
                        [Estimate] => Array
                            (
                                [id] => 1
                                [name] => City Base Cinema 10
                                [employee_id] => 1
                                [created] => 2011-07-12 18:08:37
                                [modified] => 2011-07-12 18:08:37
                            )

                        [Item] => Array
                            (
                                [id] => 6
                                [type] => PL
                                [eje] => PL
                                [description] => 4X3
                                [price unit] => ea
                                [weight] => 1
                                [price] => 0.30
                                [fulldesc] => PL4X3
                            )

                    )

            )

        [Job] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [job_number] => 2353
                        [name] => City Base Cinema 10
                        [company_id] => 1
                        [estimate_id] => 1
                        [contract_amount] => 253000
                        [employee_id] => 2
                        [location_id] => 1
                        [created] => 2011-07-12 18:17:59
                        [modified] => 2011-07-16 12:17:55
                        [Company] => Array
                            (
                                [id] => 1
                                [name] => Search Construction
                                [address] => 123 street
                                [city] => San Antonio
                                [state] => TX
                                [zip] => 78023
                                [company_type_id] => 1
                                [created] => 2011-07-12 18:16:37
                                [modified] => 2011-07-12 18:16:37
                            )

                        [Estimate] => Array
                            (
                                [id] => 1
                                [name] => City Base Cinema 10
                                [employee_id] => 1
                                [created] => 2011-07-12 18:08:37
                                [modified] => 2011-07-12 18:08:37
                            )

                        [Employee] => Array
                            (
                                [id] => 2
                                [first_name] => Adam
                                [last_name] => Morales
                                [phone] => 
                                [hourly_pay] => 20.00
                                [position_id] => 1
                                [created] => 2011-07-16 12:16:49
                                [modified] => 2011-07-16 12:16:49
                                [fullname] => Adam Morales
                            )

                        [Location] => Array
                            (
                                [id] => 1
                                [address] => 456 street
                                [city] => Helotes
                                [state] => TX
                                [zip] => 78023
                                [location_type_id] => 1
                                [full_local] => 456 street Helotes, TX 78023
                            )

                    )

            )

    )

)

我尝试在 Estimate Model 上设置 variableField:

var $virtualFields = array('total' => 'SUM(EstimateDetail.qty*EstimateDetail.Item.weight*EstimateDetail.Item.price)');

我的索引.ctp:

<tr>
        <th><?php echo $this->Paginator->sort('id');?></th>
        <th><?php echo $this->Paginator->sort('name');?></th>
        <th><?php echo $this->Paginator->sort('employee');?></th>
        <th><?php echo $this->Paginator->sort('created');?></th>
        <th><?php echo $this->Paginator->sort('modified');?></th>
        <th class="actions"><?php __('Actions');?></th>
</tr>
<?php
$i = 0;
foreach ($estimates as $estimate):
    $class = null;
    if ($i++ % 2 == 0) 
        $class = ' class="altrow"';
    
?>
<tr<?php echo $class;?>>
    <td><?php echo $estimate['Estimate']['id']; ?>&nbsp;</td>
    <td><?php echo $estimate['Estimate']['name']; ?>&nbsp;</td>
    <td>
        <?php echo $this->html->link($estimate['Employee']['fullname'], array('controller' => 'employees', 'action' => 'view', $estimate['Employee']['id'])); ?>
    </td>
    <td><?php echo $estimate['Estimate']['created']; ?>&nbsp;</td>

    <td class="actions">
        <?php echo $this->Html->link(__('View', true), array('action' => 'view', $estimate['Estimate']['id'])); ?>
        <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $estimate['Estimate']['id'])); ?>
        <?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $estimate['Estimate']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $estimate['Estimate']['id'])); ?>
    </td>
</tr>

我得到一个错误。

我得到的错误是:

Warning (2): Invalid argument supplied for foreach() [APP/views/estimates/index.ctp, line 15]

和 Sql:

1054: Unknown column 'EstimateDetail.qty' in 'field list

请帮忙。

【问题讨论】:

您需要在此处发布错误,我们无法猜测。我在某处听说您不能将聚合与 virtualFields 一起使用,不确定是否是问题所在。 【参考方案1】:

简短的回答是 - 不要试图用 SQL/virtualFields 做你正在做的事情(如果你愿意,我可以详细说明原因)。

只需通过foreach($estimates) 迭代您的结果并在 PHP 中自己计算总和。

更新:在您的示例中,您不能(轻松)在 find() 中使用聚合 (SUM),因为:

    为此,find 应该在一个查询中完成 - 即使用 JOINs - 并且 Cake 不会在 hasMany 关系上自愿使用 JOINs(它会使用许多简单的 SELECTs),这意味着你必须手动完成 (http://book.cakephp.org/view/1047/Joining-tables)。 使用SUM 的查询将只返回一行(分组),这意味着您必须执行简单的子查询才能同时获得SUM 和您的结果。

【讨论】:

是的,如果您可以详细说明并且结果已经在 foreach 中,但是我如何获得 item 数组和 EstimaeDetail 数组的计算? 我加了 $total = 0; foreach($estimate['EstimateDetail'] as $estimateDetail) $total += ($estimateDetail['Item']['price']+$estimateDetail['adjustment'])*$estimateDetail['Item']['重量']*($estimateDetail['feet']+($estimateDetail['inches']/12))*$estimateDetail['qty'];并得到了我想要的结果。谢谢lxa

以上是关于如何使用 CakePHP 从估计中获取总价以显示在 index.ctp 中的主要内容,如果未能解决你的问题,请参考以下文章

在Cakephp 3.6中如何获取图像的大小和类型以通过表单发送?

如何在 cakephp 中获取 $_SESSION 值

如何使用 OpenCV 绘制 3D 坐标轴以进行人脸姿态估计?

Prestashop - 购物篮总价

如何从 CakePHP 3 中的查询对象获取参数

CakePHP 问题:如何从单选按钮中获取价值以及如何调用操作?