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

How Sum input fields values after save in cakephp?

Good afternoon, first of all a cordial greetings to all forum. I have a problem and need help. Please would be so kind to help me with information, text, illustrations, to perform mathematical operations in cakephp, the problem is that I am still a newbie and I was a bit complicated all this, to get a better idea this is my view

表单视图:

http://www.subirimagenes.com/imagedata.php?url=http://s2.subirimagenes.com/otros/9234902vistaprevia.jpg

示例:

形式: 3 * 7.50 = 22.50 形式: 4 * 5.50 = 22 形式: 1 * 4.90 = 4.90 形式: 2 * 12.90 = 25.8

总计:72.50

What I want to do is take the amounts written on the forms and then do the calculation and place in the total field

数据库视图

http://www.subirimagenes.com/imagedata.php?url=http://s2.subirimagenes.com/otros/9234906db.jpg

------------view/add.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>

.......控制器......

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

非常感谢您的关注

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

Example taken from the book:

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