Skip to content

Restler 3: Database

Arul edited this page May 28, 2015 · 2 revisions

Which DB/ORM/ActiveRecord Library goes well with Restler 3?

User contributed page

Restler can work with any database layer so we did not provide any explicitly. Here are some of the choices suggested by Restler 3 users. Please add your suggestions and recommendations by editing this page.

It started as an issue #101 and copied here for more visibility and user contribution

Check out http://stackoverflow.com/questions/108699/good-php-orm-library for more options


@Arul- suggests:


RedBean

I have used Redbean php successfully for one of my clients, it is a light weight ORM that creates tables and columns on the fly as they are needed and makes SQL feel very much like NoSQL counter parts

CodeIgniter DB

In another project I had to help 2 developers using codeigniter and [rest server] (https://github.com/philsturgeon/codeigniter-restserver) plugin to move to restler. In order to make them feel as home I used codeigniter db, which is part of codeigniter but David Dula had removed all the dependencies and made it standalone. I used Stand Alone CI Activerecord - Based on 2.1.0 and made some improvements. I created a base class called API and created an instance of the db object as the db property so that the code we write in code igniter model such as the following can be copy pasted into restler api class as is

<?php
$query = $this->db->get('object',1,0);
$row = $query->result_array();
return $row;

Propel

It is an open-source Object-Relational Mapping (ORM) for PHP5. It allows you to access your database using a set of objects, providing a simple API for storing and retrieving data.

I have tried propel some time back. It has some initial learning curve understanding the configurations and all but well worth it

Doctrine 2

A Object Relational Mapper and the Database Abstraction Layer. I have heard from many that it is good. I havent tested it personally myself yet. It is also making use of Annotations similar to restler, I just need to check and make sure these annotations do not conflict

Do you like Laravel framework and Eloquent ORM? Then you can use Capsule which packages the Eloquent ORM for standalone use, perfect for Restler 3.

Once you setup your database in your index.php as shown below

<?php
require_once '../vendor/bin/restler.php';

use Luracast\Restler\Restler;
use Capsule\Database\Connection;

Connection::make('main', array(
    'driver' => 'mysql',
    'host' => 'localhost',
    'database' => 'foodmento_v1',
    'username' => 'root',
    'password' => '',
    'collation' => 'utf8_general_ci',
    'prefix' => '',
), true);

$r = new Restler();
$r->addAPIClass('MyApiClass');
$r->handle();

You should be able to use the DB in your api methods. For example

<?php
use Capsule\DB;

class MyApiClass
{
    function get()
    {
        return DB::table('foo')->select('*')->get();
    }
}

@nickl- suggests:


Respect/Relational the fluent Relational database persistence tool.

Features:

  • Near-zero configuration.
  • Fluent interfaces like $mapper->author[7]->fetch();
  • Adapts itself to different database styles.
  • Records are handled as Plain Data Object.
  • No need to generate a thing, nada, nothing, zilch, bugger all!

Sample DB

   author (id INT AUTO_INCREMENT, name VARCHAR(32))
   post (id INT AUTO_INCREMENT, author_id INT, title VARCHAR(255), text TEXT, created_at TIMESTAMP)
   comment (id INT AUTO_INCREMENT, post_id INT text TEXT, created_at TIMESTAMP)

Sample implementation

<?php 

      use Respect\Relational\Mapper;
      $mapper = new Mapper(new PDO('sqlite:database.sq3'));
      
      /** fetching */
      $authors = $mapper->author->fetchAll();
      foreach ($authors as $author) {
          echo $author->name . PHP_EOL;
      }      
      
      /** persisting *//
      $alexandre = new stdClass;
      $alexandre->name = 'Alexandre Gaigalas';
      $alexandre->created_at = date('Y-m-d H:i:s');

      $mapper->author->persist($alexandre);
      $mapper->flush();
      
      /** complex joins */
      $manyComments = $mapper->comment->post->author[42]->fetchAll();
      
      /** conditions */
      $mapper->post(array("created_at >="=>strtotime('2012-01-01'))->fetchAll();
      
      /** order and limiting
      $mapper->comment->post[5]->fetchAll(Sql::orderBy('comment.created_at')->limit(20));
      
      

Also comes with the fluent SQL builder or DbClass

<?php
      $db = new Db(new Pdo('sqlite:mydb.sq3'));
      $db->select('*')->from('author')->fetchAll(function($obj) {
          return AuthorFactory::create((array) $obj);
      });