What is new in Symfony 1.3 / 1.4

At the end of this month it goes out the last release of Symfony and here we will review what are the new news. If you have already used 1.2 version you will notice a couple of improvements have been added.

Between the new functionalities, the most highlighted are:

Mailer

Symfony 1.3/1.4 comes with a new integrated mailer based on SwiftMailer 4.1. And, to send an email is as easy as we show below using the composeAndSend() method:

1
2
3
4
5
6
<?php
public function executeSendEmail()
{
    $this->getMailer()->composeAndSend('from@example.com', 'to@example.com', 'Subject', 'Body');
}
?>

If you need more flexibility and you need to attach a file, you can use the compose() method and then send it:

1
2
3
4
5
6
7
8
9
10
<?php
public function executeSendEmail()
{
    $message = $this->getMailer()
        ->compose('from@example.com', 'to@example.com', 'Subject', 'Body')
        ->attach(Swift_Attachment::fromPath('/path/to/a/file.zip'));
   
    $this->getMailer()->send($message);
}
?>

Swift Mailer is a great and powerful library, and if you want to know more about it you can check its documentation.

Security

In these versions when you use the generate:app command, the security is already actived by default. The escaping_strategy value is already activated by default, but you can deactivate it with the –escaping-strategy option. Also, for the csrf_secret option, the framework already comes with a random generated keyword and you can change it in the setting.yml configuration file.

Widgets

Labels by default: When a label is auto-generated from the field name, the label is defined without the _id suffix (if it has it). For instance, for the client_id field name, the generated label will be ‘client’ now (it was client_id before).

sfWidgetFormInputText: Now the sfWidgetFormInput class is abstract, and the text input fields should be created with sfWidgetFormInputText.

Forms

sfForm::useFileds(): This new method remove all the form fields, except those passed as argument. This ease the fields visualization in the form view without the need of make an unset() of the unwanted fields. For instance, when we add a new field in the base form, this one do not appear automatically in the form view at least we add it explicitly as an argument of this method:

1
2
3
4
5
6
7
8
9
<?php
class ArticleForm extends BaseArticleForm
{
    public function configure()
    {
        $this->useFields(array('title', 'content'));
    }
}
?>

By default, the array is used to change the fields order; we can pass a second argument as a false value to deactivate this automatic sort.

sfForm::getEmbeddedForm($name): Now we can access a particular embedded form using the getEmbeddedForm() method.

Autoloaders

All the Symfony’s autoloaders are case-insensitive now, since PHP is case-insensitive as well.

Default ORM

Symfony 1.3/1.4 comes with Doctrine as its default ORM.
If we want to use Propel as ORM, we should use the –orm=Propel option:

1
:$ php symfony generate:project foo --orm=Propel

And if we want to do not use Doctrine neither Propel, we can use the –orm=none option:

1
:$ php symfony generate:project foo --orm=none

These are the most highlighted features to me :D. There are a lot more improvements related to widgets, validators, routing, and much more CLI task as well.

MySQL: Backup and Restoring

In this post we will see how to make a MySQL backup and how to restore it.

If you are a web developer and even more if you programm with PHP surely you know that the most used database engine is MySQL. Of course, as a good developer you should know that is a golden rule to make and keep backups of your databases. Here we’ll see how to do it easily.

Back up from command line (using mysqldump)

We can backup our database using the mysqldump command, which connects with the database and generates a SQL dump file. This file has all the needed SQL queries to restore the database:

1
user@unix:~$ mysqldump --opt -u[uname] -p[pass] [dbname] > [backupfile.sql]

Where [uname] is the MySQL username, [pass] the user’s password, [dbname] is the database name, [backupfile.sql] is the output file name, and [–opt] are mysqldump options.

For instance, if we want to backup a database named ‘myBlog’ with a ‘root’ as username and the ‘mypass’ passowrd, and store the backup in a file called ‘myBlog_backup.sql’, we should run:

1
user@unix:~$ mysqldump -uroot -pmypass myBlog > myBlog_backup.sql

With mysqldump we can specify which tables we want to backup, for instance if we want to backup the ‘users’ and ‘posts’ tables, we should run:

1
user@unix:~$ mysqldump -uroot -pmypass myBlog users posts > myBlog2_backup.sql

Furthermore if we need to backup more than one database we can use the –databases option:

1
user@unix:~$ mysqldump -uroot -pmypass --databases myBlog car_shop clients > manydb_backup.sql

And also if we want to backup all the databases in MySQL we can use the –all-databases option:

1
user@unix:~$ mysqldump -uroot -pmypass --all-databases > alldb_backup.sql

Other utils options for mysqldump are:
–add-drop-table: Adds a DROP TABLE sentence before of each CREATE TABLE sentence in the dump.
–no-data: Backs up only the database structure with no data.
–add-locks: Adds the LOCK TABLES and UNLOCK TABLES sentences.

Back up whith compression

If our database is huge enough, we could want to compress the output with the following command:

1
user@unix:~$ mysqldump -u[uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]

And to uncompress:

1
user@unix:~$ gunzip [backupfile.sql.gz]

Restoring the databases

Above we backed up a database called myBlog in a myBlog_backup.sql file. To restore it we should run the below commands:

1
user@unix:~$ mysql -u[uname] -p[pass] [db_to_restore] < [myBlog_backup.sql]

If we have compressed the dump:

1
user@unix:~$ gunzip < [backupfile.sql.gz] | mysql -u[uname] -p[pass] [dbname]

If we need to restore an existing database, we should run the mysqlimport command:

1
user@unix:~$ mysqlimport -u[uname] -p[pass] [dbname] [backupfile.sql]

That is. I hope it’s helful.