As Widly Inaccurate states, CodeIgniter is a wonderful PHP framework which allows rapid application development. CodeIgniter lacks a base DBLayer and ORM and makes working with multiple databases that need CRUD capabilities a pain; that’s where Doctrine comes in.
Instead of writing a comprehensive guide showing how to setup Doctrine 2 and CodeIgniter 2 together, I’m going to provide a breakdown and quick-start ZIP for those who are already aware of how to use both frameworks. A little bit of explaination, some SQL insertions, and a ZIP with well commented code later.. you’ll have Doctrine 2 and CodeIgniter 2 up and running in no-time. If you need complete breakdown explanation on how to setup CodeIgniter 2 and Doctrine 2, visit Widly Inaccurate for more details.
What’s in the ZIP?
- Unzip the “DoctrineIgnited-TLS-Web-Solutions-v1.0.zip“.
- Copy the contents of the ZIP onto your server (remote or local)
- We need to create the database and a test record. Create a database and execute the following SQL.
DROP TABLE IF EXISTS `user`; CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(32) DEFAULT NULL, `password` varchar(64) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `user_username_uniq` (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;INSERT INTO `user` (`id`, `username`, `password`) VALUES (7, 'Bob', NULL);
- Modify “application/config/database.php” to match your database connection information. After updating the file, browse to “http://domain.com/home/test/” to see if you’ve got everything right. If all goes well, you should see the word “Bob” appear on the screen. That means Doctrine found your db table, found the entry, updated the entry’s username, and retrieved the name again for displaying in CodeIgniter view. Classy.If something went wrong or Doctrine cannot find the “user” table, you’ll see an error message similiar to this…
(click to enlarge)
- That’s it! You should be up and running. However, I do recommend you look at the next section to see a break down of how everything works.
Your website should have the “application, system, .htaccess, index.php, and license.txt” files on the root.
View the website and you should see “My Test message” come up. The default controller/action URL is “home/index/”.
Downloaded 3677 times.
How does it all work?
- Doctrine 2 was installed to the “application/libraries” library and was setup to use “annotations” or PHP comments to interpret how our model’s data structure is set up.
- A “User” model was created in “application/models/User.php” which annotes the database table “user” is setup through PHP comments and also has a few manually created setter and getter methods (eg: $user->getUsername()).
- The “application/config/autoload.php” was modified to autoload the “doctrine” and “session” libraries, url and form libraries (in case you want to get fancy in your views for testing).
- The “/.htaccess” and “application/.htaccess” were both modified in preparation for a “index.php”less URL. It’s just annoying and most Linux servers have mod_rewrite setup.
- The “application/config/config.php” has the “index_page” set to blank, enable_hooks turned on, encryption_key set, global_xss_filtering turned on.
- The “application/config/database.php” has the database connection information changed to match the right database connectivity.
- The “application/config/hooks.php” as the “UhOh!” error reporting added to it.
- The “application/config/routes.php” has the default controller set to “home”.
- The “application/controllers/home.php” has a few base functions and a bunch of commenting to help guide you in making magic with Doctrine 2.
- The “MY_Exceptions.php” and “MY_Controller.php” were both added to “application/core/”. MY_Exceptions extends CodeIgniter’s error reporting and MY_Controller extends CodeIgniter’s base Controller class to include a public Doctrine Entity Manager variable.
- A few files were added to “application/errors/” to spice up the error reporting HTML output thanks to UhOh!.
- The “application/hooks/uhoh.php” was added, to well, hook onto the core Exceptions.
- The “application/libraries/Doctrine/” and “application/libraries/Doctrine.php” were both added and setup and is required for Doctrine 2 to run. Doctrine.php is where most of the Doctrine setting up and configuring will take place. It inherits the DB connectivity from the “database.php” config file.
- A base view called “home.php” was added along with some style and script assets to “application/views” thanks to HTML5 Boilerplate. Those guys rock.
Still getting errors?
Make sure you’ve got all of the following installed or enabled on your server.
- PHP 5.3+
- Apache Rewrite module enabled
- PHP short_open_tag enabled
Downloaded 3677 times.