之前保存CakePHP数学

时间:2011-12-02 23:54:03

标签: php cakephp

我试图通过我的表单提取用户提交的数据,并且不做任何事情,除以100,或者根据字段乘以1,000,000。

我在编写可以成功执行此操作的beforeSave()函数时遇到问题。当我禁用验证时,数据会提交,但我想要发生的数学运算不会发生。

我之所以这样做是因为如果用户输入2.16为2.16%,我们希望将其存储为.0216。表格提交后,它只是基本的数学,对于我的生活,我无法让它发挥作用。

任何建议都将不胜感激。 Haven没有找到关于这个主题的任何好的教程,所以如果有人知道任何有用的东西。

这是我从提交中获得的原始数据数组:

Array
(
    [Subfirmdetail] => Array
        (
            [subfirm_id] => 4
            [subfirmdetailtype_id] => 55
            [value] => 20
            [date] => 2011-12-02
            [active] => yes
            [modified] => 2011-12-02 19:00:06
            [created] => 2011-12-02 19:00:06
        )

)
Array
(
    [Subfirmdetail] => Array
        (
            [subfirm_id] => 4
            [subfirmdetailtype_id] => 56
            [value] => 20
            [date] => 2011-12-02
            [active] => yes
            [modified] => 2011-12-02 19:00:06
            [created] => 2011-12-02 19:00:06
        )

)
Array
(
    [Subfirmdetail] => Array
        (
            [subfirm_id] => 4
            [subfirmdetailtype_id] => 57
            [value] => 20
            [date] => 2011-12-02
            [active] => yes
            [modified] => 2011-12-02 19:00:06
            [created] => 2011-12-02 19:00:06
        )

)
Array
(
    [Subfirmdetail] => Array
        (
            [subfirm_id] => 4
            [subfirmdetailtype_id] => 58
            [value] => 20
            [date] => 2011-12-02
            [active] => yes
            [modified] => 2011-12-02 19:00:06
            [created] => 2011-12-02 19:00:06
        )

)
Array
(
    [Subfirmdetail] => Array
        (
            [subfirm_id] => 4
            [subfirmdetailtype_id] => 59
            [value] => 20
            [date] => 2011-12-02
            [active] => yes
            [modified] => 2011-12-02 19:00:06
            [created] => 2011-12-02 19:00:06
        )

)

这是我的beforeSave函数:

function beforeSave() {
        switch($this->data['Subfirmdetailtype']['calculation']) {
            case 0:
                $this->data['Subfirmdetail']['value'] = $this->data['Subfirmdetail']['value'];
                break;
            case 1:
                $this->data['Subfirmdetail']['value'] = $this->divide($this->data['Subfirmdetail']['value']);
                break;
            case 2:
                $this->data['Subfirmdetail']['value'] = $this->multiply($this->data['Subfirmdetail']['value']);
                break;
        }
        pr($this->data);

        return true;
    }

最后我的add.ctp

    <div class="subfirmdetails form">
