PHPackages                             aryelgois/medools - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. aryelgois/medools

ActiveLibrary[Database &amp; ORM](/categories/database)

aryelgois/medools
=================

Wrapper on catfan/Medoo

v5.1.2(7y ago)71.2k[1 issues](https://github.com/aryelgois/Medools/issues)4MITPHPPHP ^7.0

Since Nov 9Pushed 7y agoCompare

[ Source](https://github.com/aryelgois/Medools)[ Packagist](https://packagist.org/packages/aryelgois/medools)[ RSS](/packages/aryelgois-medools/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (10)Dependencies (2)Versions (19)Used By (4)

Medools
=======

[](#medools)

> “We can solve any problem by introducing an extra level of [indirection](https://en.wikipedia.org/wiki/Indirection).”

Index:

- [Intro](#intro)
- [Install](#install)
- [Setup](#setup)
- [Using a Model](#using-a-model)
    - [Creating a new Model](#creating-a-new-model)
    - [Loading from Database](#loading-from-database)
    - [Saving into Database](#saving-into-database)
    - [Deleting from Database](#deleting-from-database)
    - [Accessing and manipulating data](#accessing-and-manipulating-data)
    - [Iterating over many rows](#iterating-over-many-rows)
    - [Foreign models](#foreign-models)
    - [Other methods](#other-methods)
- [Reusing models](#reusing-models)
- [Configuring a Model](#configuring-a-model)
    - [DATABASE](#database)
    - [TABLE](#table)
    - [COLUMNS](#columns)
    - [PRIMARY\_KEY](#primary_key)
    - [AUTO\_INCREMENT](#auto_increment)
    - [STAMP\_COLUMNS](#stamp_columns)
    - [OPTIONAL\_COLUMNS](#optional_columns)
    - [FOREIGN\_KEYS](#foreign_keys)
    - [READ\_ONLY](#read_only)
    - [SOFT\_DELETE](#soft_delete)
    - [SOFT\_DELETE\_MODE](#soft_delete_mode)
- [Advanced](#advanced)
    - [Events](#events)
    - [ModelManager](#modelmanager)
- [Changelog](CHANGELOG.md)

Intro
=====

[](#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](src/Model.php) base class and define some settings.

> To interact with the Database, it is used [catfan/Medoo](https://github.com/catfan/Medoo).

Install
=======

[](#install)

Open a terminal in your project directory and run:

`composer require aryelgois/medools`

Setup
=====

[](#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](https://github.com/catfan/Medoo) objects. See details in its [guide](https://medoo.in/api/new). You can also follow the [config example](config/example.php).

This file returns an array with the following keys:

- `servers`: Lists database Servers that can be connected and their credentials. Each item is an array with the following keys:

    - `server`
    - `port` *(optional)*
    - `username`
    - `password`
    - `database_type` It needs the corresponding PHP\_PDO extension
- `databases`: List of Databases referenced in the models classes. Each item is an array that contains:

    - `server` Server key from the previous list. If omitted, `default` is used
    - `database_name`

    A shortcut to only defining `database_name` is setting the database directly as string

There are other configurations that can be used, see the [guide](https://medoo.in/api/new) 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:

```
