Ver Mensaje Individual
  #1 (permalink)  
Antiguo 07/04/2014, 17:38
Avatar de fegm_4
fegm_4
 
Fecha de Ingreso: febrero-2013
Mensajes: 114
Antigüedad: 11 años, 2 meses
Puntos: 8
Pregunta Consulta de mysqli con condiciones (Clase abstracta)

Actualmente estoy utilizando el modelo MVC que propone Eugenia Bahit en este link.

La cosa es que va todo bien hasta que trato de hacer una consulta con condicionales para un motor de búsqueda.

La función es
Código PHP:
Ver original
  1. public function getAll($ini = 0, $nreg = 25, $condition = '', $restricted = NULL) {
  2.         $this->rows = array();
  3.         $this->query = "SELECT DISTINCT r.id AS id, r.number AS number FROM request_unit AS ru INNER JOIN request AS r ON ru.order_form_id = r.id INNER JOIN transformer AS tx ON ru.transformer_id = tx.id INNER JOIN power AS p ON tx.power_id = p.id INNER JOIN voltage AS v ON tx.voltage_id = v.id INNER JOIN type AS t ON tx.types_id = t.id ";
  4.         if ($condition == '' && is_null($restricted)) {
  5.             $this->query .= "ORDER BY r.date_created DESC LIMIT $ini, $nreg";
  6.         } elseif ($condition == '' && !is_null($restricted)) {
  7.             $this->query .= "WHERE r.user_id = $restricted ORDER BY r.date_created DESC LIMIT $ini, $nreg";
  8.         } elseif ($condition != '' && is_null($restricted)) {
  9.             $this->query .= "WHERE (CONCAT(p.power_value,'kVA ',v.voltage_name,'V') LIKE '%$condition%') OR (t.type_name LIKE '%$condition%') OR (r.`comment` LIKE '%$condition%') OR (ru.notes LIKE '%$condition%') OR (r.date_created LIKE '%Intempeerie%') ORDER BY r.date_created DESC LIMIT $ini, $nreg";
  10.         } elseif ($condition != '' && !is_null($restricted)) {
  11.             $this->query .= "WHERE ((CONCAT(p.power_value,'kVA ',v.voltage_name,'V') LIKE '%$condition%') OR (t.type_name LIKE '%$condition%') OR (r.`comment` LIKE '%$condition%') OR (ru.notes LIKE '%$condition%') OR (r.date_created LIKE '%Intempeerie%')) AND r.user_id = $restricted ORDER BY r.date_created DESC LIMIT $ini, $nreg";
  12.         }
  13.         $this->get_results_from_query();
  14.         //$this->mensaje = $this->query;
  15.         if (count($this->rows) == 1) {
  16.             $data = $this->rows;
  17.             //$this->mensaje = 'La busqueda arroja resultados';
  18.         } else {
  19.             $data = array();
  20.             //$this->mensaje = 'La busqueda no arroja resultados';
  21.         }
  22.        
  23.         return $data;
  24.     }

Esta función pertenece a una clase que se llama request que extiende una clase abstracta como la que se muestra en la literatura del link.

La función que hace la consulta en cuestión es
Código PHP:
Ver original
  1. protected function get_results_from_query() {
  2.         $this->open_connection();
  3.         $result = $this->conn->query($this->query);
  4.         while ($this->rows[] = $result->fetch_assoc());
  5.         $result->close();
  6.         $this->mensaje = $this->conn->error;
  7.         $this->close_connection();
  8.         array_pop($this->rows);
  9.     }

Probé la sentencia MySQL que resulta del condicional en PHPMyAdmin y da resultado.

Probé con una salida justo antes del array_pop (colocando $this->mensaje = $this->rows) y me da resultado, el problema es que si quito el array_pop de la linea 8, me trae consecuencia en las otras que he hecho por lo que supongo que algo anda mal con mi función pero no logro identificar el problema.

Agradeceria mucho la ayuda
__________________
--
Aqui fegm_4