PHP Classes

Caribu ORM: Map objects to databases records using annotations

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 281 All time: 7,609 This week: 455Up
Version License PHP version Categories
caribu 1.6BSD License7PHP 5, Databases, Design Patterns
Description 

Author

This package can map objects to databases records using annotations.

It provides base classes that can store and retrieve objects from database tables. The base classes should be extended by implementation classes.

The object-relational mapping information is extracted from annotation comments read from the implementation classes as well using PHP reflection. So it is possible to map complex datatypes such as DateTime into database column datatypes seamlessly.

The ORM classes provide means to retrieve, store and remove objects from the configured database tables. It supports MySQL, Postgresql and SQLite using PDO.

It supports objects that reference other objects, allowing to save also the referenced objects when a given object is saved if the relationship is set to @cascade.

The package also supports mapping classes to existing database tables using the attributes @id, @table and @column to define class mapping to legacy tables.

Innovation Award
PHP Programming Innovation award nominee
May 2015
Number 11
Annotations are useful information that can be added to classes to perform other tasks besides running the class code.

This package implements object-relational mapping that reads information about the mappings from annotation comments in the class code. It can work to map objects to existing legacy database tables.

Manuel Lemos
Picture of Maik Greubel
  Performance   Level  
Innovation award
Innovation award
Nominee: 4x

 

Recommendations

Map database records to objects
Store and retrieve objects without writing native SQL again

Example

<?php
/**
 * Please execute "composer install" first, to generate the autoload.php
 */
require dirname(__FILE__) . '/../vendor/autoload.php';

/**
 * Required types
 */
use Nkey\Caribu\Model\AbstractModel;
use
Nkey\Caribu\Orm\Orm;
use
Nkey\Caribu\Orm\OrmException;

/**
 * First you have to create a new table in schema 'test' of your mysql server
 * using the following statement:
 *
 * CREATE TABLE `demo` (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `content` TEXT, `published` INT);
 */

/**
 * @entity
 * @table demo
 */
class Demo extends AbstractModel
{

   
/**
     * The primary key
     *
     * @id
     *
     * @var int
     */
   
private $id;

   
/**
     * The content value
     *
     * @column content
     *
     * @var string
     */
   
private $demoContent;

   
/**
     * Published on
     *
     * @column published
     *
     * @var int
     */
   
private $publishedOn;

   
/**
     * @return int
     */
   
public function getId()
    {
        return
$this->id;
    }

   
/**
     *
     * @param
     * $id
     */
   
public function setId($id)
    {
       
$this->id = $id;
        return
$this;
    }

   
/**
     *
     * @return string
     */
   
public function getDemoContent()
    {
        return
$this->demoContent;
    }

   
/**
     *
     * @param
     * $demoContent
     */
   
public function setDemoContent($demoContent)
    {
       
$this->demoContent = $demoContent;
        return
$this;
    }

   
/**
     *
     * @return int
     */
   
public function getPublishedOn()
    {
        return
$this->publishedOn;
    }

   
/**
     *
     * @param
     * $publishedOn
     */
   
public function setPublishedOn($publishedOn)
    {
       
$this->publishedOn = $publishedOn;
        return
$this;
    }
}

/**
 * ***********************************************************************
 */

try {
   
/**
     * Configure the OR mapper
     */
   
Orm::configure(array(
       
'type' => 'mysql',
       
'host' => 'localhost',
       
'user' => 'test',
       
'password' => 'test1234',
       
'schema' => 'test'
   
));

   
// Now some business:
   
$demoEntity = new Demo();
   
$demoEntity->setDemoContent("Not so much important content, but it needs to be persisted!");
   
$demoEntity->setPublishedOn(time());
   
$demoEntity->persist();

   
// After persistence we can access now the generated id:
   
printf("Your persisted entity has the ID %d", $demoEntity->getId());
} catch (
OrmException $exception) {

    while(
$exception != null) {
        echo
$exception->getMessage() . "\n";
        echo
$exception->getTraceAsString() . "\n";
       
$exception = $exception->getPrevious();
    }
}


Details

Build Status Code Coverage Scrutinizer Code Quality Dependency Status Codacy Badge

caribu

An annotation-based PHP Object Relational Mapper

This project aims to be an object relational mapper easy to use. The main target is to support developers writing only simple attribute containing classes and store it as entities into a database.

It has support for annotations so it is very flexible.

Here is a very basic example about the usage (for sqlite):

    CREATE TABLE super_duper_content (id INTEGER PRIMARY KEY, content TEXT);

    /SuperDuperEntity.php/
    
    /
     * @table super_duper_content
     */
    class SuperDuperEntity extends AbstractModel
    {
      /
       * @column id
       */
      private $sid;
      
      private $content;
      
      public function setSid($sid)
      {
        $this->sid = $sid;
      }
      
      public function getSid()
      {
        return $this->sid;
      }
      
      public function setContent($content)
      {
        $this->content = $content;
      }
      
      public function getContent()
      {
        return $this->content;
      }
    }

    /write-data-example.php/
    
    use \Nkey\Caribu\Orm\Orm;
    
    /First configure the ORM/
    Orm::configure(array(
      'type' => 'sqlite',
      'file' => ':memory:'
    ));
    
    // Create sqlite database table for memory storage
    // Orm::getInstance()->getConnection()->exec("CREATE TABLE super_duper_content (id INTEGER PRIMARY KEY, content TEXT)");
    
    /Now create a new entity and persist it to database/
    $entity = new SuperDuperEntity();
    $entity->setContent("Mega important content");
    $entity->persist();

    /read-data-example.php/
    
    /First read the entity from database/
    $entity = SuperDuperEntity::find(array('content' => "Mega important content"));
    
    /Display the content/
    echo $entity->getContent();

No need to write infrastructure and boilerblate code by yourself. Let the Orm do the hard work for you.

Caribu provides a convention-over-configuration behaviour by supporting annotations.

See the Wiki for more information about the capabilities and usage.


  Files folder image Files (72)  
File Role Description
Files folder imagecontrib (2 files)
Files folder imagesrc (3 directories)
Files folder imagetests (4 files, 5 directories)
Accessible without login Plain text file .gitignore Data Ignore list
Accessible without login Plain text file .travis.yml Data Travis ci configuration
Accessible without login Plain text file build.xml Data Build script
Accessible without login Plain text file composer.json Data Composer dependencies
Accessible without login Plain text file LICENSE Lic. License
Accessible without login Plain text file phpcs.xml Data Code style configuration
Accessible without login Plain text file phpdox.xml Data phpdox configuration
Accessible without login Plain text file phpmd.xml Data Mess detection configuration
Accessible without login Plain text file phpunit.xml Data PHPUnit configuration
Accessible without login Plain text file README.md Doc. Readme

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
Downloadcaribu-2017-08-22.zip 65KB
Downloadcaribu-2017-08-22.tar.gz 34KB
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
PHP Generics Download .zip .tar.gz Logging & Interpolation Required
 Version Control Unique User Downloads Download Rankings  
 100%
Total:281
This week:0
All time:7,609
This week:455Up