Skip to content

Instantly share code, notes, and snippets.

@patrickmaciel
Created March 20, 2012 14:07
Show Gist options
  • Save patrickmaciel/2135988 to your computer and use it in GitHub Desktop.
Save patrickmaciel/2135988 to your computer and use it in GitHub Desktop.
MarcasController.php - Action: admin_categoria($categoria_id, $slug) - Listar marcas por categoria
<?php
App::uses('AppModel', 'Model');
/**
* Categoria Model
*
* @property Categorias $Categorias
* @property Marca $Marca
* @property Veiculo $Veiculo
* @property Opcionai $Opcionai
* @property Plano $Plano
*/
class Categoria extends AppModel {
public $name = 'Categoria';
public $actsAs = array('Containable', 'Tree');
public $useTable = 'categorias';
public $displayField = "nome";
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'nome' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'antigo' => array(
'boolean' => array(
'rule' => array('boolean'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'ativo' => array(
'boolean' => array(
'rule' => array('boolean'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Modelo' => array(
'className' => 'Modelo',
'foreignKey' => 'categoria_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Veiculo' => array(
'className' => 'Veiculo',
'foreignKey' => 'categoria_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Opcional' => array(
'className' => 'Opcional',
'joinTable' => 'categorias_opcionais',
'foreignKey' => 'categoria_id',
'associationForeignKey' => 'opcional_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
public function getModel($tipo = null){
if($tipo != null && $tipo != ''){
if(is_numeric($tipo)){
switch($tipo) {
case 159:
$conditions['PecaAcessorio.categoria_id'] = $tipo;
break;
case 160:
$conditions['Servico.categoria_id'] = $tipo;
break;
default:
$conditions['Veiculo.categoria_id'] = $tipo;
break;
}
$tipo = $this->find('first',array(
'conditions' => array('Categoria.id'=>$tipo),
'recursive' => -1
));
$model = strtolower($tipo['Categoria']['model']);
} else {
$model = strtolower($tipo);
}
} else {
$model = 0;
}
return $model;
}
}
<?php
App::uses('AppModel', 'Model');
/**
* Marca Model
*
* @property Categoria $Categoria
* @property Modelo $Modelo
*/
class Marca extends AppModel {
public $displayField = 'nome';
public $actsAs = array('Containable');
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'categoria_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'nome' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'ativo' => array(
'boolean' => array(
'rule' => array('boolean'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Modelo' => array(
'className' => 'Modelo',
'foreignKey' => 'marca_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public function findByTipo($findTipo,$tipo,$params = null){
$cond['Marca.ativo'] = true;
$cond['Categoria.model'] = $this->Modelo->Categoria->getModel($tipo);
if($params != null){
$params = array_merge(
array(
'recursive' => -1,
'fields' => array('DISTINCT Marca.id'),
'conditions' => $cond,
'joins'=>array(
array(
'table' => 'modelos',
'alias' => 'Modelo',
'type' => 'INNER',
'conditions' => array(
'Marca.id = Modelo.marca_id'
)
),array(
'table' => 'categorias',
'alias' => 'Categoria',
'type' => 'INNER',
'conditions' => array(
'Categoria.id = Modelo.categoria_id'
)
)
)
),
$params
);
}else{
$params = array(
'recursive' => -1,
'fields' => array('Marca.id','Marca.nome'),
'conditions' => $cond,
'joins'=>array(
array(
'table' => 'modelos',
'alias' => 'Modelo',
'type' => 'INNER',
'conditions' => array(
'Marca.id = Modelo.marca_id'
)
),array(
'table' => 'categorias',
'alias' => 'Categoria',
'type' => 'INNER',
'conditions' => array(
'Categoria.id = Modelo.categoria_id'
)
)
)
);
}
return $this->find($findTipo,$params);
}
}
<?php
class MarcasController extends AppController{
/**
* admin_categoria
*
* @param string id
* @return void
*/
public function admin_categoria($categoria_id, $slug = null) {
$this->paginate = array(
'contain' => array(
'Modelo' => array(
'Categoria'
)
),
'conditions' => array(
'Modelo.Categoria.id' => $categoria_id
)
);
$this->set('marcas', $this->paginate());
}
?>
<?php
App::uses('AppModel', 'Model');
/**
* Modelo Model
*
* @property Marca $Marca
* @property Verso $Verso
*/
class Modelo extends AppModel {
public $actsAs = array('Containable');
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'marca_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'nome' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'ativo' => array(
'boolean' => array(
'rule' => array('boolean'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Marca' => array(
'className' => 'Marca',
'foreignKey' => 'marca_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Categoria' => array(
'className' => 'Categoria',
'foreignKey' => 'categoria_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Versao' => array(
'className' => 'Versao',
'foreignKey' => 'modelo_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment