Is there a way to "summarize" the otherif's?

1

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(); ?>
    
asked by anonymous 09.01.2015 / 16:55

1 answer

1

As you can see, conditions change only the value of conditions . So you can set it separately in this way:

$conditions = array();
if ($cliente_id != null) $conditions["Client.id"] = $cliente_id;
if ($usuario_id != null) $conditions["User.id"] = $usuario_id;

$horarios = $this->RegistroHorario->find('all', array(
    'conditions' => $conditions,
    'order' => 'RegistroHorario.data_fim DESC'
));
    
09.01.2015 / 17:06