Archive for the ‘Programming’ Category

If you get this error:
1Warning: Warning (2): preg_match() [<a href=’function.preg-match’>function.preg-match</a>]: Delimiter must not be alphanumeric or backslash in [/var/www/vhosts/h2oservices.com/httpdocs/cake/libs/model/model.php, line 2611]
When trying to use a custom validation rule, make sure the method visibility is set to public.
So this should work:
123public function myCustomValidateRule(){
    // Custom validation code
}
It seems protected visibility works on some versions of [...]

Ternary in PHP Heredoc

15, Dec 2011

It can’t be done using the normal Ternary operator but you can emulate it with:
12345678910111213// So instead of:

$str = <<<HTML
{$a ? 1 : 2}
HTML;

// You can do:

$values = array(’1′, ‘2′);

$str = <<<HTML
{$values[$a]}
HTML;

If you have fields that don’t exist in your table schema and you want to submit those to an action that is protected by the Security component. You can tell the component to ignore (and therefore not black hole/404 your request) the field(s) using its $disabledFields attribute.
In this example I’m ignoring the security_code (captcha) field, [...]

You can save has and belongs to many models in CakePHP (v1.3.4) whilst adding additional data to the join table, in a single operation.
In this example I’m saving Users that HABTM Videos. The users_videos join table has an order column.
Firstly add the ‘with’ parameter to the join, you can add the ‘order’ parameter too [...]

I had a couple of CKEditors in a jQuery UI dialog box today. The dialog box contained an iFrame with the actual CKEditors inside that.
On the odd occasion when the modal box opened (tested in FireFox 3.6 on the Mac and PC) the vertical scroll position of the iFrame would not be at the top. [...]

I’m in the situation where most of my CMS components are extensible CakePHP plugs.
I’ve just started to use the CakeDC migrations plugin (been meaning to for a long time as I always loved Rails migrations).
I had previously automated the insertion of plugin ACO node records using a Bash script that chugged through a stack [...]

If you’re using the CakePHP Containable behavio(u)r and trying to query with conditions across a Has and Belongs To Many association, you need to specify you wish to contain the dynamically bound model records, before you do the actual find.
Here’s one example from the book that doesn’t work with Containable:
123456$this->Recipe->bindModel(array(’hasOne’ => array(’RecipesTag’)));

$this->Recipe->find(’all’, array(
    ‘fields’ [...]

You can stop nested elements triggering their parent element event handlers (DOM bubbling) using this handy jQuery method:
http://api.jquery.com/event.stopImmediatePropagation/
I ran into this issue whilst working on a calendar/scheduling app. Where day containing elements needed click events and their child elements needed click events too.
Love the jQuery ;]

I was experiencing this issue with some larger file uploads using the CakePHP Media plugin.
https://bugs.webkit.org/show_bug.cgi?id=5760
I added this to the virtual host declaration in my Apache config file to fix it:
123456<VirtualHost x.x.x.x:x>

    # Prevent Safari from hanging on uploads
    BrowserMatch  Safari  nokeepalive

</VirtualHost>

Especially if you’re trying to match attributes and their values!
Thankfully Kooilnc, the godsend has a solution:
http://stackoverflow.com/questions/1231770/innerhtml-removes-attribute-quotes-in-internet-explorer
I’ve wrapped that up into a jQuery function here:
123456789101112131415161718192021222324$.fn.ieInnerHTML = function() {
  var zz = this.html(),
       z =
     zz.match(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|’.*?’|[^'">\s]+))?)+\s*|\s*)\/?>/g);
    if (z){
      for (var i=0;i<z.length;i++){
        var y, zSaved = z[i];
  [...]


top