Skip to content

Latest commit

 

History

History
224 lines (163 loc) · 5.02 KB

README.en.md

File metadata and controls

224 lines (163 loc) · 5.02 KB

KumbiaPHP

Scrutinizer Code Quality Code Coverage Build Status Code Climate

ENGLISH - SPANISH

ActiveRecord

New ActiveRecord in development

Don't use in production

Install with composer in KumbiaPHP

Requires KumbiaPHP > 0.9RC

  • Create file composer.json in to project root:
--project  
    |  
    |--vendor  
    |--default  
    |--core  
    |--composer.json        This is our file  
  • Add the next lines:
{
    "require": {
        "kumbia/activerecord" : "dev-master"
    }
}
  • Execute command composer install

  • Continue with steps number 2 and 3 of the next section.

Install in KumbiaPHP

Requires KumbiaPHP > 0.9RC

  1. Copy folder lib/Kumbia in vendor. (vendor/Kumbia/ActiveRecord/..)

  2. Copy config_databases.php in app/config/databases.php and set configuration

  3. Add in app/libs/ : lite_record.php and/or act_record.php

LiteRecord

For those who prefer SQL and the advantages of an ORM it includes a mini ActiveRecord

<?php
//app/libs/lite_record.php

/**
 * LiteRecord 
 * For those who prefer SQL and the advantages of an ORM
 *
 * Parent class to add your methods
 *
 * @category Kumbia
 * @package ActiveRecord
 * @subpackage LiteRecord
 */

use Kumbia\ActiveRecord\LiteRecord as ORM;

class LiteRecord extends ORM
{

}

ActRecord

Full ActiveRecord

<?php
//app/libs/act_record.php

/**
 * ActiveRecord
 *
 * Parent class to add your methods
 *
 * @category Kumbia
 * @package ActiveRecord
 * @subpackage ActiveRecord
 */

use Kumbia\ActiveRecord\ActiveRecord;

class ActRecord extends ActiveRecord
{

}

Example

Model

<?php
//app/models/people.php

class People extends ActRecord //or LiteRecord depending on your choice
{

}

Controller

<?php
//app/controller/people_controller.php

//Load::models('people'); This is not necessary in v1

class PeopleController extends AppController {

    public function index() {
        $this->data = People::all();
    }
    
    public function find($id) {
        $this->data = People::get($id);
    }
}

Using LiteRecord methods

Filtering data

    //get all as array of records
    $rows = People::all();
    echo $row[0]->name;

    //get by primary key as record
    $row = People::get($peopleId);
    echo $row->name;

    //filter as array of records
    $rows = People::filter("WHERE name LIKE ?", [$peopleName]);
    echo $rows[0]->name;

    //filter by sql as record
    $row = People::first("SELECT * FROM people WHERE name = :name", [":name" => $peopleName]);
    echo $row->name;

    //filter by sql as array of records
    $rows = People::all("SELECT * FROM people WHERE hire_date >= ?", [$hireDate]);
    echo $rows[0]->name;

DML / Insert, update, delete

    //adding a new record
    $peopleObj = new People();
    $peopleObj->create([
        'name' => 'Edgard Baptista',
        'job_title' => 'Accountant',
        'hire_date' => date('Y-m-d'),
        'active' => 1
    ]); //returns True or False on success or fail

    //adding a new record alternative
    //please prefer this method by simplicity. 
    //save executes create method when primary key is missing 
    //and update ones when it exists
    $peopleObj = new People();
    $peopleObj->save([
        'name' => 'Edgard Baptista',
        'job_title' => 'Accountant',
        'hire_date' => date('Y-m-d'),
        'active' => 1
    ]); //returns True or False on success or fail

    //adding a new record alternative //shorthand method
    //passing the data when instantiate the class
    $peopleObj = new People([
        'name' => 'Edgard Baptista',
        'job_title' => 'Accountant',
        'hire_date' => date('Y-m-d'),
        'active' => 1
    ]);
    $peopleObj->save(); //returns True or False on success or fail

    //updating a record
    //first find the record to update
    $peopleObj = People::get($peopleId);

    $peopleObj->update([
        'name' => 'Edgard Baptista Jr',
        'active' => 0
    ]); //returns True or False on success or fail

    //updating a record alternative
    //first find the record to update
    $peopleObj = People::get($peopleId);

    $peopleObj->save([
        'name' => 'Edgard Baptista Jr',
        'active' => 0
    ]); //returns True or False on success or fail


    //deleting a record by primary key
    People::delete($peopleId);