Professional and Affordable Web Design

0800 080 5401

Introduction to Propel

What is Propel?
When I first started producing websites with PHP, I used text files for storing user submitted content. It wasn't very sophisticated but did work. Of course, I abandoned that after discovering databases, and used them ever since. Unfortunately using plain mysql_query() function brought new threats like SQL Injection. Forgetting to check one variable passed to SQL query could result in someone clearing out the entire database.

Database Abstraction Libraries like ADOdb or PDO fixed that problem - but than others appeared.

PHP was primarily an easy scripting language for websites, but pretty soon it evolved into a fairly powerful object oriented language. And as more and more scripts were written with use of objects and classes it became irritating to manually write set and get methods for all properties of all classes and making sure that model from PHP is well connected with database structure.

This is where Propel comes in. It's main goal is to allow coder to forget about connecting PHP with the database model. Furthermore, when you get to know Propel well, you will probably never have to use SQL ever again (except maybe for some extremely complicated queries). Once you have database structure prepared in an Propel-friendly XML format, with execution of two commands you can generate all model classes and put structure into database.

After setting it up, you can use it as following (lets assume that there is a book table in the database with fields: id, author and title):

PHP code snippet
$book = new Book( );
$book->setAuthor( 'Arthur C. Clarke' );
$book->setTitle( 'The Stars' );
$book->save();

 

The code above will create a new row in book table and set author and title. There will be more examples to follow showing how to select single and multiple rows from table, update rows, etc. From above example you can get idea what Propel is about.

Setting up the model
Before we can start using model we need to generate it from schema.xml file. Let's try with a simple example from database table we used above.

example

 

This will generate table book with two fields id, title and author. Now lets try to use a separate table for authors, and we will connect it with book:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<database name="myModel" defaultIdMethod="native">
<table name="book">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="title" type="varchar" size="255" required="true" />
<column name="author_id" type="integer" required="true" />
<foreign-key foreignTable="author">
<reference local="author_id" foreign="id"/>
</foreign-key>
</table>
<table name="author">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="name" type="varchar" size="255" required="true" />
<column name="surname" type="varchar" size="255" required="true" />
</table>
</database>

test
&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; standalone=&quot;no&quot;?&gt;
&lt;database name=&quot;myModel&quot; defaultIdMethod=&quot;native&quot;&gt;
&lt;table name=&quot;book&quot;&gt;
&lt;column name=&quot;id&quot; type=&quot;integer&quot; required=&quot;true&quot; primaryKey=&quot;true&quot; autoIncrement=&quot;true&quot;/&gt;
&lt;column name=&quot;title&quot; type=&quot;varchar&quot; size=&quot;255&quot; required=&quot;true&quot; /&gt;
&lt;column name=&quot;author_id&quot; type=&quot;integer&quot; required=&quot;true&quot; /&gt;
&lt;foreign-key foreignTable=&quot;author&quot;&gt;
&lt;reference local=&quot;author_id&quot; foreign=&quot;id&quot;/&gt;
&lt;/foreign-key&gt;
&lt;/table&gt;
&lt;table name=&quot;author&quot;&gt;
&lt;column name=&quot;id&quot; type=&quot;integer&quot; required=&quot;true&quot; primaryKey=&quot;true&quot; autoIncrement=&quot;true&quot;/&gt;
&lt;column name=&quot;name&quot; type=&quot;varchar&quot; size=&quot;255&quot; required=&quot;true&quot; /&gt;
&lt;column name=&quot;surname&quot; type=&quot;varchar&quot; size=&quot;255&quot; required=&quot;true&quot; /&gt;
&lt;/table&gt;
&lt;/database&gt;

 

Once we have our schema.xml file ready, we need to prepare configuration in the build.configuration file. You can basically connect to any database supported by PDO, as Propel uses PDO as database abstraction library. Let's use sqlite, our build.configuration will look as following:

propel.project = myProject
propel.database = sqlite
propel.database.url = sqlite:/path/to/myDatabase.db

You will learn how to connect Propel with different database types in next releases of this tutorial or you can navigate now to Propel project website. http://propel.phpdb.org/trac/

The last thing we need to do before building our model is to prepare runtime configuration. This configuration contains information about database, user, password, etc. In our example it will contain path to sqlite database file:

So our runtime-conf.xml will contain:

<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
<propel>
<datasources default="myModel">
<datasource id="myModel">
<adapter>sqlite</adapter>
<connection>
<dsn>sqlite2:/path/to/myDatabase.db</dsn>
</connection>
</datasource>
</datasources>
</propel>
</config>

Building model
To build a model all you need to do is to execute propel-gen. Optionally as a parameter you can provide path where you want Propel to be build in, f.e.
propel-gen /path/to/myModel

Once you build your model you will notice three new directories inside of build directory: conf, sql and classes. The first one contains runtime configuration, and it is basically what you inserted in runtime-conf.xml but converted into PHP arrays to speed up process of reading configuration. Sql directory contains schema.sql file that is used to create the database structure. In the classes folder you will find all PHP classes that we will use to access database.

You can execute propel-gen command as many times as you want, you should execute it always after you modified the structure in schema.xml file.

To insert structure into your database, you can use propel-gen /path/to/myModel insert-sql command, but be aware that every time you execute this command, your database is being overwritten, so any existing rows in database will be removed, if you want to keep them – back them up first.

In the next article you will find information on how to use your newly generated classes to perform basic CRUD (Create/Retrieve/Update/Delete) operations on the database without a single line of SQL.

Comments0 Comments

All comments are moderated for spam and will not be shown. All genuine comments wil be show, however the links will be based on a NO FOLLOW RULE. Repeat commenters adding value to the articles and discusions will have removed alowing Follow Rule to Work.

Make a Comment

Notify me when someone responds

Quick Contact

See Full Portfolio Some Of Our Work

Screenshot of dsbs.co.uk website
dsbs.co.uk

Driving Schools Booking Service (DSBS) is a network of driving instructors, covering the whole of the UK. For this project, we were...

Screenshot of countysecurity.co.uk website
countysecurity.co.uk

County Security is a fully featured E - commerce solution, with an integrated "system configurator", which allows users to choose...

Customer News & Resources

At Mutiny Design we are constantly gathering together articles and help guides to assist our clients.

Introduction to sitemap.xml

Checking for a sitemap A site map (or sitemap) is a list of pages of a web site accessible to crawlers or users. It can be either a document in any form used as a planning tool for web design, or a web page that lists the pages on a web site, Some developers feel that site index is a more appropriately used term to relay page function, web visitors are used to seeing each term and generally associate both as one and the same. However, a site index is often an A-Z index that provides access to content, while a site map provides a general... Read More »

Center a web page in CSS

One simple way to center a web page using CSS is to create a container div, that is horizontally centered by having its left and right margins set to auto. Using this method, you can still apply colours / background images to the body tag, so its a flexible solution. (if you didnt need this ability, just apply margins and width to the body tag instead, and forget using the container div). The container div has the same width as your webpage and, well, contains it. All the code for your web page is placed inside the container div. This will... Read More »

-