In my CakePHP project, there are two dropdown fields for doing a search in the database, both as string's. I need to add two more fields (both dates). In the code below (controller), I used 4 if's to do field verification, when I add the date fields, will I need to create many more if's or is there another easier way?
RegitroHorarioController.php
$horarios = array();
$cliente_id = (isset($this->request->data['RegistroHorario']['cliente_id']) ? $this->request->data['RegistroHorario']['cliente_id'] : null );
$usuario_id = (isset($this->request->data['RegistroHorario']['user_id']) ? $this->request->data['RegistroHorario']['user_id'] : null );
if($cliente_id != null && $usuario_id != null){
$horarios = $this->RegistroHorario->find('all', array(
'conditions' => array('User.id' => $usuario_id, 'Cliente.id' => $cliente_id),
'order' => 'RegistroHorario.data_fim DESC'
));
}elseif($usuario_id != null && $cliente_id == null){
$horarios = $this->RegistroHorario->find('all', array(
'conditions' => array('User.id' => $usuario_id),
'order' => 'RegistroHorario.data_fim DESC'
));
}elseif($usuario_id == null && $cliente_id != null){
$horarios = $this->RegistroHorario->find('all', array(
'conditions' => array('Cliente.id' => $cliente_id),
'order' => 'RegistroHorario.data_fim DESC'
));
}elseif($usuario_id == null && $cliente_id == null){
$horarios = $this->RegistroHorario->find('all', array(
'order' => 'RegistroHorario.data_fim DESC'
));
}
View:
schedules_generation.ctp
<?php echo $this->Form->create('RegistroHorario', array('class' => 'form-horizontal bucket-form', 'autocomplete' => 'off')); ?>
<div class="col-lg-3">
<?php echo $this->Form->select('RegistroHorario.cliente_id', array(null => 'Todos') + $clientes, array('empty' => false ,'div' => false,'label'=>false, 'class' => 'form-control m-bot15', 'width' => '10'));?>
</div><div class="col-lg-3">
<?php echo $this->Form->select('RegistroHorario.user_id', array(null => 'Todos') + $usuarios, array('empty' => false ,'div' => false,'label'=>false, 'class' => 'form-control m-bot15', 'width' => '10'));?>
</div>
<input type="submit" class="btn btn-success" value="Buscar" />
<?php echo $this->Form->end(); ?>