Download the PHP package aryelgois/medools without Composer

On this page you can find all versions of the php package aryelgois/medools. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package medools

Medools

“We can solve any problem by introducing an extra level of indirection.”

Index:

Intro

A basic framework for models related to a Database.

With this framework, you configure just the minimal for your models and then you focus on your own code. Abstract the communication with your Database!

Creating a new model for your application is easy, just extend the Model base class and define some settings.

To interact with the Database, it is used catfan/Medoo.

Install

Open a terminal in your project directory and run:

composer require aryelgois/medools

Setup

Before using this framework, you need a config file somewhere in your application. It defines Servers and Databases that are accessed by the models.

The reason for the config file being .php is that it contains sensitive data, and if a client could request it, nothing would be returned.

Basically, it contains data to instantiate catfan/Medoo objects. See details in its guide. You can also follow the config example.

This file returns an array with the following keys:

There are other configurations that can be used, see the guide for more. You can add them where it is reasonable. When connecting to a database, its array is merged on top of the server's. It means that a database can overwrite a server configuration.

If the database server is not a servers key, it is used as it is. So the servers array is totally optional, as long as the database contains all required data to connect to the Database.

The whole point of this config file is that you can define multiple databases in the same Server without repeating its configurations, as well define databases in different servers.

Also, you need to include this line in the beginning of your code:

It's a good idea to put in something like a bootstrap.php, which also requires composer's autoload (prior to the line above), and is always required by your scripts.

MedooConnection works like a Medoo factory, but reuses its instances.

Using a Model

Creating a new Model

You can instantiate a new Model, without any parameters, to create a fresh model object. Then add data into its columns, like in any other object. Your code should known which columns are available, but you can call getColumns() for a complete list from that model.

Your changes are stored in that object, so you need to save() in order to send to the Database.

Loading from Database

Instantiate the model with a value for its PRIMARY_KEY, or specify an associative array with which columns you would like to filter. Only one row is loaded.

You can also load() after creating the object.

Saving into Database

Use save() to send all changes to the Database, and update() to only send some.

Some columns you might have changed can have different values afterwards, due to some validation process or Database defaults.

Deleting from Database

Simply use delete().

If the model has SOFT_DELETE configured, it will just update that column. So you are able to undelete() later.

Otherwise, it will completely delete the row from the Database, and reset() the model object.

You can use isDeleted() to check if the model was deleted. It is useful when SOFT_DELETE is configured.

Accessing and manipulating data

Just like in any object:

* It means that you can chain the models:
$model->foreign->column

You can also get data with:

And change data with:

Iterating over many rows

A ModelIterator is provided to access multiple rows, but it provides only one at time.

Give it a model class you want to iterate over, and some filter array. Then it will load() each matched row, one by one.

A shortcut is calling getIterator() directly from the model class, which just asks for $where.

Foreign models

This framework supports foreign models. You can configure them in the model class, and access $model->foreign_column. They are simply other models, referenced in your model.

They are loaded on demand, so you don't need to worry about loading lots of foreigns just because you want a single column from the model.

:warning: Warning: Be careful not to configure a circular foreign constraint. When serializing a model, it can fail because of recursion.

Other methods

Useful methods that are available:

You can also add custom methods in your models, to automatically get some data in a format, or for doing a specific task.

There are also event methods that are automatically called by some base methods.

Reusing models

To avoid creating multiple instances for the same model, there is a ModelManager class which contains pointers to models already created. To retrieve them, use the getInstance() method, which asks for a model class and a where clause (only one row is selected). A shortcut is calling the getInstance() directly from the model, which just asks for $where.

See more in the Advanced section.

If you wish, you can still create a new instance for an already existing model. The new object will not contain changes made in the old one.

Configuring a Model

The settings are constants in each model class. You can omit some to reuse from parent class.

Only COLUMNS are required to define a new model.

DATABASE

Database key in the Medools config file

TABLE

Database's Table the model represents

The recomended is to use a plural name for the table and its singular in the model name

COLUMNS

Columns the model expects to exist

The column may be followed by a prefered data type. If not specified, defaults to string:

Possible types are those supported by Medoo:

These types are used when accessing the column in the database, but since Medoo has a type auto-detection, only Object and JSON are required to automatically unserialize/decode arrays and objects. You can still use the others for an explicit type casting.

Note that nullable/optional columns can have a null value.

PRIMARY_KEY

Primary Key column or columns

AUTO_INCREMENT

Auto Increment column

STAMP_COLUMNS

List of columns to receive the current timestamp automatically

The columns are automatically updated with the current timestamp on save() and update(). This constant allows multiple timestamp columns. If the column was manually changed, it will not be overwritten.

These columns are implicitly optional, because they are changed before saving/updating the model. The exception is auto columns, which must be controlled by the Database (MySQL only allows one timestamp).

The following structure is valid:

Here, column_b will use the default.

OPTIONAL_COLUMNS

List of columns which have a default value or are nullable

You don't need to include implict optional columns, like AUTO_INCREMENT, STAMP_COLUMNS and SOFT_DELETE.

FOREIGN_KEYS

Foreign Keys map

A map of columns in the curent model which point to a column in another model

Example:

If the foreign class is in the same namespace, you can use ClassName::class.

READ_ONLY

If __set(), save(), update(), delete() and undelete() are disabled

SOFT_DELETE

If delete() actually removes the row or if it changes a column

It defines the column affected by the soft delete

This column is implicitly optional, so you must define a default value in the database schema accordingly to SOFT_DELETE_MODE.

SOFT_DELETE_MODE

How the soft delete works

Which value SOFT_DELETE should be setted to.

Possible value When not deleted When deleted
'deleted' 0 1
'active' 1 0
'stamp' null current timestamp

Advanced

Events

There are some methods that can be extended by overriding event methods. It makes easier to extend some functionalities.

Currently, these events are available:

ModelManager

This class tracks every model loaded during a request. It aims to avoid model duplication, mainly in foreign keys.

If you create a new instance for an already existing model, the new instance replaces the old one in this class, but other pointers to the old instance are not updated. To avoid this, use the getInstance() method provided either in this class or in the model.

Changelog


All versions of medools with dependencies

PHP Build Version
Package Version
Requires php Version ^7.0
aryelgois/utils Version ^0.5
catfan/medoo Version ^1.5
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package aryelgois/medools contains the following files

Loading the files please wait ....