在cakephp的控制器中获取日期

时间:2017-09-25 15:34:18

标签: date cakephp

我有一个带数据贴纸的日期表单,我想将这些日期发送到控制器,将这些日期放在一个查询中,我的问题是我不知道如何捕获它们以在查询中插入它们,在视图中我有:



<?php $this->end(); ?>
<?=$this->Form->create(false, array('type' => 'get', 'controller' => 'informes', 'action' => 'crm')); ?>

<div class="row">
	<div class="col-sm-2">Fecha del Informe</div>
	<div class="col-sm-4">
		<div class="input-daterange input-group" id="datepicker">
			<span class="input-group-addon">Desde</span>
			<input type="text" class="input-sm form-control" name="start" id="start"/>
			<span class="input-group-addon">hasta</span>
			<input type="text" class="input-sm form-control" name="end" id="end"/>
		</div>
	</div>
	<div class="col-sm-2">
	</div>
	<div class="col-sm-2"><button class="btn btn-xs btn-primary" type="submit">Generar Informe</button></div>
</div>
<?=$this->Form->end(); ?>
&#13;
&#13;
&#13;

并在控制器中:

// Loading different models
$this->loadModel('Centro');
$this->loadModel('Items');
$this->loadModel('Sesion');
$this->loadModel('Zona');
$this->loadModel('TratamientosZona');
$this->loadModel('tratamiento');
$this->loadModel('Paciente');

// My query with CakePHP ORM
$sesion_pagada = $this->Centro->find('all', array(
    array('fields' => 'Centro.nombre', SUM(IF('Paciente.sexo' = 'F', 'Zona.valor_mujer', 'Zona.valor_hombre')) AS suma
        'joins' => array(
            array('alias' => 'Sesion', 'table' => 'sesions', 'type' => 'INNER',
                'conditions' => 'Centro.id = Sesion.centro_id'),
            array('alias' => 'Items', 'table' => 'items', 'type' => 'LEFT',
                'conditions' => 'Sesion.id = Item.sesion_id'),
            array('alias'), => 'Zona', 'table' => 'zonas', 'type' => 'INNER',
                'conditions' => 'Item.zona_id = Zona.id',
            array('alias' => 'TratamientosZona', 'table' => 'tratamientos_zonas', 'type' => 'INNER',
            'conditions' => 'Zona.id = TratamientosZona.zona_id'),
            array('alias' => 'Tratamiento', 'table' => 'tratamientos', 'type' => 'INNER',
            'conditions' => 'Tratamiento_zona.tratamiento_id = Tratamiento.id'),
            array('alias' => 'Paciente', 'table' => 'pacientes', 'type' => 'INNER',
            'conditions' => 'Tratamiento.paciente_id = Paciente.id')
        ),
        'conditions' => array(
        'Sesion.estado_id =' => 5,
        'Sesion.fecha BETWEEN ? and ?'=> array( $query['start'], date('Y-m-d'))
    )
)));

1 个答案:

答案 0 :(得分:2)

您可以使用此代码从GET请求中获取数据:

// For cakePHP 2.x
$start = $this->request->query['start'];
$end = $this->request->query['end'];

// For cakePHP 3.x >= 3.4.0
$start = $this->request->getQuery('start');
$end = $this->request->getQuery('end');

// cakePHP 3.x <= 3.4.0
$start = $this->request->query('start');
$end = $this->request->query('end');