Is there any way to "summarize" elseif’s?

Asked

Viewed 65 times

1

In my Cakephp project, there are two dropdown fields to do a database search, both as string’s. I need to add two more fields (both dates). In the code below (controller), I used 4 if’s to check the fields, when I add the date fields, I will need to create many more if’s or there is 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_manage.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(); ?>

1 answer

1


From what I can see, the conditions change only the value of conditions. So you can set it separately 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'
));
  • It worked, thanks!!

  • 2

    As in my previous question, I would mark it as useful, but I still don’t have 15 points to do it.. How much to own them I will mark, even more

Browser other questions tagged

You are not signed in. Login or sign up in order to post.