I have worked with Propel in the past and it has helped me generate the ORM layer for applications effortlessly. I know that CakePHP has its own ORM layer. However, upon evaluating my options, I have considered not to use the Cake ORM layer. Here is my justification.
I am migrating an existing application to CakePHP. Existing database tables and fields do not match CakePHP’s database conventions. If I modify my tables and fields, all code accessing the database would need to be modified and re-tested. There is quite a bit of code in the current application’s libraries and cron jobs that I don’t have the luxury to re-write. While I am considering moving the application to CakePHP, I plan on using Propel to provide the ORM layer. In the future, I could consider migrating to CakePHP ORM.
CakePHP does not support composite primary keys. Moreover, I like my model objects to have explicit getters and setters to facilitate my IDE’s code completion. Don’t get me wrong, I love associative arrays. However, not for defining my object models.
Here is how I integrated Propel 1.5.6 with CakePHP 1.3.8.
1) Create a folder called “propel” under “vendor”.
2) Place the propel runtime files under the newly created “propel” folder. As far as I can tell, the only folder needed from propel generator is the “lib” folder.
3) Place the generated Propel configuration files “conf.php” and “classmap-conf.php” under your Cake’s “config” folder.
4) If you wish to have database configuration live in one place, then perform the highlighted modifications below to propel’s “conf.php” file. That way you do not need to modify database settings in 2 places. Here is what my conf.php file looks like:
<?php // This file generated by Propel 1.5.6 convert-conf target include_once 'database.php'; $dbConfig = new DATABASE_CONFIG(); $conf = array ( 'datasources' => array ( 'iv' => array ( 'adapter' => 'mysql', 'connection' => array ( 'dsn' => 'mysql:host=localhost;dbname=' . $dbConfig->default['database'], 'user' => $dbConfig->default['login'], 'password' => $dbConfig->default['password'], ), ), 'default' => 'iv', ), 'generator_version' => '1.5.6', ); $conf['classmap'] = include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'classmap-conf.php'); return $conf;
5) Now place all you generated model files under the “model” folder.
If you plan to use Cake model files along-side Propel model files, then I would suggest generating Propel models file with a package name specified. That way, propel model files will live in a separate folder inside the Cake model folder.
If the databases table name are not following cakePHP conventions, then specify the table with “useTable” as follows:
var $useTable = "mySpecialTable";
6) Lastly, load Propel in your cake’s bootstrap.php as follows:
// Include the main Propel script App::import('Vendor', 'Propel', array('file' => 'propel' . DS . 'lib' . DS . 'Propel.php')); // Initialize Propel with the runtime configuration Propel::init("config/iv-conf.php"); // Add the generated 'classes' directory to the include path set_include_path(ROOT . DS . APP_DIR . DS . "models" . DS . PATH_SEPARATOR . get_include_path());
Hope this helps anyone who is planning on using Propel with CakePHP.