Skip to content

aredda/PHP-ORM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A simple PHP mapping library

By utilizing document comments, I managed to build a primitive annotation system, which allowed me to give extra information about properties, so I could construct valid and precise SQL queries from the provided models.

However, it is still under development.

Demonstration

In this situation, we will create our database from the code we will write, it is known as "code-first".

Let us suppose that we have two entities:

Country (id, name)

Band (id, name, genre, #country)

1. We will provide our models in an object-oriented style like that:

1. Next, we need a context/container/setup/communicator with the SQL Server, in order to build a context we need to create a class that extends Database as shown below:

  • Database is an abstract class that holds all of the required logic in order to act like a Context.
  • Database::map(args) is a method that takes the Models we want to map.
  • Database::setup() is an abstract method that should be defined, it should contain the Database::map(args) instruction, to determine which models to map.
  • Database::is_created() checks if there's a database with the same name as the Context Class (In our case it is TestDatabase)
  • Database::create() generates an SQL script from the provided models, then attempts to create the database, returns false if the operation fails.
  • Database::refresh($lazyMode = false) retrieves data from SQL Server, then it distributes the data to the containers of each provided model,
    $lazyMode = false tells the Context to bring data excluding hasMany relations.
3. To use the context, we should instantiate an object of our TestDatabase class:

Database::__construct($connection) takes a connection object as a parameter.
Database::setup() is invoked within the constructor.

4. Go to your phpMyAdmin panel, to confirm the creation of your database:

Country model:

Band model:

That's it, you're ready to go.


I have implemented this simple ORM in one of my other projects, if you're interested in other examples, check this.
More details and documentation will be available soon.

About

An attempt to simulate a PHP ORM.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages