Introduction

C5orm is an object-relational mapping (ORM) for Concrete5 cms. C5orm is inspired by Eloquent, so most of c5orm syntax is pretty similar with Eloquent syntax.

Why you might need this ?

By default Concrete5 use Doctrine orm. Doctrine orm is hard to get started and the usage/syntax is so much more complicated than Eloquent. And using Eloquent in Concrete5 package may pose a problem because in Concrete5.7 uses a different version of some Iluminate packages which clash with Eloquent.

C5orm has beautiful usage syntax like Eloquent, but it will still suitable with Concrete5 because its not require another packages which can make conflict with Concrete5 core code. Its only require Concrete5 Database class.

Getting Started

Installation

First download the c5orm from composer. Go to root directory of your package and run composer require "fudyartanto/c5orm"

You directory structure should looks like below.

  • your_package root of package directory
    • vendor composer vendor directory
      • fudyartanto
        • c5orm c5orm directory
    • autoload.php include this file to load all class in composer vendor

Register C5orm class to your package controller.


protected $pkgAutoloaderRegistries = [
    'vendor/fudyartanto/c5orm/src' => '\Fudyartanto\C5orm'
];

Basic Usage

Defining Model


use Fudyartanto\C5orm\Model;

class Plane extends Model
{
    /**
 * The table associated with the model.
 *
 * @var string
 */
    protected static $table = 'plane';
}

Create, Read, Update and Delete Data (CRUD)


// Create data
$plane = new Plane();
$plane->name = "Sukhoi";
$plane->length = 21;
$plane->height = 6;
$plane->range = 3000;

if ($plane->save()) {
  // Success create data
} else {
  // Failed create data
}

// Read data by its primary key
$plane = Plane::find(1);
if ($plane) {
  echo $plane->name;
}

// Update data
$plane->name = "New Sukhoi";
if ($plane->save()) {
  // Success update data
} else {
  // Failed update data
}

// Delete data
if ($plane->delete()) {
  // Success delete data
} else {
  // Failed delete data
}

Querying With Model

Examples


// Retrieving all of the records for a given table.
$planes = Plane::all();

// Convert result to array.
$planes = Plane::all()->toArray();

// Get first occurrence data with where clause
$plane = Plane::where("name", "=", "Sukhoi")->first();

// Get total data
$total = Plane::where("height", ">", 6)->count();