Created
March 11, 2012 21:46
-
-
Save gabrielslau/2018317 to your computer and use it in GitHub Desktop.
Consulta complexa com o Cake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $highlightCategories = array(1,2,3,4,5,6,7,8,9); // IDs das Categorias selecionadas | |
| // Estrutura principal da consulta | |
| $sql = ""; | |
| $sqlBody = " (SELECT `Noticia`.`id` as `Noticia.id`, `Noticia`.`categoria_id` as `Noticia.categoria_id`, `Noticia`.`titulo` as `Noticia.titulo`, `Noticiascategoria`.`id` as `Noticiascategoria.id`, `Noticiascategoria`.`titulo` as `Noticiascategoria.titulo`, `Noticiascategoria`.`slug` as `Noticiascategoria.slug` FROM `noticias` AS `Noticia` LEFT JOIN `noticiascategorias` AS `Noticiascategoria` ON (`Noticia`.`categoria_id` = `Noticiascategoria`.`id`) WHERE `Noticia`.`status` = '1' AND "; | |
| // Gera a consulta para cada categoria | |
| foreach($highlightCategories as $categoria){ | |
| $sql .= $sqlBody." `Noticia`.`destaque` = '1' AND `Noticia`.`categoria_id` = '".$categoria."' ORDER BY `Noticia`.`id` DESC LIMIT 2) UNION "; | |
| } | |
| // Usa um método para retirar o último UNION da string | |
| $sql = cut_string_using_last('UNION', $sql); | |
| $UltimasNoticias = $this->Noticia->query($sql); | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Formato dos Dados Retornados | |
| Array | |
| ( | |
| [0] => Array | |
| ( | |
| [0] => Array | |
| ( | |
| [Noticia.id] => 3859 | |
| [Noticia.categoria_id] => 24 | |
| [Noticia.titulo] => Um copo ecologicamente correto | |
| [Noticiascategoria.id] => 24 | |
| [Noticiascategoria.titulo] => Meio Ambiente | |
| [Noticiascategoria.slug] => meio-ambiente | |
| ) | |
| ) | |
| [1] => Array | |
| ( | |
| [0] => Array | |
| ( | |
| [Noticia.id] => 3860 | |
| [Noticia.categoria_id] => 23 | |
| [Noticia.titulo] => Encontro de ministros discute importancia das ferrovias. | |
| [Noticiascategoria.id] => 23 | |
| [Noticiascategoria.titulo] => Construção Civil | |
| [Noticiascategoria.slug] => construcao-civil | |
| ) | |
| ) | |
| ... | |
| ... | |
| ... | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment