I'm using the zend framework to write an app and I'm having lots of problems getting my relational database models to work. I've read the quickstart and the docs several times and I'm still not sure how to do this. I post a picture of the relationships between the different tables involved to avoid having to explain everything since English is not my first language and I tend not to make myself clear when I try to explain...well complex things.
The tables press_releases, social_networking, blog_posts, rss_feed, directorios, users and articulos are all set as foreign keys in the table named planilla_users. I coded my table models the way the quickstart shows:
class Application_Model_DbTable_PlanillaUsers extends Zend_Db_Table_Abstract
{
protected $_name = 'planilla_users';
protected $_referenceMap = array(
'User' => array(
'columns' => 'users_id',
'refTableClass' => 'Application_Model_DbTable_Users',
'refColumns' => 'id'
),
'Articulo' => array(
'columns' => 'art_id',
'refTableClass' => 'Application_Model_DbTable_Articulos',
'refColumns' => 'id'
),...etc
...and the rest following the format of:
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
protected $_dependentTables = array('Application_Model_DbTable_PlanillaUsers');
I also have a model Planilla.php with all the setters and getters for the information to be stored/updated/retrieved/deleted,...however...I'm completely blank on what to do with the mapper. I don't know how it's supposed to work and I honestly haven't found a good example on how to do something like this yet. So any help would be mostly appreciated.
My advice to you would be to have a look at Doctrine, 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.
There is a nice screen cast here 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.
I also hear that ZF 2 will have Doctrine 2.0 included.
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.
You will notice a line at the top
pluginpaths.Freedom_Zend_Application_Resource
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 examplepluginpaths.Yourcompany_Zend_Application_Resource = "Yourcompany/Zend/Application/Resource"
The next line is where you place your database connection settings
resources.doctrine.connection_string = "mysql://username:password@localhost/database_name"
The next three lines:
tell Doctrine where to place the classes generated, as I am using a modular setup these go into my
application/modules/default/models/Doctrine
andapplication/modules/default/models/Doctrine/Base
respectively (see folder tree below).There are some other lines that will need changing, these should be self evident.
You will also need to add the following lines under the development heading in your application.ini.
You will also need the resource file Doctrine.php. Where this is located depends on your setup, mine is as follows.
The *Doctrine.php file is as follows (obviously ignore the *!!)
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.
The only line you should need to change is
so it points to your application's root folder, similar to the line in your index.php file in your public folder.
Now hopefully you should be ready to generate your database class files. Goto a terminal window and entrer the following.
If all has gone well the folders
application/modules/default/models/Doctrine
andapplication/modules/default/models/Doctrine/Base
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.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.
I hope this helps.