one-to-one,
one-to-many and
many-to-many relationships. These relationships makes it easier to interact with the database
through the build-up of more concise, well organized and easily maintainable code structure. Here, most of the database
operations are executed through automatically generated sql queries which are dependent on the data structure and how they expected to be managed.
The format in which structures are defined in relation with session accounts which makes it easier to process queries are otherwise termed as database
models. These models forms a building block for performing database operations. In spoova, the root Model class manages all user relationships with
the database. All child models that are extended to the root Model class will inherit the essential properties needed for performing database operations.
The following are helper methods within the Model class.
Model::where()
Model::read()
Model::update()
Model::delete()
Model::ofUser()
Model::of()
read(), update() and delete() methods can be applied upon it. Assuming we have a model class User
as below:
<?php
namespace spoova/mi/windows/Models;
class User extends Model {
function __construct(){
}
}
where() method from a route as shown below:
<?php
namespace spoova/mi/windows/Routes;
use spoova/mi/windows/Models/User;
class SomeRoute{
function __construct() {
User::where('id = ?', [1])->read()->User; // read user where id is one
User::where('id = ?', [1])->delete(); // delete user where id is 1
User::where('id = ?', [1])->update(['firstname'=>'Felix']); // update user where id is 1, set firstname as "Felix"
}
}
where() condition can be applied. The first argument contains list of fields and placeholders
for binded parameters while the second argument contains a list of binded parameter values. The assumed table name will be "users". The table name can be
modified by re-defining a tableName() method on the model class which must return a string of the custom table name.
read() method.
User::read()->User; // fetch all user data User::read(['username'])->User; // fetch only the username of every user User::read(['firstname', 'lastname'], [10])->User; // fetch firstname and lastname of 10 users
update() method is used to update a selected record.
It takes a single array parameter. Which contains key(field name) and value(new) pairs. When a condition is not set upon it,
all records will be updated:
Posts::update(['date' => 2025-11-07 ]) //update all posts records, set all date rows as "2025-11-07"
delete() method takes a single parameter which can either be a bool of true or an integer limit of number of data to be deleted. The limit may not
be applicable on multiple table. Calling this method without a condition can be dangerous as all records
belonging to the relative database table may get deleted. The bool argument of "true" ensures that a developer
is aware of the changes they are about to make (i.e deleting all records) before making them. If no argument is supplied, and no condition is set on the delete method,
this method will not delete any data. It is also advised to to keep the live server off if this method will be applied.
Posts::delete(true); //delete all posts
ofUser() method is used to pull a data of the current user session account.
The database default structure format demands that any table owned by the current user must have a
user_id foreign key column that is mapped to the id field on the user's table. When data
are obtained using models, ofUser() makes it possible to pull only data related to the current user.
For example: If a database table "user" contains an id (primary key) column, then a table (comments) must have a field
name with user_id. The user_id is then used to pull data from the comments table which belongs
to the current session user.
<?php
namespace spoova\mi\core\class\Models;
use Models;
Posts extends Models {
}
public static function tableName() {
return 'Posts';
}
}
ofUser()
static method is applied on "Posts" (i.e Posts::ofUser() ), then spoova will try to find
the Posts related to the current user id by looking for a user_id field in the
Posts table. By default, this method uses the current user id to pull data, however, this can be modified.
The ofUser() method takes its first argument as an integer. This integer is the id of the user
whose data must be pulled from the database. For example:
<?php
namespace spoova\mi\windows\Routes;
use Window
use spoova\mi\windows\Models\Posts;
Home extends UserFrame {
function __construct() {
$currentUserPosts = Posts::ofUser()->read()->Posts;
$customUserPosts = Posts::ofUser(2)->read()->Posts;
}
}
}
read() method is used to read data from the database while the ->Posts
is the current Model name which stores the data obtained as a traversable object. When an error occurs, the last error
is saved and can be obtained using the read()->DBError property. In the event that the foreignKey field name is
not user_id, then a second argument can be supplied into the ofUser() method to define a new key name, In this
relationship, the user database table (i.e user) will be the owner while the Model (e.g Posts) table is being owned. We don't need to set the
user table since that has been done in the icore/init file during installation.
of() method is similar to the ofUser() method. In this method, the table name can be
customized if the relationship is between a model and any other database table. The first argument
takes a new database table name while other arguments follows the ForeignId and ForeignKey structure respectively. By default,
if the owner database table for example is "admin", then a owned table "posts" must have an "admin_id" foreign key field
while the owner table must have an "id" local key field which helps spoova
to naturally connect the two fields. For example:
Posts::of('admin', 3)->read()->Posts;
Posts::of('admin', 3, 'user_id')->read()->Posts;
user_id field.
It is assumed that the custom foreign key supplied in this case is related to the admin table's local key "id".
This means that the admin table must have an "id" primary key field.
bind() method is a method that can be called upon the model's of() method.
The bind() method is used to set up a connection between three database fields. In this
connection, the binded table is the highest table while the current model is the lowest table. The database table structures
resemble the format below:
comments
-id
-post_id
posts
-id
-user_id
user
-id
Comments::of('posts', 3)->bind('users')->read()->Comments;
comments table has a foreign key field of post_id while the
posts table has a foreign key field of user_id. From the sample above, the
comment table will look within itself for where post_id foreign key is equivalent to posts table local key "id" 3.
then the posts table will bind to its own parent "users" through the parent foreign key field name "user_id".
This relationship can thus be defined as a complex relationship, one that is defined for a Child, Parent and GrandParent.
In the event that the foreign key of the post table on comments table above is not post_id, this can be modified by supplying a third
argument of the foreign key field name on the of() method.
Comments::of('posts', 3, 'foreignKey')->bind('users')->read()->Comments;