Excellent question to start a guide help for that.
Firstly, do not use the $_GET and $_POST vars. They can contain any kind of stuff. Too dangerous.
Then, it is really easy to do the job easy, fast and safe on your component.
TODO :
models/peoples.php (People, collection)
public function __construct($config = array())
{
//Define the sortables fields (in lists)
if (empty($config['filter_fields'])) {
$config['filter_fields'] = array(
);
}
//Define the filterable fields
$this->set('filter_vars', array(
//HERE
'pid' => 'cmd'
'nid' => 'cmd'
//CMD is accepting chars, but INT is defaulting to 0, so not that good.
));
parent::__construct($config);
}
What does it means behind ?
-> it receive the params 'filter_pid' and 'filter_nid'
-> parse the received values against injection (type cmd)
-> set up 2 states vars for this model : 'filter.pid', 'filter.nid'
Then in prepareQuery(), the trick is to call this filter only if it is requested through url.
Your example is complex, so I make it easy for readers :
if ($this->getState('filter.pid') != null)
$this->addWhere("(a.perfil = " . (int)$this->getState('filter.pid'));
-> Definitively parse to integer for security because int is not quoted
-> Use addWhere() available thanks to Cook, because it is incredible when you mix up a lot of possibles SQL calls, because the model is able to send unique SQL statements and avoid conflicts.
Anyway, try to use addWhere(), addJoin(), addSelect()...
@madaura :
Your example now
if (($this->getState('filter.pid') != null) && ($this->getState('filter.nid') != null))
{
$mad_p = (int)$this->getState('filter.pid');
$mad_n = (int)$this->getState('filter.nid');
$this->addWhere("(a.perfil_1 = $mad_p AND a.tipo_perfil >= $mad_n) OR (a.perfil_2 = $mad_p AND a.tipo_perfil >= $mad_n) OR (a.perfil_3 = $mad_p AND a.tipo_perfil >= $mad_n)");
}
Can be simplified :
(a.perfil_1 = $mad_p AND a.tipo_perfil >= $mad_n)
OR (a.perfil_2 = $mad_p AND a.tipo_perfil >= $mad_n)
OR (a.perfil_3 = $mad_p AND a.tipo_perfil >= $mad_n)
TO
a.tipo_perfil >= $mad_n AND (a.perfil_1 = $mad_p OR a.perfil_2 = $mad_p OR a.perfil_3 = $mad_p)
And maybe better ...
After the Regular Expressions, where is the SQL Guru ???