Hello and welcome to another King23 article.
If you have looked at the current state (v0.0.3) of King23, you might have noticed that while it basically follows the MVC pattern, it does currently not come with any specific database layer to build the model. However, while writing this tutorial i have added a few things to help you to get King23 running with Doctrine.
Currently there are several interesting approaches on how to handle your model, be it using an object or document based database system (MongoDB, CouchDB…), or one of the widely known ORM for PHP (Propel, Doctrine..), however which ideally to pick often depends on the requirements of the application that needs to be build.
Personally i use several of those methods with King23 (and others with other Frameworks),
and sometimes i even use hand written classes (nortern-crusade.com for example has a hand-build model).
Doctrine is one of the more powerfull ORM, comming with an own query language (dql), the ability to talk to several database systems, build in caching, powerfull code generators etc.
If you are familliar with the Symfony framework, you might know that Doctrine replaced the older ORM Propel there.
Either way, there are alot of reasons why you would want to use a ORM like Doctrine, and if you are unsure if this is interesting for you (or not), then use your google-fu to find more information, its beyond the scope of this article to argue about the pro and cons of a ORM.
This article assumes that you have a king23 installation, PHP5.2.3+ and Doctrine in version 1.2 (2.0 requires PHP 5.3, and works differently, so if you decide to go for 2.0, you are on your own!)
Lets get started..
prepare our project
obviously the first thing that we do is to use king23 to setup a project for us,
in this example “myapp”:
gareth:/opt/www# king23 King23:create_project myapp [2010-06-08 19:16:45] King23 (Version: 0.0.2) project creation [2010-06-08 19:16:45] copying project template to myapp [2010-06-08 19:16:46] creating symlink myapp/lib/King23 to point to /opt/king23 [2010-06-08 19:16:46] Setting rights in myapp/template_c/ [2010-06-08 19:16:46] Project: myapp created
there are several ways of obtaining Doctrine, and its up to you to pick the one that suits you most. The Doctrine project recommends the use of svn / svn:externals to get the source, but if that really suits you depends alot on if you use svn or not. Another way of installing Doctrine would be to simply use the pear installer, but personally i want to keep the application self contained, so no librarys in other pathes wanted.
For this tutorial i have decided to use a svn export, since it will give me a clean library folder without any svn folders. Also im going to use the version tagged as 1.2.2.
Probably you want to use a different way (for example a svn checkout instead of export, so its easier for you to update, or you do want the tests, the sandbox or the docs left out – either way, check the doctrine documentation for more information).
gareth:/opt/www/# cd myapp/lib gareth:/opt/www/myapp/lib# svn export http://svn.doctrine-project.org/tags/1.2.2/ Doctrine
Now i have a Doctrine distribution in my library folder.
The next step is to configure doctrine, so we can use the cli task that (now) comes with King23. The Project template should have put a doctrine.php in your conf folder, configuring is as easy as editing that one, and then open the config.php, go to the bottom and make sure the doctrine.php is included.
Doctrine comes with a command line interface, that will do stuff like (re)build your model, work with migrations etc (check out the Doctrine Manual for more information). If you have done everything right, then you should now be able to run:
And you should be rewarded with an output like this:
[2010-06-13 11:19:17] King23 Doctrine CLI wrapper Doctrine Command Line Interface king23 King23:doctrine compile king23 King23:doctrine dql king23 King23:doctrine generate-migrations-models king23 King23:doctrine rebuild-db king23 King23:doctrine generate-migrations-db king23 King23:doctrine create-tables king23 King23:doctrine dump-data king23 King23:doctrine generate-migration king23 King23:doctrine generate-models-db king23 King23:doctrine load-data king23 King23:doctrine create-db king23 King23:doctrine generate-sql king23 King23:doctrine drop-db king23 King23:doctrine build-all-reload king23 King23:doctrine generate-migrations-diff king23 King23:doctrine generate-models-yaml king23 King23:doctrine generate-yaml-models king23 King23:doctrine generate-yaml-db king23 King23:doctrine build-all-load king23 King23:doctrine build-all king23 King23:doctrine migrate
to learn more about doctrine / the doctrine cli, check out the doctrine website.