Download the PHP package mattvb91/lightmodel without Composer
On this page you can find all versions of the php package mattvb91/lightmodel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package lightmodel
LightModel
What is LightModel?
The LightModel ORM is test project to build an ActiveRecord style implementation from scratch.
Using this in a live project is not recommended. Please look at Eloquent or Zend Model for a robust and highly tested solution.
Usage
To initialize LightModel you need to call and pass your instance of PDO to it.
You can also pass in an optional array for further configuration. For example:
Currently the typecast option is the only option available. If used this will typecast Integer columns defined in your MySQL database to be an integer attribute on your model.
To get started with a model, your class needs to extend
Creating a Model
You will need to implement the function in which you define a key value array for the values associated with your columns.
For example a basic table with with the following structure could be represented like this:
You do not need to manually bind the primary key column if it is set up as an auto increment value in your DB.
You can of course use your normal getters to access your column values too inside the method as long as you bind it to your correct column.
To create a new row on your user table you would use the following:
To override the table name or table key simply implement the following in your class:
Fetch a row by key
To fetch a row by its key use the static on the class you want to fetch. For example:
Check a model exists
To check if a model actually exists in your database you can use the method.
Refresh a model
You may run into situations where you need to fetch the latest version of your row again. Use the method to update your current model.
Keep in mind this will set your model back to whats currently in your DB.
Delete a model
To delete a model simply call the method:
Relationships
BelongsTo
To define a Belongs to relationship use the method in your model. For example if our User is associated with a Department you could do the following inside your User class.
Once a relationship has been queried once any further accesses to not hit the database again.
Fetching multiple rows
To fetch multiple rows simply use the method.
Filtering
You can also filter data sets by passing an optional array int the method. You must pass the correct table column name.
Optionally you can also pass in the operator you want to perform. The order MUST be Table_Column => ['Operator', 'Value']
To set the order or limit the results returned you can make use of the and constants and pass them in your options array:
Fetching keys
You may sometimes run into situations where performing a brings back too large of a resultset. You can instead perform the same filters using which instead of returning an array of fully loaded Models it will now only return the unique column ID's that fit your filtered criteria. You can then use that ID to individually load the full model manually: