4. Describe the classes
And now the important part. PersistenceObject-classes do not know what attributes we want to save and where in the database we want to save them. Therefore we need to describe the setup for each PersistenceObject we have. Lets start with UserPersistence:
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"; $this->addBasicPropertyDescription("id"); $this->addBasicPropertyDescription("userName"); $this->addBasicPropertyDescription("password"); $this->addBasicPropertyDescription("userRole"); $this->addOneToManyPropertyDescription( "albums", "user_id", "user_album", "album_id", "Album", ExternalPropertyDescriptor::$DELETE, "album", "id"); $this->addOneToOnePropertyDescription( "favouritesAlbum", "user_id", "user_favouritesAlbum", "album_id", "Album", ExternalPropertyDescriptor::$DELETE); parent::PersistenceObject(func_get_args()); } }
"WOAH what's that?!?", you say.
Well let me explain. We have three kinds of descriptions here:
- addBasicPropertyDescription which is a simple class-attribute. strings, integers, floats and such can be described with this.
- addOneToManyPropertyDescription which describes a connection that states "This owns a lot of other objects". These are always arrays of Objects as is the case with the albums-property in the User-class.
- addOneToOnePropertyDescription which describes a connection that states "One object onws a single instance of an another object". These are always of type Object as is the case with the favouritesAlbum-property in the User-class
"But what are those parameters?".
The parameters explain that all instances of a user's albums-property
can be found by user_id in user_album-connection table. The albums are referenced by the values in the album_id column.
The albums themselves are of class-type Album. These are the mandatory instructions. The rest are optional but I'll explain them too.
When a user is deleted we want all its albums to be deleted too. Therefore we issue ExternalPropertyDescriptor::$DELETE as on
cascade event. Finally; when the albums are fetched for a user we can announce the order that the albums are fetched. In this case
we announce that we want the albums to be sorted by their id, which is located in the album-table.
Lets continue with AlbumPersistenceObject:
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"; $this->addBasicPropertyDescription("id"); $this->addBasicPropertyDescription("name"); $this->addBasicPropertyDescription("description"); $this->addOneToManyPropertyDescription("pictures", "album_id", "album_picture", "picture_id", "Picture", ExternalPropertyDescriptor::$DELETE, "picture", "id"); $this->addReferenceDescription("user_album", "album_id"); $this->addReferenceDescription("user_favouritesAlbum", "album_id"); $this->addReferenceDescription("album_picture", "album_id"); parent::PersistenceObject(func_get_args()); } }
"OK OK OK, I see we have similiar BasicPropertyDescriptors and a OneToManyPropertyDescriptor.
But what are ReferenceDescriptions?"
Well i'm glad you asked. Since Album is referenced by Users and Pictures,
we need to make sure that when an album is deleted,
all references to that album are deleted too. Therefore the descriptions.
- The first description states that a table called "user_album" might have a reference in the column "album_id".
- The second states that a table named "user_favouritesAlbum" might contain a reference in the "album_id" column.
- And the last one states that a table called "album_picture" might have references in the "album_id" column.
ReferenceDescriptions are important for the integrity of the database so make sure you announce them correctly.
And finally the PicturePersistence-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 PicturePersistence extends PersistenceObject { public function PicturePersistence() { $this->tableName = "picture"; $this->className = "Picture"; $this->addBasicPropertyDescription("id"); $this->addBasicPropertyDescription("name"); $this->addBasicPropertyDescription("description"); $this->addBasicPropertyDescription("url"); $this->addManyToManyPropertyDescription("albums", "picture_id", "album_picture", "album_id", "Album"); $this->addReferenceDescription("album_picture", "picture_id"); parent::PersistenceObject(func_get_args()); } }
The only new thing here is the addManyToManyPropertyDescription-method call. A ManyToMany connection states that "There are many objects that might reference to this object and in the same time this object reference to many objects of the same type". Yeah, that was difficult. Basically the situation is that many albums can reference to the same pictures as other albums and each picture knows which albums it belongs to. Many objects connect to Many objects.
Now if you run the test everything should be.... oh wait, We get a "No database selected" error. We forgot to configure database connections.
previous - Create the PersistenceObjectsnext - Configure DatabaseManager and create the database