Zend Framework Tutorial
By Lyndon Baptiste2008-08-13
Before zend_Db_Table I found myself wearied by creating model classes complete with getters and setters for each relation I had in my database and fetchList and fetchRow methods which not only accessed some database connection, but also created and populated objects. This would become messier and messier each time I had an object composed of another, because with OOP there is no foreign key, but rather a collection of objects. I was confused as to whether or not I should automatically load up the next object and eventually went as far as having a lazy load parameter. So to me Zend_Db_Table is a blessing not in disguise, but simply a blessing. I now have the ability to subclass this beauty of a class, specify the name of the table the class should map to, its primary key and provide a database object which was already done through a call to the static method Zend_Db_Table::setDefaultAdapter($db); on line 28 of index.php. So now that's done and doesn't need to happen again let's create a database table in our zend database and a corresponding class. The code for the sql is as follows below.
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL auto_increment,
`email` varchar(100) NOT NULL,
`password` varchar(32) NOT NULL,
`gender` char(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
In the models directory create a file User.php and paste the following code:
<?php
class User extends Zend_Db_Table_Abstract {
protected $_name = 'user';
protected $_primary = 'id';
}
?>
There are more options that you can set up such as a reference map between classes and database entities, which can make fetching parent rows and dependent rowsets easier. For the purpose of this tutorial I wouldn't go so far. We'll incorporate the use of the User class to save a record to the database.
Tutorial pages:
|
|
|||||||||
You might also want to check these out:
|
Link to This Tutorial Page!

