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,607 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  
Name: Maik Greubel <contact>
Classes: 10 packages by
Country: Germany Germany
Age: ???
All time rank: 105967 in Germany Germany
Week rank: 51 Up4 in Germany Germany Up
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

  Files folder image Files (72)  /  contrib  
File Role Description
  Accessible without login Plain text file phpcs.xsd Data Code style schema
  Accessible without login Plain text file phpunit.xsd Data PHPUnit schema

  Files folder image Files (72)  /  src  
File Role Description
Files folder imageModel (1 file)
Files folder imageOrm (15 files)
Files folder imageType (6 files)

  Files folder image Files (72)  /  src  /  Model  
File Role Description
  Plain text file AbstractModel.php Class Test model

  Files folder image Files (72)  /  src  /  Orm  
File Role Description
  Plain text file Orm.php Class Caribu ORM main class
  Plain text file OrmAnnotation.php Class Caribu ORM annotation provider
  Plain text file OrmClassUtil.php Class Class analysis functionality
  Plain text file OrmConnection.php Class Connection related functionality
  Plain text file OrmDataType.php Class Data type enumeration
  Plain text file OrmDataTypeConverter.php Class Data type conversion provider
  Plain text file OrmEntityAnalyzer.php Class Entity analyzing functionality
  Plain text file OrmException.php Class Derived exception
  Plain text file OrmExceptionHandler.php Class Exception handling related functionality
  Plain text file OrmMapping.php Class Mapping functionality
  Plain text file OrmPersister.php Class Persisting functionality
  Plain text file OrmStatement.php Class Statement related functionality
  Plain text file OrmTransaction.php Class Transaction related functionality
  Plain text file OrmTypeUtil.php Class Type analysis functionality
  Plain text file OrmUtil.php Class Common utility functionality

  Files folder image Files (72)  /  src  /  Type  
File Role Description
  Plain text file AbstractType.php Class Abstract database type
  Plain text file IType.php Class Database type interface
  Plain text file MySQL.php Class MySQL database type
  Plain text file Postgres.php Class PostgreSQL database type
  Plain text file Sqlite.php Class Sqlite database type
  Plain text file TypeFactory.php Class Database type factory

  Files folder image Files (72)  /  tests  
File Role Description
Files folder imagecomplex-tests (9 files)
Files folder imagefailure-tests (6 files)
Files folder imageModel (13 files)
Files folder imagesimple-tests (2 files)
Files folder image_files (4 files)
  Accessible without login Plain text file AbstractDatabaseTestCase.php Test Abstract test case
  Accessible without login Plain text file demo.php Example Example
  Accessible without login Plain text file MySqlAbstractDatabaseTestCase.php Test Test case abstraction
  Accessible without login Plain text file PostgresAbstractDatabaseTestCase.php Test Abstract test case for postgresql

  Files folder image Files (72)  /  tests  /  complex-tests  
File Role Description
  Accessible without login Plain text file ComplexTest.php Test Complex unit test
  Accessible without login Plain text file EntityListTest.php Test Referenced entity unit test
  Accessible without login Plain text file MySQLComplexTest.php Test Unit test for MySQL complex tests
  Accessible without login Plain text file MySQLEntityListTest.php Test MySQL test version of entity list unit test
  Accessible without login Plain text file MySQLPersistComplexTypesTest.php Test Unit test for complex type mapping in MySQL
  Accessible without login Plain text file PostgresComplexTest.php Test Unit test for postgresql
  Accessible without login Plain text file PostgresPersistComplexTypesTest.php Test Unit test for complex type mapping in Postgres
  Accessible without login Plain text file ReferencesTest.php Test Unit test for referencial persistence and retrieval
  Accessible without login Plain text file SimpleReferenceTest.php Test Relationship many-to-one unit test

  Files folder image Files (72)  /  tests  /  failure-tests  
File Role Description
  Accessible without login Plain text file DuplicateIdTest.php Test Test duplicate id behaviour
  Accessible without login Plain text file FailureTest.php Test Failures unit test
  Accessible without login Plain text file InvalidConnectionSettingsTest.php Test Unit test for invalid connection settings
  Accessible without login Plain text file InvalidModelTest.php Test Unit test for invalid model
  Accessible without login Plain text file InvalidReferenceTest.php Test Unit test for invalid mapping
  Accessible without login Plain text file InvalidTypeTest.php Test Another failing unit test

  Files folder image Files (72)  /  tests  /  Model  
File Role Description
  Accessible without login Plain text file AnnotatedGuestBookModel.php Test Annotation test model
  Accessible without login Plain text file Author.php Test Another unit test model
  Accessible without login Plain text file BlogPost.php Test Referenced entity test model
  Accessible without login Plain text file BlogUser.php Test Referenced entity test model
  Accessible without login Plain text file Book.php Test Another unit test model
  Accessible without login Plain text file GuestBookModel.php Test Test model
  Accessible without login Plain text file InvalidModel.php Test Test entity containing errors
  Accessible without login Plain text file InvalidReferenceModel.php Test Testing model for invalid mapping
  Accessible without login Plain text file MockedModel.php Test Test model
  Accessible without login Plain text file NoteModel.php Test Test model
  Accessible without login Plain text file ReferencedGuestBook.php Test Entity for referenced tests
  Accessible without login Plain text file User.php Test Entity for referenced tests
  Accessible without login Plain text file ValidNoteModel.php Test Test entity for valid tests

  Files folder image Files (72)  /  tests  /  simple-tests  
File Role Description
  Accessible without login Plain text file NonAnnotationTestTest.php Test Unit test for non-annotated entities
  Accessible without login Plain text file SimpleTest.php Test Simple unit test

  Files folder image Files (72)  /  tests  /  _files  
File Role Description
  Accessible without login Plain text file blog-seed.xml Data Referenced entity test data
  Accessible without login Plain text file guestbook-seed.xml Data Test data
  Accessible without login Plain text file notes-seed.xml Data Test data
  Accessible without login Plain text file referenced-guestbook-seed.xml Data Dataset for testing reference retrieval and persistence

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
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,607
This week:455Up