2015-01-12 48 views
1

大家下午好,首先向所有論壇致以親切的問候。我有 有問題,需要幫助。請幫我用 信息,文字,插圖,在cakephp中執行 的數學運算,問題是我還是新手,我有點複雜了,這是爲了得到一個更好的主意,這是我的視圖如何在cakephp中保存輸入字段值?

窗體視圖:

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

我想要做的就是採取書面的金額表格上的,然後做 在總場

DB視圖

的計算和處

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

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

非常感謝您對我們的關注

回答

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

使用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)); 
} 
+0

謝謝回答這個問題。 你能幫我嗎?,我嘗試beforesave()函數,但不理解邏輯,我一直試圖做這個4天。 –

+0

在文檔和提供的示例中無法理解什麼?只需進行計算並將結果設置到總場中即可完成。示例代碼恰好顯示了更改日期的情況。 – burzum