Forms 在cakephp中保存后,如何对输入字段值求和?

Forms 在cakephp中保存后,如何对输入字段值求和?,forms,cakephp,input,sum,Forms,Cakephp,Input,Sum,下午好,首先向各位论坛致以亲切的问候。我有 一个问题,需要帮助。请帮我处理这件事 用于执行数学运算的信息、文本、插图 在cakephp中,问题是我还是一个新手,有点 把这一切复杂化,为了更好地了解这是我的观点 表单视图: 例如: 表格:     3 * 7.50 = 22.50 表格:     4 * 5.50 = 22 表格:     1 * 4.90 = 4.90 表格: 2*12.90=25.8 总数:72.50 我想做的是把写在表格上的金额记下来,然后 计算和放置在total字段

下午好,首先向各位论坛致以亲切的问候。我有 一个问题,需要帮助。请帮我处理这件事 用于执行数学运算的信息、文本、插图 在cakephp中,问题是我还是一个新手,有点 把这一切复杂化,为了更好地了解这是我的观点

表单视图:

例如:

表格:     3 * 7.50 = 22.50 表格:     4 * 5.50 = 22 表格:     1 * 4.90 = 4.90 表格: 2*12.90=25.8

总数:72.50

我想做的是把写在表格上的金额记下来,然后 计算和放置在total字段中

数据库视图

----------查看/添加ctp

<div class="ventas form">
   <?php echo $this->Form->create('Venta'); ?>
   <fieldset>
    <legend><?php echo __('PROCESO DE COMPRA'); ?></legend>
    <?php
    echo $this->Form->input('nombre');
    echo $this->Form->input('apellido');
    echo $this->Form->input('cedula');
    echo $this->Form->input('direccion');
    echo $this->Form->input('mail');
    echo $this->Form->input('telefono');
    echo $this->Form->input('tarjeta');
    echo $this->Form->input('numtarjeta');
    echo __('<legend>SELECCIONE SU PELICULA</legend>'); 
    echo $this->Form->input('cartelera_id',array('label' => 'Seleccione su pelicula'));
    echo $this->Form->input('cant_adulto', array('label' => 'Cantidad de boletos - Precio normal $ 7,50'));
    echo $this->Form->input('cant_niño', array('label' => 'Cantidad de boletos - Precio niños/ancianos $ 5,50'));
    echo $this->Form->input('cant_discapacitado', array('label' => 'Cantidad de boletos - Precio discapacitados $ 4,90'));

echo __('<legend>SELECCIONE SU COMBO</legend>'); 
    echo $this->Form->input('combo_id');
    echo $this->Form->input('numcombo', array('label' => 'Cantidad de combos - Precio discapacitados $ 12,90'));
    echo $this->Form->input('total');
    ?>
   </fieldset>

<?php echo $this->Form->end(__('Guardar')); ?>

</div>
非常感谢您的关注

使用模型的回调。在那里进行计算并写入字段


谢谢你回答这个问题。您能帮助我吗?我尝试使用beforesave()函数,但不理解其逻辑,我已经尝试了4天。文档和提供的示例中有什么不可理解的地方?只需进行计算并将结果设置为“总计”字段,即可完成计算。示例代码仅通过更改日期就完全显示了这一点。
   class VentasController extends AppController {
/** * Components
 * * @var array
 */ public $components = array('Paginator');
/** * index method
 * * @return void
 */
    public function index() {
        $this->Venta->recursive = 0;
        $this->set('ventas', $this->Paginator->paginate());
    }/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        if (!$this->Venta->exists($id)) {
            throw new NotFoundException(__('Invalid venta'));
        }
        $options = array('conditions' => array('Venta.' . $this->Venta->primaryKey => $id));
        $this->set('venta', $this->Venta->find('first', $options));
    }
/**
 * add method *
 * @return void */
    public function add() {
        if ($this->request->is('post')) {
            $this->Venta->create();
            if ($this->Venta->save($this->request->data)) {
                $this->Session->setFlash(__('La venta se almacenó correctamente.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('La venta no se almacenó correctamente. Por favor, Intente de nuevo.'));
            }
        }
        $carteleras = $this->Venta->Cartelera->find('list',array('fields'=>'pelicula'));
        $this->set(compact('carteleras'));
        $combos = $this->Venta->Combo->find('list',array('fields'=>'nombre'));
        $this->set(compact('combos'));
    }
/** * edit method
 * * @throws NotFoundException
 * @param string $id
 * @return void
 */ public function edit($id = null) {
        if (!$this->Venta->exists($id)) {
            throw new NotFoundException(__('Invalid venta'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->Venta->save($this->request->data)) {
                $this->Session->setFlash(__('La venta se almacenó correctamente.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('La venta no se almacenó correctamente. Por favor, Intente de nuevo.'));
            }
        } else {
            $options = array('conditions' => array('Venta.' . $this->Venta->primaryKey => $id));
            $this->request->data = $this->Venta->find('first', $options);
        }
        $carteleras = $this->Venta->Cartelera->find('list');
        $this->set(compact('carteleras'));
        $combos = $this->Venta->Combo->find('list');
        $this->set(compact('combos'));
    }
/**
 * delete method *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */ public function delete($id = null) {
        $this->Venta->id = $id;
        if (!$this->Venta->exists()) {
            throw new NotFoundException(__('Invalid venta'));
        }
public function beforeSave($options = array()) {
    if (!empty($this->data['Event']['begindate']) &&
        !empty($this->data['Event']['enddate'])
    ) {

        $this->data['Event']['begindate'] = $this->dateFormatBeforeSave(
            $this->data['Event']['begindate']
        );
        $this->data['Event']['enddate'] = $this->dateFormatBeforeSave(
            $this->data['Event']['enddate']
        );
    }
    return true;
}

public function dateFormatBeforeSave($dateString) {
    return date('Y-m-d', strtotime($dateString));
}
public function vent() {
   if ($this->request->is('post')) {
       $this->Venta->create();
       $this->request->data['Venta']['total'] = ($this->request->data['Venta']        
        ['cant_adulto'] * 7.5) + ($this->request->data['Venta']['cant_niño'] *  
        5.5) +  ($this->request->data['Venta']['cant_discapacitado'] * 4.9)+ 
        ($this->request->data['Venta']['numcombo'] * 12.9) ;
   if ($this->Venta->save($this->request->data)) {

   $this->Session->setFlash(__('La venta se almacenó correctamente.'));
   $inserted_id=$this->Venta->id;
   return $this->redirect(array('action' => 'view', $inserted_id));
   } else {
   $this->Session->setFlash(__('La venta no se almacenó correctamente. Por   
   favor, Intente de nuevo.'));
   }}
   $carteleras = $this->Venta->Cartelera-  
                 >find('list',array('fields'=>'pelicula'));
   $this->set(compact('carteleras'));
   $combos = $this->Venta->Combo->find('list',array('fields'=>'nombre'));
   $this->set(compact('combos'));
}