If you have an array that represents the days in a month, ready to populate a HTML table, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Array
(
    [0] =>  
    [1] =>  
    [2] => 1
    [3] => 2
    [4] => 3
    [5] => 4
    [6] => 5
    [7] => 6
    [8] => 7
    [9] => 8
    [10] => 9
    [11] => 10
    [12] => 11
    [13] => 12
    [14] => 13
    [15] => 14
    [16] => 15
    [17] => 16
    [18] => 17
    [19] => 18
    [20] => 19
    [21] => 20
    [22] => 21
    [23] => 22
    [24] => 23
    [25] => 24
    [26] => 25
    [27] => 26
    [28] => 27
    [29] => 28
    [30] => 29
    [31] =>  
    [32] =>  
    [33] =>  
    [34] =>  
)

This is a nice way IMO to render (in a view/template) the days markup inside the tbody tag:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<? $daysInWeek = 7 ?>

<? foreach($days as $key => $day): ?>
    <? $rows[$key % $daysInWeek][] = !empty($day) ? $day : '&nbsp;'  ?>
<? endforeach ?>

<tbody>

    <? foreach($rows[0] as $k1 => $day ): ?>
        <tr>
            <? foreach($rows as $k2 => $r): ?>
                <td><?= $rows[$k2][$k1 % $daysInWeek] ?></td>
            <? endforeach ?>
        </tr>
    <? endforeach ?>

</tbody>

If you get this error:

1
Warning: 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:

1
2
3
public function myCustomValidateRule(){
    // Custom validation code
}

It seems protected visibility works on some versions of PHP (5.2.11 tested and working) and not others.

It can’t be done using the normal Ternary operator but you can emulate it with:

1
2
3
4
5
6
7
8
9
10
11
12
13
// So instead of:

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

// You can do:

$values = array('1', '2');

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

Don’t forget kids to check the size of the tmp partition if you are having issues uploading large files with PHP.

After setting all the usual configuration directives (upload_max_filesize, post_max_size etc) uploads were still failing for me returning error code 7.

After poking around on the Debian VPS for a while I realised the /tmp partition was 128mb!!

Luckily you can set the path for file uploads using the following directive:

1
upload_tmp_dir = /path/to/somewhere/with/some/disk/space

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, which obviously I don’t store anywhere in the database table:

1
2
3
$this->Security->disabledFields = array(
  'Enquiry.security_code'
);

The proper way to do this is with a static call to Debugger:

1
Debugger::log($output);

For my own records. Batch file rename using shell:

1
2
3
Rename all jpg files that contain the string 19_ and replace it with 28_

for file in *.jpg ; do mv $file `echo $file | sed 's/19_/28_/g'` ; done

Just managed to count another table using a model virtual field, obviously preserving the association with the sub query conditions.

So in my ProductTemplate model I have this:

1
2
3
var $virtualFields = array(
    'page_count' => 'SELECT COUNT(*) FROM pages as Page WHERE Page.product_template_id = ProductTemplate.id'
  );

The nice thing about this is you can use the field with the CakePHP Model::find ordering and with the pagination component.

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 if it makes sense in your context:

1
2
3
4
5
6
7
8
9
10
class User extends AppModel {
 
  public $hasAndBelongsToMany = array(
    'Video' => array(
      'with' => 'UsersVideo',
      'order' => 'UsersVideo.order'
    )
  );
 
}

Make sure you do a $model->saveAll() in the controller:

1
2
3
4
5
6
if($this->User->saveAll($this->data)){
  // Success message
  // Redirect    
} else {
  // Error message
}

The trick is in the naming of the form input elements. The format is Video.X.UsersVideo.field_name.

Here’s an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?= $form->input('Video.id') ?>

<?= $form->input('Video.0.UsersVideo.video_id', array(
  'type' => 'select',
  'options' => $videos
)) ?>
     
<?= $form->input('Video.0.UsersVideo.order', array(
  'type' => 'select',
  'options' => range(1, count($videos))
)) ?>

<?= $form->input('Video.0.UsersVideo.user_id', array(
  'type' => 'hidden',
  'value' => $user['User']['id']
)) ?>

Which outputs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[User] => Array
        (
            [id] => 3
        )
[Video] => Array
        (
            [0] => Array
                (
                    [UsersVideo] => Array
                        (
                            [video_id] => 9
                            [order] => 2
                            [user_id] => 3
                        )

                )

        )

This will add a record to the join table with the order value.

Obviously you can expand on this to save more users_videos records during the same saveAll call.

top