Archive for the ‘Linux’ Category

CakePHP: complex SELECT queries

Wednesday, September 3rd, 2008

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, will cause you few hours on how to rearrange data presentation, meanwhile you can use :

/*
* 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;
?>
<?php echo $paginator->prev(‘<< ‘.__(‘Previous Page’, true),
array(), null, array(‘class’=>‘disabled’));?>
 |     <?php echo $paginator->numbers();?>
    <?php echo $paginator->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. ;)

Speed up your work in Vim

Friday, May 9th, 2008

If you don’t know what’s , then:

  1. You’re Windows IDE’s fan
  2. You’ve never used

An option “you’re using Emacs” is not included - you would have definetely known . You’d hate for the sake of “holly wars”.

I’m not an Emacs lover, neither those huge IDE’s with bunch of boxes, bookmarks, you name . Most of these things are useless when you need to code something fast, so I just stuck with : simple, light, fast, and have a lot of functionality. Here are just few of speeding up coding process I’ve been using lately:

If you got some repetitive function calls or anything you’re not bothered to print just fire up:

Comments:
:ab #cb /********************************
:ab #ce /*******************************/

Printing

#cb

or

#ce

will paste initial comment pattern.

:ab #r require_once();
:ab #i  include_once();

Minus another dozen of lines to be typed.

Text search:

You can

:set incsearch

which will enable incremental search for you, or use regexp, like:

:/\<agent\>/

will find you “agent” in current file.

Using ftp:

cmap ,r :Nread ftp://ftpdomain/public_html/index.html
cmap ,w :Nwrite ftp://ftpdomain/public_html/index.html

These are just basics, Bram Moolenaar, the developer of , got a good article on how to optimize your work with Vim, and David Rayner with his page of Vim tips. Compulsory to learn for beginners!