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.
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. 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 theDatabase::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 isTestDatabase
)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 excludinghasMany
relations.
• Database::__construct($connection)
takes a connection object as a parameter.
• Database::setup()
is invoked within the constructor.
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.