In principle, the combination of Zend Framework with Doctrine is not too difficult. But first let’s talk about the preparations. According to the author of Zend Framework, the default file structure of project can be a bit more optimal.
Here is the default structure of the Zend Framework project files:
/ application/ default/ controllers/ layouts/ models/ views/ html/ library/
It can often be that you will have a number of applications (e.g., frontend and backend ), and you want to use the same model for them. In this case, it can be a good practice to create your models folder in library/, in which case the new structure would look as follows:
/ application/ default/ controllers/ layouts/ views/ html/ library/ Model/
In addition, the folder models/ is renamed to Model. We now proceed as follows:
- Download a fresh copy of Doctrine-xxx-Sandbox.tgz from the official website.
- Copy the contents of the lib/folder from the archive to our project library/ folder.
- Create another folder bin/sandbox/ in the root of our project and copy the rest of the archive there (except models/ folder and the index.php file).
Now the structure of our project should look like this:
/ application/ default/ controllers/ layouts/ views/ bin/ sandbox/ data/ lib/ migrations/ schema/ config.php doctrine doctrine.php html/ library/ Doctrine/ Model/ Doctrine.php
Clear the content of the folder bin/sandbox/lib/ – we now have the library in another place.
Now it’s time to configure the Doctrine to work with new file structure.
Change the value of the constant MODELS_PATH in the file bin/sandbox/config.php::
SANDBOX_PATH . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'Model'
Next, change the connection settings for the database. Change the value of the DSN constant to reflect your database settings. For example, if you use the DBMS MySQL, the DSN might look like this:
Configure include_paths on the first line in the config file, so our script can find files on new locations:
set_include_path( '.' . PATH_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . PATH_SEPARATOR . '.' . DIRECTORY_SEPARATOR . 'lib' . PATH_SEPARATOR . get_include_path());
Then connect the main Doctrine library file directly after installation paths, and set the startup function:
require_once 'Doctrine.php'; /** * Setup autoload function */ spl_autoload_register( array( 'Doctrine', 'autoload' ));