1. Create the model classes
2. Create a test
> 3. Create the PersistenceObjects
4. Describe the classes
5. Configure DatabaseManager and create the database
6. Use the PersistenceObjects in your controllers
7. Add database-methods
3. Create the PersistenceObjects
Now we need to extend PersistenceObject-class for each managed class we have. In this case we create three Persistenceclasses:
- UserPersistence
- AlbumPersistence
- PicturePersistence
Let's begin with the UserPersistence-class.
require_once dirname(__FILE__)."../lib/persistenceobjects/classes.php"; require_once dirname(__FILE__)."/../model/User.php"; require_once dirname(__FILE__)."/../model/Album.php"; require_once dirname(__FILE__)."/../model/Picture.php"; class UserPersistence extends PersistenceObject { public function UserPersistence() { $this->tableName = "user"; $this->className = "User"; parent::PersistenceObject(func_get_args()); } }
What happens here is that:
- We first pronounce the table name where all User data is located in the database. The table is named "user".
- Then we say that the class that holds all user values is named "User".
- Finally we call the parent constructor giving it all the arguments that was given to this constructuror. This is important because All PersistenceObjects can be constructed with different kinds of arguments. Say like the id of a user-object so that we could fetch it from the database or an instance of User class which we want to save to the database.
Remember to be case sensitive here!!!
Lets do the same for the rest of the classes:
require_once dirname(__FILE__)."../lib/persistenceobjects/classes.php"; require_once dirname(__FILE__)."/../model/User.php"; require_once dirname(__FILE__)."/../model/Album.php"; require_once dirname(__FILE__)."/../model/Picture.php"; class AlbumPersistence extends PersistenceObject { public function AlbumPersistence() { $this->tableName = "album"; $this->className = "Album"; parent::PersistenceObject(func_get_args()); } }
require_once dirname(__FILE__)."../lib/persistenceobjects/classes.php"; require_once dirname(__FILE__)."/../model/User.php"; require_once dirname(__FILE__)."/../model/Album.php"; require_once dirname(__FILE__)."/../model/Picture.php"; class PicturePersistence extends PersistenceObject { public function PicturePersistence() { $this->tableName = "picture"; $this->className = "Picture"; parent::PersistenceObject(func_get_args()); } }
Now if you run the test you should get an Exception that says UserPersistence has no PropertyDescriptions. Let's attend to that.
previous - Create a testnext - Describe the classes