PHP Classes

Dialect PHP ORM and ODM Library: Store and retrieve objects in database using ORM

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-08-27 (2 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 50 All time: 10,720 This week: 455Up
Version License PHP version Categories
dialectorm 2.1.0Artistic License5PHP 5, Databases, Design Patterns
Description 

Author

This class can store and retrieve objects in database using object-relational mapping and object-document-mapping (NoSQL).

It provides a base class that implements functions to access database tables that store records with the values of the variables and relationships of the objects that mapped to table records.

Applications just need to extend the base class to create implementation data objects by defining the variables and fields, table relationships and primary keys of the tables that will store those data objects.

The package also provides of the same classes in JavaScript and Python.

Picture of Nikos M.
Name: Nikos M. is available for providing paid consulting. Contact Nikos M. .
Classes: 19 packages by
Country: Greece Greece
Age: 48
All time rank: 8449 in Greece Greece
Week rank: 195 Up2 in Greece Greece Up
Innovation award
Innovation award
Nominee: 8x

Winner: 2x

Documentation

DialectORM

Super-simple, fast and versatile Object-Relational-Mapper (ORM) with Relationships and Entity-Attribute-Value and Object-NoSql-Mapper for PHP, Python, JavaScript

DialectORM

version 2.1.0

Dependencies:

  • Dialect
  • PHP: 5.3+
  • Python: 3+
  • JavaScript: ES7+

Supports:

  • Relational Datastores: MySql, MariaDB, Postgresql, Sql Server, Sqlite, easily extended to other Relational Stores via adding Dialect configuration
  • NoSql / Key-Value DataStores: Redis, easily extended to other NoSql Stores via providing adapter

example (see /test folder)

class Post extends DialectORM
{
    public static $table = 'posts';
    public static $pk = ['id'];
    public static $fields = ['id', 'content'];
    public static $extra_fields = ['postmeta', 'post_id', 'id', 'key', 'value'];
    public static $relationships = [];

    public function typeId($x)
    {
        return (int)$x;
    }

    public function typeContent($x)
    {
        return (string)$x;
    }

    public function validateContent($x)
    {
        return 0 < strlen($x);
    }
}

class PostStatus extends DialectORM
{
    public static $table = 'poststatus';
    public static $pk = ['id'];
    public static $fields = ['id', 'status', 'type', 'post_id'];
    public static $relationships = [];

    public function typeId($x)
    {
        return (int)$x;
    }

    public function typePostId($x)
    {
        return (int)$x;
    }

    public function typeStatus($x)
    {
        return strtolower((string)$x);
    }

    public function typeType($x)
    {
        return strtolower((string)$x);
    }

    public function validateStatus($x)
    {
        return in_array($x, ['approved', 'published', 'suspended']);
    }

    public function validateType($x)
    {
        return in_array($x, ['article', 'tutorial', 'general']);
    }
}

class Comment extends DialectORM
{
    public static $table = 'comments';
    public static $pk = ['id'];
    public static $fields = ['id', 'content', 'post_id'];
    public static $relationships = [];

    public function typeId($x)
    {
        return (int)$x;
    }

    public function typeContent($x)
    {
        return (string)$x;
    }

    public function typePostId($x)
    {
        return (int)$x;
    }

    public function validateContent($x)
    {
        return 0 < strlen($x);
    }
}

class User extends DialectORM
{
    public static $table = 'users';
    public static $pk = ['id'];
    public static $fields = ['id', 'name'];
    public static $relationships = [];

    public function typeId($x)
    {
        return (int)$x;
    }

    public function typeName($x)
    {
        return (string)$x;
    }

    public function validateName($x)
    {
        return 0 < strlen($x);
    }
}

Post::$relationships = [
    'status' => ['hasOne', 'PostStatus', ['post_id']],
    'comments' => ['hasMany', 'Comment', ['post_id']],
    'authors' => ['belongsToMany', 'User', ['user_id'], ['post_id'], 'user_post'],
];
PostStatus::$relationships = [
    'post' => ['belongsTo', 'Post', ['post_id']]
];
Comment::$relationships = [
    'post' => ['belongsTo', 'Post', ['post_id']],
];
User::$relationships = [
    'posts' => ['belongsToMany', 'Post', ['post_id'], ['user_id'], 'user_post'],
];

function output($data)
{
    if (is_array($data))
    {
        echo json_encode(array_map(function($d){return $d->toArray(true);}, $data), JSON_PRETTY_PRINT) . PHP_EOL;
    }
    elseif ($data instanceof DialectORM)
    {
        echo json_encode($data->toArray(true), JSON_PRETTY_PRINT) . PHP_EOL;
    }
    else
    {
        echo ((string)$data) . PHP_EOL;
    }
}

output('Posts: ' . (string)Post::count());
output('Users: ' . (string)User::count());

$post = Post::fetchAll(['conditions' => ['content' => 'a php post..'],'single' => true]);
if (empty($post))
{
    $post = new Post(['content'=>'a php post..']);
    $post->setCustomField1('custom value 1'); // extra custom fields as Entity-Attribute-Value pattern
    $post->setComments([new Comment(['content'=>'a php comment..'])]);
    $post->setComments([new Comment(['content'=>'one more php comment..'])], ['merge'=>true]);
    $post->setAuthors([new User(['name'=>'a php user'])]);
    $post->setStatus(new PostStatus(['status'=>'approved','type'=>'article']));
    $post->save(['withRelated'=>true]);
}
output($post);

print('Posts:');
output(Post::fetchAll([
    'conditions' => ['custom_field1' => 'custom value 1'],
    'withRelated' => ['status', 'comments', 'authors'],
    'related' => [
        'authors' => ['conditions'=>['clause'=>['or'=>[
            ['name'=>['like'=>'foo']],
            ['name'=>['like'=>'bar']]
        ]]]],
        'comments' => ['limit'=>1] // eager relationship loading with extra conditions, see `Dialect` lib on how to define conditions
    ]
]));

see also:

  • ModelView a simple, fast, powerful and flexible MVVM framework for JavaScript
  • tico a tiny, super-simple MVC framework for PHP
  • LoginManager a simple, barebones agnostic login manager for PHP, JavaScript, Python
  • SimpleCaptcha a simple, image-based, mathematical captcha with increasing levels of difficulty for PHP, JavaScript, Python
  • Dromeo a flexible, and powerful agnostic router for PHP, JavaScript, Python
  • PublishSubscribe a simple and flexible publish-subscribe pattern implementation for PHP, JavaScript, Python
  • Localizer a simple and versatile localization class (l10n) for PHP, JavaScript, Python
  • Importer simple class &amp; dependency manager and loader for PHP, JavaScript, Python
  • EazyHttp, easy, simple and fast HTTP requests for PHP, JavaScript, Python
  • Contemplate a fast and versatile isomorphic template engine for PHP, JavaScript, Python
  • HtmlWidget html widgets, made as simple as possible, both client and server, both desktop and mobile, can be used as (template) plugins and/or standalone for PHP, JavaScript, Python (can be used as plugins for Contemplate)
  • Paginator simple and flexible pagination controls generator for PHP, JavaScript, Python
  • Formal a simple and versatile (Form) Data validation framework based on Rules for PHP, JavaScript, Python
  • Dialect a cross-vendor &amp; cross-platform SQL Query Builder, based on GrammarTemplate, for PHP, JavaScript, Python
  • DialectORM an Object-Relational-Mapper (ORM) and Object-Document-Mapper (ODM), based on Dialect, for PHP, JavaScript, Python
  • Unicache a simple and flexible agnostic caching framework, supporting various platforms, for PHP, JavaScript, Python
  • Xpresion a simple and flexible eXpression parser engine (with custom functions and variables support), based on GrammarTemplate, for PHP, JavaScript, Python
  • Regex Analyzer/Composer Regular Expression Analyzer and Composer for PHP, JavaScript, Python

  Files folder image Files (26)  
File Role Description
Files folder imagesrc (3 directories)
Files folder imagetest (1 file, 3 directories)
Accessible without login Image file dialectorm.jpg Icon Icon image
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (26)  /  src  
File Role Description
Files folder imagejs (1 file)
Files folder imagephp (1 file)
Files folder imagepy (2 files)

  Files folder image Files (26)  /  src  /  js  
File Role Description
  Accessible without login Plain text file DialectORM.js Data Auxiliary data

  Files folder image Files (26)  /  src  /  php  
File Role Description
  Plain text file DialectORM.php Class Class source

  Files folder image Files (26)  /  src  /  py  
File Role Description
  Accessible without login Plain text file DialectORM.py Data Auxiliary data
  Accessible without login Plain text file __init__.py Data Auxiliary data

  Files folder image Files (26)  /  test  
File Role Description
Files folder imagejs (4 files, 2 directories)
Files folder imagephp (4 files, 2 directories)
Files folder imagepy (4 files, 2 directories)
  Accessible without login Plain text file dialectorm.sql Data Auxiliary data

  Files folder image Files (26)  /  test  /  js  
File Role Description
Files folder imagenosql (1 file)
Files folder imagesql (1 file)
  Accessible without login Plain text file out-mysql.txt Doc. Documentation
  Accessible without login Plain text file out-redis.txt Doc. Documentation
  Accessible without login Plain text file test-mysql.js Data Auxiliary data
  Accessible without login Plain text file test-redis.js Data Auxiliary data

  Files folder image Files (26)  /  test  /  js  /  nosql  
File Role Description
  Accessible without login Plain text file redis.js Data Auxiliary data

  Files folder image Files (26)  /  test  /  js  /  sql  
File Role Description
  Accessible without login Plain text file mysql.js Data Auxiliary data

  Files folder image Files (26)  /  test  /  php  
File Role Description
Files folder imagenosql (2 files)
Files folder imagesql (1 file)
  Accessible without login Plain text file out-mysql.txt Doc. Documentation
  Accessible without login Plain text file out-redis.txt Doc. Documentation
  Plain text file test-mysql.php Class Class source
  Plain text file test-redis.php Class Class source

  Files folder image Files (26)  /  test  /  php  /  nosql  
File Role Description
  Plain text file redis.php Class Class source
  Plain text file redis_cli.php Class Class source

  Files folder image Files (26)  /  test  /  php  /  sql  
File Role Description
  Plain text file mysql.php Class Class source

  Files folder image Files (26)  /  test  /  py  
File Role Description
Files folder imagenosql (1 file)
Files folder imagesql (1 file)
  Accessible without login Plain text file out-mysql.txt Doc. Documentation
  Accessible without login Plain text file out-redis.txt Doc. Documentation
  Accessible without login Plain text file test-mysql.py Data Auxiliary data
  Accessible without login Plain text file test-redis.py Data Auxiliary data

  Files folder image Files (26)  /  test  /  py  /  nosql  
File Role Description
  Accessible without login Plain text file redis.py Data Auxiliary data

  Files folder image Files (26)  /  test  /  py  /  sql  
File Role Description
  Accessible without login Plain text file mysql.py Data Auxiliary data

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
 Version Control Unique User Downloads Download Rankings  
 100%
Total:50
This week:0
All time:10,720
This week:455Up