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!

Getting Started

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:

  1. CREATE TABLE 'user' (
  2. id INT(11) NOT NULL AUTO_INCREMENT,
  3. name VARCHAR(255) NOT NULL,
  4. DATE INT(11) NOT NULL,
  5. PRIMARY KEY ('id')
  6. );
  7.  
  8. CREATE TABLE 'login' (
  9. id INT(11) NOT NULL AUTO_INCREMENT,
  10. ip VARCHAR(255) NOT NULL,
  11. DATE INT(11) NOT NULL,
  12. USER INT(11) NOT NULL,
  13. PRIMARY KEY ('id'),
  14. FOREIGN KEY ('user') REFERENCES 'user'('id')
  15. );

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!

How does it work

This section of the code generates two tables in the MySQL database, user and login, which are linked with a foreign key.

  1. $this->user = \Model\User::Create(array("id"=>1, "name"=>"Joe Bloggs", "date"=>time()));
  2. $this->login = \Model\Login::Create(array("ip"=>"127.0.0.1", "date"=>time(), "user"=>1));
  3. $this->loginB = \Model\Login::Create(array("ip"=>"127.0.0.1", "date"=>time(), "user"=>1));

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:

  1. INSERT INTO 'user' ('id', 'name', 'date') VALUES ('1', 'Joe Bloggs', NOW())

The returning value is an object reflecting that row, for example:

  1. echo $this->user->id; //"1"
  2. echo $this->user->name; //Joe Bloggs

A row can also be retrieved by just asking for a new Object:

  1. $this->user = new \Model\User(1); //Looks for a record with a primary key of 1

As well as this, most of the commands you would expect in MySQL are available as functions in the Object:

  1. $this->user->Delete(); // Deletes the MySQL object
  2. $this->user->setAttribute("name", "Joseph Bloggs"); // Alters the table
  3. $this->user->setAttributes(array("name"=>"ivebeenlinuxed", "date"=>time())); //Sets a range of fields


Add Comment | Categories: Boiler Framework | Posted: January 31, 2012

Leave a Reply

Your email address will not be published. Required fields are marked *


five × = 20

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>