Elegant: Getting Started

Introduction

Elegant is more than just a ORM. Elegant is also a database query builder, with a built-in database handler that enables an active record pattern implementation. Elegant is adaptable with any custom mvc project and could be used with many other popular php frameworks such as, Laravel and Codeigniter. By simply dropping a copy of the Elegant directory in any php mvc project you could get started on using the wonderful features of Elegant. Each database table has a corresponding "Model" that interacts with that table. Models allow you to build and execute MySQL queries.

Before getting started, be sure to configure a database connection in Elegant/dbconfig.php. For more information on configuring your database, check out the documentation.

Database Configuration

Inside the Elegant folder, you can find the dbconfig.php file. Set the proper configurations to use with your database.

<?php /* Elegant/dbconfig.php */ define("DB_HOST","localhost"); define("DB_USER","root"); define("DB_PASS","your-password"); define("DB_NAME","your-database-name"); ?>

Defining Models

To get started, let's create an Elegant model. All Elegant models extend Elegant\Model class.

Elegant Model Conventions

Now, let's look at an example Book model, which we will use to retrieve and store information from our books database table:

<?php include_once("Elegant/Model.php"); class Book extends Model { //note: Every model must have a construct and a call to parent. public __construct() { parent::__construct($this); } } ?>

Table Names

Note that we did not tell Elegant which table to use for our Book model in the previous code snippet. By convention, table names in Elegant use the "snake case" name of the model classes, in all lower case letters unless a different table name is explicitly specified within the model Book's construct as done in the code snippet below:

<?php include_once("Elegant/Model.php"); class Book extends Model { public __construct() { /** * The table name associated with the model. * * @var string */ $this->table_name = 'books'; parent::__construct($this); } } ?>

Retrieving Models

Adding Additional Constraints

The Elegant all method will return all of the results in the model's table. Since each Elegant model serves as a query builder, you may also add constraints to queries, and then use the get method to retrieve the results:

<?php $book_model = new Book(); $cols = array('title', 'author', 'description', 'genre'); $books = $book_model->where('active', '=', '1') ->orWhere('title', '=', 'Harry Potter') ->orderBy('name', 'desc') ->take(10) ->get($cols); ?>

Since Elegant models are query builders, you should review all of the methods available on the query builder. You may use any of these methods in your Elegant queries.

Working with Data

For Elegant methods like all and get retrieve results that are stored in an Array where each row can be reached by an integer index. Each row contains an instance of an Child Model, where each property is the column name holding the value returned from the database. The following code snippet is an example for working with your Elegant results:

<?php /* in controller */ $this->book_model = new Book(); $books = $this->book_model->getBookList(); include 'view/templates/header.php'; include 'view/pages/booklist.php'; include 'view/templates/footer.php'; /* in view */ foreach( $books as $book) { echo $book->title."</br>"; } ?>

Inserting & Updating Models


Inserts

To create a new record in the database, simply create a new model instance, set attributes on the model, then call the save method:

<?php include_once("Elegant/Model.php"); class Customer extends Model { public function __construct() { parent::__construct($this); } public function create ($name, $address) { $this->name = $name; $this->address = $address; return $this->save(); } } ?>

Updates

<?php include_once("Elegant/Model.php"); class Customer extends Model { public function __construct() { parent::__construct($this); } public function updateById ($id, $name, $address) { $this->name = $name; $this->address = $address; return $this->where('id', '=', $id)->save(); } } ?>

Deletes

To delete a model, specify by chaing a call to the method where with a call to the delete method on a model instance:

<?php include_once("Elegant/Model.php"); class Customer extends Model { public function __construct() { parent::__construct($this); } public function removeById($id) { return $this->where('id', '=', $id)->delete(); } } ?>