Database

BoosterORM and BoosterQueryBuilder libraries.

BoosterORM

Booster ORM is used to handle all models classes that as a developer in a web app will be necesary to create.

* remember table is set by default by models you dont need to add anything

The methods that support BoosterORM are:

  • all()

$users = User::all();
//retrives all the columnns of table users
//as a collection of objects to loop it as array
  • where()

to retrive data from a where condition.

$users = User::where('role=?', 'admin')->get();

Its posible to create a link wheres methods like this:

$users = User::where('role=?', 'admin')->where('age>?', '18')->get();

Either is posible to add multiple params in just one query:

$users = User::where('role=? AND age>?', ['admin', 18])->get();
  • get()

Is used to retrive data as a collection of object and data could be trait as array either, it´s necessary to add it as link method with where() methods, because without it data return as properties, and can´t be loop to print data as collection or array.

  • limit()

Is used to limit data retrive rows.

$users = User::where('age > ?', 18)->limit(10)->get();
  • orderBy()

Is used to give order to retrive data.

$users = User::where('age BETWEEN 18 AND 25')->orderBy('name DESC')->get();
  • groupBy()

Is used to group rows by certain criteria, rember that it works as any sql query, so if you group by certain columns your data could be or not the result that you want.

$users = User::where('age BETWEEN 18 AND 25')->groupBy('age')->get();

To retrive all this methods just write an foreach as a regular array:

foreach($users as $key->$user){
    echo $user->name;
}
  • first()

Is used to retrive a single row from a collection of objects, in this case the first element.

$user = User::all()->first();
echo $user->name;

Observe that this method does not need a get() linked method to retrive a collection of objects, first() method retrive just one and you can print it directly .

If in certain moments you have doubts of what are you retriving in some method of BoosterQueryBuilder or BoosterORM, you could use the helper:

dd($user);

It´s return a var_dump() of the param object or variable, with preformatted text to make it more readable, and a die() native function of PHP to stop running the script.

  • last()

Return the last object of a collection of objects.

$user = User::where('status !=?', 'admin')->last();
echo $user->email;//in this example we suppose that exist a column named email
  • find()

Search in a table by a certain value and column as a needle in haystack, this method works in two ways:

$user = User::find(15);

It works perfectly to get all columns from the user table in id 15, but in many cases you will need to search in other column so find() method could be use as:

$user = User::find('name', 'Jhon Doe');

So it could be used in both ways :) .

  • delete()

It deletes a row by id.

User::delete(15);

require the integer of id to delete the columns of these id.

  • save()

insert or update an object created in controllers if the object have property id like:

$object->id

It update data with current values, but if the object have not id it creates a new record in database.

Ex without property id.

$user = new User;
$user->name = 'John Doe';
$user->email = 'johndoe@mail.com';
$user->age = 20;
$user->save();

In this case the user is inserted.

$user = new User;
$user->id = 15;
$user->name = 'Johnny Doe';              //changed name
$user->email = 'johnnydoe@mail.com';     //changed email
$user->age = 20;
$user->save();

In this case the user is updated because id exists;

  • paginate()

This method is really helpful to retrive data when you have many rows and you want to display it with pagination.

Its particularly useful when you want to retrive for example blog posts of a blog, or to retrive a data of products in a table, certanly datatable exists and is a good plugin but it creates a paginate data natively and is faster because it does not query all data just the paginated one.

paginate just need a number of elements to display as integer or string, but integer is better to avoid recasting that consume a little bit more resources.

$posts = Posts::paginate(5);

It support the linked methods style like this:

$posts = Posts::where('status=?'. 'publish')->paginate(10);

paginate method does not need a get method linked because it already retrive data ready to use as a collection of objects.

This method is very relation with links() method, certainly does not have sense to use it without links() method.

  • links()

This method works on views, and set very easy, quickly and elegant one line function to generate the pagination links HTML markup dinamically , it have some useful features:

  • Create links to different pages.

  • Give a bold style to current page.

  • No need to adapt to most popular CSS frameworks like boostrap 3, bootstrap 4 and MaterializeCSS. (You could use some params to display it as a native element of those frameworks).

  • the default pagination generated have just a few styles to display inline and mark the current page, but with your custom css rules you would be able to customize paginator with id="paginator" and current page with id="current"

To print quicky a pagination just go to your view and type:

User::links();

Paginate and Links methods are included in both BoosterORM and BoosterQueryBuilder,

So you will must use the same class if you use BoosterORM paginate() method in your controller then you will need to use BoosterORM in view file to display the paginator links.

BoosterQueryBuilder

Alternativly you could use a queryBuilder to execute querys to database without create models, it will be find if you need a specific query like inner, left or right join that is too complex and not correspond just to specific table.

Or because you will execute a specific query to a table just a few times, other consideretion is because instead of MVC you want to use FrontController structure, either way both are ok, and you could use both at the same time.

BoosterQueryBuilder Methods are:

  • table

  • all

  • find

  • where

  • get

  • limit

  • orderBy

  • groupBy

  • first

  • last

  • insert

  • update

  • delete

  • statement

  • exists

  • paginate

  • links

Last updated

Was this helpful?