Skip to content

Instantly share code, notes, and snippets.

public function view($id = null) {
$this->Noticia->id = $id;
if (!$this->Noticia->exists()) {
throw new NotFoundException(__('Invalid noticia'));
}
$this->set('noticia', $this->Noticia->read(null, $id));
}
public function view($id = null) {
$this->Noticia->id = $id;
if (!$this->Noticia->exists()) {
throw new NotFoundException(__('Invalid noticia'));
}
$this->set('noticia', $this->Noticia->read(null, $id));
}
@gabrielslau
gabrielslau / ThumbnailHelper.php
Created March 8, 2012 17:16
Controller para redimensionamento e Upload de imagems (e/ou arquivos diversos)
<?php
/**
* Helper to generate thumbnail images dynamically by saving them to the cache.
* Alternative to phpthumb.
*
* Esta classe foi adaptada para o uso da classe VEROT para manipulação das imagens (por Gabriel Lau)
*
* Inspired in http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
*
* @author Emerson Soares ([email protected])
@gabrielslau
gabrielslau / consulta.php
Created March 11, 2012 21:46
Consulta complexa com o Cake
<?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 ";
@gabrielslau
gabrielslau / ModelValidates.php
Created March 11, 2012 22:11
multiple_validation_sets_cakephp
<?php
/*
** Função para validação personalizada
** @link http://snook.ca/archives/cakephp/multiple_validation_sets_cakephp
*/
function validates($options = array()) {
// copy the data over from a custom var, otherwise
$actionSet = 'validate' . Inflector::camelize(Router::getParam('action'));
if (isset($this->validationSet)) {
$temp = $this->validate;
@gabrielslau
gabrielslau / rotas.php
Created March 11, 2012 22:26
Exemplo de uso de Rotas com o CakePHP
<?php
Router::connect(
'/projetos/:categoria' ,
array('controller' => 'projetos', 'action' => 'index'), // Local à ser redirecionado
array('pass' => array('categoria'), 'categoria' => '[a-zA-Z-0-9_]*')
);
Router::connect(
'/projetos/:categoria/:codigo' ,
array('controller' => 'projetos', 'action' => 'view'), // Local à ser redirecionado
array('pass' => array('codigo'), 'categoria' => '[a-zA-Z-0-9_]*', 'codigo' => '[a-zA-Z-0-9_]{7}')
@gabrielslau
gabrielslau / Cidade.php
Created March 12, 2012 18:53
Models para relacionamento da Geobase (Pais,Estado e Cidade)
<?php
App::uses('AppModel', 'Model');
/**
* Cidade Model
*
*/
class Cidade extends AppModel {
var $name = 'Cidade';
var $displayField = 'nome';
var $useDbConfig = 'geobase';
@gabrielslau
gabrielslau / Projeto.php
Created March 13, 2012 19:17
Página de Cadastro/edição de projeto
<?php
App::uses('AppModel', 'Model');
/**
* Projeto Model
*
*/
class Projeto extends AppModel {
/**
* Display field
*
@gabrielslau
gabrielslau / PagesController.php
Created March 20, 2012 13:56
Código da view utilizada no cadastro e edição do conteúdo de uma página
<?php
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
$this->request->data['Page']['created'] = date('Y-m-d H:i:s');
@gabrielslau
gabrielslau / GeoController.php
Created April 20, 2012 03:39
Script simplificado que realiza uma requisição em AJAX e popula outro campo do formulário
<?php
App::uses('AppController', 'Controller');
/**
* Users Controller
*
* @property User $User
*/
class GeoController extends AppController {
public $components = array('RequestHandler','Json');