Archive
Cakephp 1.2.x: Distinct Select
Just a reminder for myself:
$foo = $this->$model->find('all', array('fields'=>array("DISTINCT {$model}.dnid")));
CakePHP 1.1.x style used to be:
$this->$model->findAll(null, "DISTINCT {$model}.city");
CakePHP: jQuery styling of flash messages
CakePHP session flash messages is one of the most useful things for usability, and user notification. Trying to make it more fancy, I came to jQuery framework with its HighlightFade plugin.
Adding just a couple of lines, we can get nice effects, looking the same as Wordpress messages in Posting/Editing:
var $j = jQuery.noConflict(); //I'm using Prototype as well, so we don't need conflicts
$j(document).ready(function() {
if( $j("#flashMessage") ){
$j("#flashMessage").highlightFade({color:'#24F273',speed:2000, iterator:'exponential'});
}
});
Thus, next time you will pass $this->Session->setFlash('Foo');, your flash message with default
will be used with above HighLight effect.
CakePHP: named over url parameters passing
Playing around with CakePHP parameters passing through URL, I’ve noticed that using Paginator helper for listing entities mixes up named and url params in the URL.
For instance, classical paginator URL is:
http://domain.com/controller/action/page:number
which looks like:
Array(
[named] =>Array(
[page] => number
)
)
Any parameter used in the Paginator goes inside of the URL and doesn’t affect the behaviour (in my case, URL param is used for language switching), i.e. http://domain.com/controller/action/?param=foo/page:number
No matter what your $this->params['url']['param'] contains – it won’t reflect on the logic.
Thus, if we use another variable $this->params['named']['foo'], we can always access it in our viewers and utilize it in the $paginator variable, so our URL will look like:
http://domain.com/controller/action/page:number/foo:bar
Useful links:
Additional parameters in $paginator
CakePHP: complex SELECT queries
As the project grows I had to work on some more complex queries to provide users with better searching facilities.
In this case, you might use two options:
- Straightforward find() function from App::Model (where you’ll have to handle the outpu of data yourself, and trying to fit the search results in your websites layout)
- Use Pagination functionality (which is designed for handling big chunks of data for you)
A simple example: from a small search menu, I need to get the data about item’s price, its type etc, so at this point, find() solution would look like:
$condition = array('OR' => array(
'Item.type' => $this->data['Item']['type'],
'Item.qty' => $this->data['Item']['qty'],
),
array('Item.price BETWEEN ? AND ?' => array($start_price,$end_price))
);
$this->('results', $this->Item->find('all', $condition));
Once you set results array in the view template, it will cause you few hours on how to rearrange data presentation, meanwhile you can use Pagination:
/*
* I'm going to use the same $condition
* the difference will be at the view level and the way of setting the data "results"
*/
$this->set('results', $this->paginate('Item', $condition);
And in the view you might add some code like:
echo $paginator->counter(array(
'format' => __('%page% of %pages%, showing %current%
records out of %count% total, starting on record %start%, ending on %end%', true)
));
foreach($results as $i => $item):
/*
* Items output
*/
endforeach;
?>
prev('<< '.__('Previous Page', true),
array(), null, array('class'=>'disabled'));?>
| numbers();?>
next(__('Next Page', true).' >>',
array(), null, array('class'=>'disabled'));?>
Last lines of the code would manage the results listing for you, which has to be defined in your Controller:
var $paginate = array(
'Item' => array(
'limit' => 5,
'order'=> array('Item.added' => 'ASC')
)
)
Done, now you can easily handle your search outputs.
CakePHP: lists, beautiful bits
These tiny bits of beauty really make the development enjoyable:
$names = $this->find('list', array(
'conditions'=> null,
'order' => 'Developer.id ASC',
'fields' => array('Developer.id','Developer.'.$name.''),
'recursive' => 0
));
as the result, getting:
Array = (
[1] => 'Pafilia',
[2] => 'Vashiotis'
);
I’ll miss these features if I move from this framework