07/12/2008
» Jobeet: An alternative tutorial for Day 3
Par / by COil,
07/12/2008 / 23:00 :: Tags :: dbdesigner4
| jobeet
| model
| mysql
| mysql workbench
| plugin
| propel
| symfony
Hi symfonians !
Well, i didn't post very often during these last months. So let's try to write something interesting. In this post i will show you an alternative way to build your database with symfony, propel and the DB4 tool. This tutorial is not really an "alternative" to the official tutorial.
Let's say that we will just see another way to build our database, i won't be as exhaustive as Fabien did in the Jobeet Day 3 tutorial, therefore, for all details please refer to the official Jobeet tutorial.
Pre-requisites:
- Having done at least day 1 and 2 of the Jobeet tutorial. (or you already know symfony, in this case you will need a fresh symfony 1.2 project)
- Not being too angry with Propel

What you will learn:
- Using the sfDB4toPropelPlugin to build your schema.yml without writing a single line of this file.
What you will NOT learn:
- How wonderful symfony is. For this, read the official Jobeet tutorial of Fabien.

So let's go... We will start at this part of the Jobeet 3 tutorial.

1 - Plugin installation
First we will install the sfDB4toPropel plugin, i did not released any package for the symfony 1.2 version yet, so we will install it via SVN. (edit: i've just released the 1.0.2 version for sf 1.2.0)
$ cd plugins $ mkdir sfDB4toPropelPlugin $ cd sfDB4toPropelPlugin $ svn co http://svn.symfony-project.com/plugins/sfDB4toPropelPlugin/branches/1.2
Be careful not to check out the root of the plugin but the branch related to the symfony version you want to use, actually 1.1 or 1.2)
(PS: You can also set an svn external to the plugin if your project already uses a svn repository)
You plugin folder should now look at this now:

2 - Creating the database
2.1 - Create your database: (using UTF8) (or with PhpMyadmin)
$ mysqladmin -uroot -pmYsEcret create jobeet
2.2 - Set up the database settings:
$ php symfony configure:database "mysql:host=localhost;dbname=jobeet" root mYsEcret
(you can leave the password blank)
3 - Using the DB4 tool
3.1 - Install it, check out the instructions on the DB4 website:
Now we are ready to design our database.
3.2 - Updating the database name
1st we need to change the name of the connection that will be used by symfony. The default database name is propel, to use this setting, in DB4 -> main menu -> options -> model option, in this screen put propel for the model name
option.
3.3 - Creating the tables
Add a table using the following button (the one at the top)

And name this table 'jobs' (we will use plural for tables names but singular for the phpNames like in the official tutorial). Now enter the list of the fields, note that you can:
- Tell if the field is an auto-increment one
- Put the default value of the field
- Add a comment to describe the field
Now let's modify the phpName of the table, fot this, just enter the phpName in the comment field of the table: JobeetJob (if you leave this field blank the default phpName will be used)
Save into the /doc/database/db4.xml file (relative to the root of your project), this is the default path that will use the plugin, so let's keep this path for now.
Your jobs
table should look like this now:

Note the phpName entered at the bottom.
3.4 - Others tables
Now repeat the operation for the category and affiliate tables. (don't forget the phpName of each table) Your schema should look like this now:

3.5 - Setting the relations between tables
Now that we have our 3 main tables, it's time to set up the relations. (buttons on the left)
- Create a 1-n relation from the category table to the job one
- Create a n-m relation between the category and affiliate tables
- Double click on the relation between job and category and rename the field to category_id (you can also put a comment and set the onUpdate and onDelete instructions)
- Rename the relations of the categories_affiliates table to
category_id
andaffiliate_id
Your schema should look like this now:

That's it, finally we have our DB4 schema.
Now let's see how to use it with symfony.
3.6 Generating our schema.yml
Now that we have our db4.xml file we want to generate our schema.yml that will be used by symfony and propel (that's the goal of this tutorial)
Launch the following script (chmod +x the file before)
$ ./plugins/sfDB4toPropelPlugin/bin/db4.sh
In fact this batch will help us re-generating all that we need after a modification on the DB4 schema. But it just does the following task:
- Converts the db4.xml file into a schema.yml file
- Launch the propel:build-all-load command
- Clears the cache
Now open your /config/schema.yml file it should look like the one provided by Fabien but there are some differences:
- There is an additional section, in the one of fabien, it is not written because the default values are used.
propel:
_attributes:
defaultIdMethod: native
package: lib.model
- If you have put comments in your fields, then you also have them in you schema.yml (usefull to quicky remember the use of a field)
is_public: { type: BOOLEAN, required: true, default: '1', description: 'Tells if the job is public or not' }
- The id declaration for our 3 main tables is not the same, once again it's a symfony convention, the ~ tells that we will use default settings.
- Physical tables names are not the same, that's true but that's not a problem, because in your application we will always use the phpNames of the tables and not is physical names.
- OMG ! No.. There is a big mistake, we don't have unique index on the email field of the Affiliate table and neither on the token field of the Job table, let's see how to correct this.
3.7 - Setting up the index
Open the Job table details with DB4, in the Indices section at the bottom, click on the add button, name your index 'jobs_unique_token' for example. Now drag and drop the token field to the columns section at the bottom right. Save. Launch our db4 batch. If you open your schema.yml you will see a new section for the Job table:
_uniques: { jobs_unique_token: [token] }
Now repeat the operation for Affiliate email field. Launch the script. Check the results.
4 - Conclusion
As we have seen, with the help of this plugin we were able to create a clean schema.yml file for our project without having to write a single line of code. I am using this plugin for almost 2 years (the 1.0 version was never published), it just works well, especially when you start a new project from scratch. What is great is even if you want to do major modifications on your database at an early stage of your development, you can do it and very quickly. You will save a lot of time.
For the next version, i'd like to see what is possible to do with MySQL Workbench, i did not tested it yet for now but it looks really nice. (the creator of DB4 is now in the MySQL Workbench team) Moreover it would be also nice to be able to do the same thing for Doctrine... If anyone wants to help, your are welcome.
Related posts:
- New symfony 1.1 plugin + tutorial : sfDB4toPropelPlugin
- Jobeet - Day 1: Starting up the Project
- Jobeet - Day 2: The Project
- Jobeet - Day 3: The Data Model
- Jobeet - Day 6: More with the Model
PS: I have published this tutorial quiet quickly, please help me by reporting typos and errors. 



Donc si vous avez des propositions de modifications à n'hésitez pas, je peux les regrouper et envoyer la mise à jour à 

