Models act as the gateway to the MySQL database, but do not require you to write any MySQL queries! The entire system can be accessed through PHP objects, which are autogenerated through the build system!
Models are the essential link to the database. This particular part of the Framework pre-dates the rest of the code, and was used as a standalone LINQ-like (Language Integrated Query) component for PHP and MySQL. This code was then ported to Boiler as part of it’s general release. The LINQ framework allows complete access to the database, without worrying about MySQL coding issues, and without having to manually write MySQL queries.
The best place to look for a working example of the LINQ components is in the build test file “tests/DBObjectTest.php” which is found in your installation. Let’s work through this example:
At this stage we need to do some model generation. If you do not have ant, you will need to read up on build.xml, but ant is easy to install, especially on Linux.
1) Run “ant db-config”. This will create a config.php file. Alter the values to connect to your database. If you have a Windows system you will need to manually create a config.php
2) Run “ant models”. This will create all your models using a set of PHP scripts
Tip: If you haven’t used the MySQL Workbench – it’s a great tool for modelling your databases!
This section of the code generates two tables in the MySQL database, user and login, which are linked with a foreign key.
This part of the code is the most used function. This function creates a new record in the MySQL table. Effectively, line 1 of this example creates the following SQL statement:
The returning value is an object reflecting that row, for example:
A row can also be retrieved by just asking for a new Object:
As well as this, most of the commands you would expect in MySQL are available as functions in the Object: