Sending an email to more than one address using the Email Component
Feb0
It took me some time to figure out how to send an email to more than one email address using the CakePHP Email Component. I’ll share my findings, maybe this will save somebodys time.
According to the cookbook the “to” variable only accepts a string, which is different to the “cc” and “bcc” variable. Both accept an array. To send an email to more than one address you have to sepearate the email addresses using a comma, which is pretty annoying as it is not documented.
I’ll open a ticket in Lighthouse to make the “to” variable also accept an array.
Update: I opened a new Lighhouse ticket and also made a contribution to the cookbook to document this behavior.
How to set the layout from a view
Dec0
A few minutes ago I came across a blog post by Jeremy Burns where he describes how to render different layouts depending on the controller. Although his solution is correct, there is an even better solution that follows the MVC concept.
You can set the layout inside your view. All you have to do is adding the following code:
<?php
$this->layout = 'your_layout';
?>
It’s easy, isn’t it?
File upload validation
Jul0
Data validation is one of the easiest things in CakePHP as long as you can use the build-in validation rules. But at the moment there is no build-in rule to validate uploaded files. Today I will show you how to implement a custom validation rule and validate uploads.
Step 1: The custom validation function
To make the validation function available to all your models, you have to put it into your AppModel.
The function is designed to accept jpg, gif and png file extensions, but you can easily add other extensions in your model. Also the user is not forced to upload a file.
Filename: app/app_model.php
<?php
class AppModel extends Model {
function validFile($check, $settings) {
$_default = array(
'allowEmpty' => true,
'extensions' => array(
0 => 'jpg',
1 => 'jpeg',
2 => 'gif',
4 => 'png'
)
);
$_settings = array_merge(
$_default,
ife(
is_array($settings),
$settings,
array()
)
);
// Remove first level of Array
$_check = array_shift($check);
if($_settings['allowEmpty'] == false && $_check['size'] == 0) {
return true;
}
// No file uploaded.
if($_settings['allowEmpty'] && $_check['size'] == 0) {
return false;
}
// Check for Basic PHP file errors.
if($_check['error'] !== 0) {
return false;
}
// Use PHPs own file validation method.
if(is_uploaded_file($_check['tmp_name']) == false) {
return false;
}
// Use CakePHPs build-in rule to validate the extension
return Validation::extension(
$_check,
$_settings['extensions']
);
}
}
?>
Step 2: Create the validation rule in your model
Now we are ready to use the rule in our model. As an example we will validate the user picture in our User model. We will only accept jpg files and force the user to upload an image.
Filename: app/models/user.php
<?php
class User extends AppModel {
var $name = 'User';
var $validate = array(
'user_picture' => array(
'rule' => array(
'validFile',
array(
'allowEmpty' => false,
'extensions' => array(
0 => 'jpg',
1 => 'jpeg'
)
)
),
'error' => 'You have to upload a jpg file.'
)
);
}
?>
Step 3:
After implementing the validation rule we are ready to build the code to move the uploaded file to the corresponding folder. But I will leave this up to you. ;-)
Changing a MySQL database from latin1 to utf8
Jun0
Yesterday I found a solution on the german site administrator.de to quickly change a whole MySQL database from latin1 (latin1_general_ci) to utf8 (utf8_general_ci).
ALTER DATABASE `database` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE `table1` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE `table2` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE `table3` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
After running the above SQL script, the whole database including all (text) fields are converted to utf8.
Adding a TinyMCE image browser the CakePHP way
May2
My first article at the Bakery is online now and available for everybody: Adding a TinyMCE imagebrowser the CakePHP way. I first needed to make my article a little bit more compliant to the CakePHP conventions, but then the article was approved.
As promised I made a small demo available for download. Just post a comment in case you need some help with the demo.
First article at the Bakery published
May0
Yesterday I published my first CakePHP tutorial at the Bakery. The article is called Adding a TinyMCE image browser the CakePHP way and is about, well … adding an image browser to your TinyMCE installation. :-)
Before my article is available to the CakePHP community, someone from the CakePHP team has to review the article and then approve it. I don’t know how long it takes, but as I’m a little bit excited, I hope that the approval is done in the next few days.
Shortly after the approval is done, I’ll build a tiny demo and make it available for download on my blog.
Hello again!
May0
After not posting anything to my blog for nearly one year, I decided to make a cut, redesign braindead1.de and start blogging again.
Mainly my new posts will be about my experiences with CakePHP. I hope that some of my posts will be of such a value to anybody as other postings of the CakePHP community are to me. That’s the reason, why I start to blog in English. So please be patient with me, if my English is sometimes a little bit broken. :-)