Posts Tagged ‘PHP

Quick refresher for my own records. CakePHP ACL component console commands:
1234567891011121314151617181920212223# View ACO tree
cake acl view aco

# Create root ACO container – in this instance site
cake acl create aco root site

# Create ACO controller node – in this instance the products controller
cake acl create aco site Products

# Create ACO controller action node – in [...]

This is quite handy if you’re working in a team and you don’t want the designer/client poking about in the controllers to set page titles. You can set them in the view using this code:
UPDATE thanks for bringing the v1.3 change to my attention JT.
12345v1.3
<? $this->viewVars[’title_for_layout’] = ‘My page title’ ?>

v1.2
<? $this->pageTitle = ‘My page [...]

I’ve been using the containable behavio(u)r in CakePHP along with the $recursive = -1 property in my models to cut down on the amount of SQL querying going on.
I needed to specify a second level recursion on one of my associated models and managed it by doing this:
12345$this->paginate[’Invoice’][’contain’] = array(
    ‘Order’ => ‘OrderedProduct’,
    [...]

Tried to do a schema snapshot of my db today (cake schema generate) to throw in SVN. I’d added an enum column to one of my tables and it threw this error:
1Notice: Schema generation error: invalid column type enum(’flat_fee’,'per_unit’) does not exist in DBO in /vhosts/site.com/accounts/cake/libs/model/schema.php on line 475
Enum must be too old-school and crusty [...]

This is quite a handy method for your tools class. It will split camelcase strings up nicely. You could then explode if necessary:
1234567891011121314// New version thanks to Oliver Specht

static function splitByCaps($string){
    return preg_replace(’/([a-z0-9])?([A-Z])/’,'$1 $2′,$string);
}

Tools::splitByCaps(’HelloWorld’);
# Will return ‘Hello World’

/* Old version
static function splitByCaps($string){
    return preg_replace(’/[A-Z]/’, ‘ $0′, $string);
}
*/

I managed to recompile PHP with some new directives and install it on my live production server with minimal downtime.
I followed these steps:
1) Downloaded the source of PHP. I stuck to the same branch (5.2) as there was nothing I required in 5.3.
2) Backed up my /etc/php.ini file.
3) Compiled PHP with ./configure using my old [...]

Found this today which is really handy (and simple!) if you need a leading zero on your single digit numbers:
12345$months = range(1,12);
foreach($months as $month){
    echo sprintf("%02d", $month);
}
// Will output 01,02,03,04,05,06,07,08,09,10,11,12

To reset the key values in a PHP array you can do this:
1234567891011121314151617181920212223$things = array(’a',’b',’c',’d');
unset($things[0]);
print_r($things);

#outputs
#Array
#(
#    [1] => b
#    [2] => c
#    [3] => d
#)

# Actually do the reset.
$things = array_values($things);

print_r($things);
#outputs
#Array
#(
#    [0] => b
#    [1] => c
#    [2] => d
#)

Ran into an interesting situation yesterday. I wanted to create some dynamic (overloading) object attributes (object member variables) in PHP with private visibility. Forcing encapsulation via getter methods. It seems this is not possible, as dynamic attributes are only ever public (see the PHP documentation).
There is however, as always, a work around! If you [...]

Uploaded my first site today that uses SWF Upload to handle large file uploads. The admin area of that site had basic Apache HTTP authentication via a .htaccess file.
SWF Upload failed with a 401 error after testing. It seems you can’t use this style of authentication with the current version of SWF Upload. Something to [...]


top