Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My advice to you would be to have a look at <a href="http://www.doctrine-project.org/projects/orm" rel="nofollow">Doctrine</a>, it is an object relational mapper which can intergrate quite nicely with ZF. I personally use version 1.2 with ZF 1.11.4 with no problems.</p> <p>There is a nice screen cast <a href="http://www.zendcasts.com/deep-integration-between-zend-and-doctrine-1-2/2010/01/" rel="nofollow">here</a> which explains how to intergate Doctrine into ZF. Doctrine can be a bit of a pig to to learn but once you understand it, it will save you time in the long run. Doctrine also comes with a command line script which can be used to reverse engineer databases into Doctrine classes. If you have the relations set on the database then Doctrine can pick that up too.</p> <p>I also hear that ZF 2 will have Doctrine 2.0 included.</p> <p>The method I use to create the Doctrine classes is to first setup doctrine in your appication.ini file, add these lines somewhere under production header.</p> <pre><code>[production] //... ; Doctrine settings pluginpaths.Freedom_Zend_Application_Resource = "Freedom/Zend/Application/Resource" resources.doctrine.connection_string = "mysql://username:password@localhost/database_name" resources.doctrine.compiled = false ; use compiled version of Doctrine resources.doctrine.cache = false ; use query cache ; Information required for models generator resources.doctrine.models_path = APPLICATION_PATH "/modules/default/models/Doctrine" resources.doctrine.module_directories[] = APPLICATION_PATH "/modules/default/models/Doctrine/base" resources.doctrine.module_directories[] = APPLICATION_PATH "/modules/default/models/Doctrine" ; Generator settings resources.doctrine.generate_models_options.phpDocPackage = Your App Name resources.doctrine.generate_models_options.phpDocSubpackage = Doctrine resources.doctrine.generate_models_options.phpDocName = Your Company Name resources.doctrine.generate_models_options.phpDocEmail = your@email.address resources.doctrine.generate_models_options.pearStyle = true resources.doctrine.generate_models_options.generateTableClasses = true resources.doctrine.generate_models_options.generateBaseClasses = true resources.doctrine.generate_models_options.classPrefix = "Model_Doctrine_" resources.doctrine.generate_models_options.baseClassPrefix = "Base_" resources.doctrine.generate_models_options.baseClassesDirectory = resources.doctrine.generate_models_options.classPrefixFiles = false resources.doctrine.generate_models_options.generateAccessors = false //... </code></pre> <p>You will notice a line at the top <code>pluginpaths.Freedom_Zend_Application_Resource</code> Freedom is my generic namespace in my library (see folder tree below). I here I have a Zend folder where I can place my extra ZF code, this includes overiding existing ZF functions if required. Freedom is my company name, yours will obviously differ. This line will need to change to your company's name, for example <code>pluginpaths.Yourcompany_Zend_Application_Resource = "Yourcompany/Zend/Application/Resource"</code></p> <p>The next line is where you place your database connection settings <code>resources.doctrine.connection_string = "mysql://username:password@localhost/database_name"</code></p> <p>The next three lines:</p> <pre><code>resources.doctrine.models_path = APPLICATION_PATH "/modules/default/models/Doctrine" resources.doctrine.module_directories[] = APPLICATION_PATH "/modules/default/models/Doctrine/Base" resources.doctrine.module_directories[] = APPLICATION_PATH "/modules/default/models/Doctrine" </code></pre> <p>tell Doctrine where to place the classes generated, as I am using a modular setup these go into my <code>application/modules/default/models/Doctrine</code> and <code>application/modules/default/models/Doctrine/Base</code> respectively (see folder tree below).</p> <p>There are some other lines that will need changing, these should be self evident.</p> <p>You will also need to add the following lines under the development heading in your application.ini.</p> <pre><code>[development : production] //... ; phpSettings resources.doctrine.compiled = false ; use compiled version of Doctrine resources.doctrine.cache = false ; use query cache //... </code></pre> <p>You will also need the resource file Doctrine.php. Where this is located depends on your setup, mine is as follows.</p> <pre><code>-Application -configs -layouts -modules -default // ZF default controller -controllers -models -Doctrine // folder for you generated classes -Base // Do not change the files in this folder -views -scripts -Library -Doctrine // the doctrine application as downloaded doctrine-cli.php // you will need to create this (see below) Doctrine.php // come with the doctrine application -Freedom // my generic namespace, shared across multiple apps -Zend // place to overide/add ZF classes -Application -Resource *Docrine.php // the file below -Zend // ZF 1.11.4 (yours may differ) -ZendX // ZF extras </code></pre> <p>The *Doctrine.php file is as follows (obviously ignore the *!!)</p> <pre><code>/** * Doctrine application resource * * @author Juozas Kaziukenas (juozas@juokaz.com) */ class Freedom_Zend_Application_Resource_Doctrine extends Zend_Application_Resource_ResourceAbstract { /** * Initialize */ public function init() { $doctrineConfig = $this-&gt;getOptions(); if (isset($doctrineConfig['compiled']) &amp;&amp; $doctrineConfig['compiled'] == true &amp;&amp; file_exists(APPLICATION_PATH . '/../library/Doctrine.compiled.php')) { require_once 'Doctrine.compiled.php'; } else { require_once 'Doctrine.php'; } $loader = Zend_Loader_Autoloader::getInstance(); $loader-&gt;pushAutoloader(array('Doctrine_Core', 'autoload'), 'Doctrine'); $manager = Doctrine_Manager::getInstance(); // set models to be autoloaded and not included (Doctrine_Core::MODEL_LOADING_AGGRESSIVE) $manager-&gt;setAttribute( Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE ); // enable ModelTable classes to be loaded automatically $manager-&gt;setAttribute( Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true ); // enable validation on save() $manager-&gt;setAttribute( Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL ); // enable accessor override $manager-&gt;setAttribute( Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true ); // enable sql callbacks to make SoftDelete and other behaviours work transparently $manager-&gt;setAttribute( Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true ); // enable automatic queries resource freeing $manager-&gt;setAttribute( Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true ); // connect to database $manager-&gt;openConnection($doctrineConfig['connection_string']); // set to utf8 $manager-&gt;connection()-&gt;setCharset('utf8'); if (isset($doctrineConfig['cache']) &amp;&amp; $doctrineConfig['cache'] == true) { $cacheDriver = new Doctrine_Cache_Apc(); $manager-&gt;setAttribute( Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver ); } return $manager; } } </code></pre> <p>Next you will need to create the doctrine-cli.php file to bootstrap your application this should be located in the root of your library file (see tree above), mine is as follows.</p> <pre><code>/** * Doctrine CLI script * * @author Juozas Kaziukenas (juozas@juokaz.com) */ define('APPLICATION_ENV', 'development'); define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../your/application')); set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library'), './', get_include_path(), ))); require_once 'Zend/Application.php'; // Create application, bootstrap, and run $application = new Zend_Application( APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini' ); $application-&gt;getBootstrap() -&gt;bootstrap('doctrine') -&gt;bootstrap('autoload'); // set aggressive loading to make sure migrations are working Doctrine_Manager::getInstance()-&gt;setAttribute( Doctrine::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_AGGRESSIVE ); $options = $application-&gt;getBootstrap()-&gt;getOptions(); $cli = new Doctrine_Cli($options['resources']['doctrine']); $cli-&gt;run($_SERVER['argv']); </code></pre> <p>The only line you should need to change is</p> <pre><code>define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../your/application')); </code></pre> <p>so it points to your application's root folder, similar to the line in your index.php file in your public folder.</p> <p>Now hopefully you should be ready to generate your database class files. Goto a terminal window and entrer the following.</p> <pre><code>cd /home/path/to/library php doctrine-cli.php generate-models-db </code></pre> <p>If all has gone well the folders <code>application/modules/default/models/Doctrine</code> and <code>application/modules/default/models/Doctrine/Base</code> should contain the classes for your database. As I mentioned above do not change the files in the Base folder, you can use the classes in the parent Doctrine folder to make changes. You will also notice that there are two classes per database table in the Doctrine folder, one is suffixed with Table. Here I tend to place my DQL code to keep it away from my models/controllers. The other classes can be used to make changes to the classes in the base folder, also you can add hooks and listeners here to add table specific code such as adding password encryption or presetting dates etc.</p> <p>I hope I have explained it clearly enough as it is a pig of a job to get working but this is how mine is set up.</p> <p>I hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload