Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Weighted, Paginated MySQL Search Query for CakePHP

CakePHP formatted, weighted MySQL query

$this->paginate = array(
  'fields' => "*, (MATCH (title) AGAINST ('$q' IN BOOLEAN MODE)*100) + (MATCH (body) AGAINST ('$q' IN BOOLEAN MODE)*10) + MATCH (clients) AGAINST ('$q' IN BOOLEAN MODE) AS rating",
  'conditions' =>  "MATCH (title,body,clients) AGAINST ('$q' IN BOOLEAN MODE)",
  'order' => 'rating DESC',
  'limit' => 10
);
$results = $this->paginate('Article');

A scaffolding input for multple checkboxes with habtm

We have mutiple conditions, which are habtm to

echo "<!--{$form->input('Condition')}-->";
        if(is_array($conditions)){
        foreach($conditions as $condition => $title){
            print $title . "<input type='checkbox' value='{$condition}' name='data[Profile][Profile][]'>". "\n";
            }
        } 

Automatic Pagination

// description of your code here
Automatic Pagination:
(using model bankBranches with fields 'name' and 'bank_id')
var $paginate = array('order' =>array('name' => 'asc')); //paginate by name

OR

$this->set('bankBranches', $this->paginate('BankBranch.bank_id')); //paginate by bank_id


Sometimes one works, and sometimes the other one works...