<?php echo $this->Form->create('Subfirmdetail', array('name' => 'subfirmdetail'));?>
    <fieldset>
        <legend><?php __('Add Subfirmdetails'); ?></legend>
    <?php if(!empty($this->params['named'])) { ?>    
    <div class="span-7" style="text-align: left;">
    <?php
                //asset allocation entry section
                echo "<b><u><h3>Asset Allocation</h3></u></b>\n";
                $num = 0;
                foreach($allocations as $allocation) {
                    echo "<b>" . $allocation['Subfirmdetailtype']['description'] . "</b><br>\n";
                    echo $form->hidden('Subfirmdetail.' . $num . '.subfirm_id', array('value' => $this->params['named']['subfirm_id'])) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.subfirmdetailtype_id', array('value' => $allocation['Subfirmdetailtype']['id'])) . "\n";
                    echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('class' => 'quantity', 'label' => '', 'onblur' => 'calculateTotal();', 'onkeydown' => 'ForceNumericInput(this, true, true)')) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.date', array('value' => date("Y-m-d"))) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.active', array('value' => 'yes')) . "\n";
                    echo $this->Form->hidden('Subfirmdetailtype.' . $num . '.calculation') . "\n";
                    $num++; 
                }
                echo "<b>Total * (must equal 100%)</b>" . $this->Form->input('total', array('label' => '', 'id' => 'total_quantity', 'value' => 0, 'disabled' => 'disabled'));
    ?>
    </div>
    <div class="span-8">

        <?php
                //asset allocation entry section
                echo "<b><u><h3>General Statistics</h3></u></b>\n";
                //pr($generals);
                foreach($generals as $general) {
                    echo "<b>" . $general['Subfirmdetailtype']['description'] . "</b><br>\n";
                    echo $form->hidden('Subfirmdetail.' . $num . '.subfirm_id', array('value' => $this->params['named']['subfirm_id'])) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.subfirmdetailtype_id', array('value' => $general['Subfirmdetailtype']['id'])) . "\n";
                    if($general['Datatype']['name'] == "select") {
                        echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('label' => '', 'options' => $options[$general['Subfirmdetailtype']['id']]));
                    } else {
                        echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('label' => ''));
                    }
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.date', array('value' => date("Y-m-d"))) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.active', array('value' => 'yes')) . "\n";
                    echo $this->Form->hidden('Subfirmdetailtype.' . $num . '.calculation') . "\n";
                    $num++;
                }
    ?>

    </div>

    <div class="span-8 last">
        <?php
                //asset allocation entry section
                echo "<b><u><h3>Returns</h3></u></b>\n";
                foreach($returns as $return) {
                    echo "<b>" . $return['Subfirmdetailtype']['description'] . "</b><br>\n";
                    echo $form->hidden('Subfirmdetail.' . $num . '.subfirm_id', array('value' => $this->params['named']['subfirm_id'])) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.subfirmdetailtype_id', array('value' => $return['Subfirmdetailtype']['id'])) . "\n";
                    echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('label' => '', 'onkeydown' => 'ForceNumericInput(this, true, true)')) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.date', array('value' => date("Y-m-d"))) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.active', array('value' => 'yes')) . "\n";
                    echo $this->Form->hidden('Subfirmdetailtype.' . $num . '.calculation') . "\n";
                    $num++;
                }
    ?>
    </div>

    <div class="span-8" style="margin-top: 25px;">
    <?php
                //asset allocation entry section
                echo "<b><u><h3>Volatility</h3></u></b>\n";
                foreach($volatilities as $volatility) {
                    echo "<b>" . $volatility['Subfirmdetailtype']['description'] . "</b><br>\n";
                    echo $form->hidden('Subfirmdetail.' . $num . '.subfirm_id', array('value' => $this->params['named']['subfirm_id'])) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.subfirmdetailtype_id', array('value' => $volatility['Subfirmdetailtype']['id'])) . "\n";
                    echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('label' => '', 'onkeydown' => 'ForceNumericInput(this, true, true)')) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.date', array('value' => date("Y-m-d"))) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.active', array('value' => 'yes')) . "\n";
                    echo $this->Form->hidden('Subfirmdetailtype.' . $num . '.calculation') . "\n";
                    $num++;
                }
    ?>
    </div>
    <div class="span-7 last" style="margin-top: 25px;">

        <?php
                //asset allocation entry section
                echo "<b><u><h3>Upside/Downside</h3></u></b>\n";
                foreach($upsidedownsides as $upsidedownside) {
                    echo "<b>" . $upsidedownside['Subfirmdetailtype']['description'] . "</b><br>\n";
                    echo $form->hidden('Subfirmdetail.' . $num . '.subfirm_id', array('value' => $this->params['named']['subfirm_id'])) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.subfirmdetailtype_id', array('value' => $upsidedownside['Subfirmdetailtype']['id'])) . "\n";
                    echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('label' => '', 'onkeydown' => 'ForceNumericInput(this, true, true)')) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.date', array('value' => date("Y-m-d"))) . "\n";
                    echo $this->Form->hidden('Subfirmdetail.' . $num . '.active', array('value' => 'yes')) . "\n";
                    echo $this->Form->hidden('Subfirmdetailtype.' . $num . '.calculation') . "\n";
                    $num++;
                }
    ?>

    </div>


    </fieldset>
    <?php echo $this->Form->end(__('Submit', true));?>
    </div>

     <?php } else {
        echo "Sorry, you have not come from an appropriate subfirm page to add these details.  Please visit a subfirm page before adding data.";
    }  //pr($generals); ?>

</div>

<script language="javascript">
    //function to convert percentages entered in by users (e.g. 20% to 0.20)
    function calculateTotal() {
        var total = 0;
        <?php $num = 0; ?>
        $(".quantity").each(function() {
            if (!isNaN(this.value) && this.value.length != 0) {
                total += parseFloat(this.value);
            }
        }); 

        total = total;

        if (total != 100) {
            total = total + "% - Sorry, total must equal 100%";
            $("#total_quantity").val(total);
        } else {
            total = total + "%";
            $("#total_quantity").val(total);
        }

    }

    //function to force returns inputs to be numbers between 1 - 100

</script>

2 个答案:

答案 0 :(得分:0)

您确定switch语句正确执行吗?我在代码示例中的任何地方都没有看到对此数据的引用,但您在交换机中调用它。

$this->data['Subfirmdetailtype']['calculation']

我建议将这些内容放在一边,看看你遇到了什么案例:

debug('Hello case n'); 
die;

答案 1 :(得分:0)

这是我最终使用的。不得不对我的模型进行一些更改,并将数据拉入适当的部分。如果有人需要做类似的事情......我来帮忙。

function divide($number) {
            return $number / 100;
        }

        function multiply($number) {
            return $number * 1000000;
        }

        function beforeSave() {
            switch($this->data['Subfirmdetail']['calculation']) {
                case "divide":
                    $this->data['Subfirmdetail']['value'] = $this->divide($this->data['Subfirmdetail']['value']);
                    break;
                case "multiply":
                    $this->data['Subfirmdetail']['value'] = $this->multiply($this->data['Subfirmdetail']['value']);
                    break;
                case "none":
                default:
                    $this->data['Subfirmdetail']['value'] = $this->data['Subfirmdetail']['value'];
                    break;
            }

            return true;
        }