Model Generation
This page will try to explain how the automated AbstractModels generator work, and so how to create your database in a way that the generator will do most of the job for you.
Example Database
For exemples in this page let's assume we have a database that look like this:
| posts | |
| postId | integer primaryKey |
| user | integer |
| title | varchar 255 |
| description | longtext |
| users | |
| userId | integer primaryKey |
| name | varchar 255 |
| tags | |
| tagId | integer primaryKey |
| name | varchar 255 |
| posts_tags | |
| post | integer |
| tag | integer |
Detecting relationship
When the generator parse the database structure it will automaticly assume relations between fields and tables that have the exact same names. Table names can also be the plural form of the field name.
In our sample database, we can see that table posts contain a user field, seeing this the generator will consider that a user can have many posts and that each post may have one user.
The fact that the field posts.user can be NULL or not will affect the way that the relation will be considered.
- If the field can be null we will consider that a post doesn't require a user to exist, the relation can be expressed like this:
posts ignores users and users ignores posts - If the field can't be null a post can't exists without an existing user, the relation is considered as:
posts depend on users and users are required by posts
But in both of previous statements, a relationship between posts and users is detected and will be managed by the generated classes.
The default value of the field posts.user will also affect the relationship between thoose two elements, especially when we will delete users entries linked to one or more posts.
- If the field has a default value setted when a user is deleted then all posts that are linked to it will see their posts.user value reset to default.
- If the field has no default value it will depend of the relation type of the two elements if it's an Ignored relation then nothing will happen, if it's of a DependOn relation then linked posts will be deleted too.
sample generation script
Here's a minimal example on how to generate models from our databases.
We create a file createModels.php with the following lines of codes:
<?php require 'class-modelGenerator.php'; define('DB_CONNECTION','mysqldb://dbname;hostname;username;password'); $generator = new modelGenerator(DB_CONNECTION,'models',3); $generator->onExist = 'o' // mean overwrite existing BASE_modelClass but not the extended ones $generator->doGeneration('DB_CONNECTION',''); ?>
then we just have to launch our script manually preferably on command line:
user@host$ php createModels.php
The script will create 6 files in the models directory:
- 3 BASE classes that contains the default methods of extended models, and thoose are the files we will eventually update if a newer version is out.
- BASE_posts.php
- BASE_tags.php
- BASE_users.php
- 3 extended classes that contains our final classes. Thoose are the files we will eventually edit to sweet our needs. We won't update thooses files if a newer version of abstractModels is out.
each one of thoose files also contains an extended modelCollection corresponding to the models they handle. This permitt us to define specific collection methods for particular modelCollections.- posts.php
- tags.php
- users.php
So if we want to redefine relationships between models or set filters or anything else we will edit the extended files.
