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

Recommend this page to a friend!
  Info   View files Documentation   View files View files (20)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-10-16 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 26 This week: 1All time: 10,067 This week: 195Up
Version License PHP version Categories
dialectorm 1.0Artistic License5PHP 5, Databases, Design Patterns
Description Author

This class can store and retrieve objects in database using object-relational mapping.

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: 13 packages by
Country: Greece Greece
Age: 44
All time rank: 108510 in Greece Greece
Week rank: 18 Up1 in Greece Greece Up
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

 

Details

DialectORM

Tiny, fast, super-simple but versatile Object-Relational-Mapper (ORM) with Relationships for PHP, Python, JavaScript

DialectORM

see also:

  • Importer simple class & dependency manager and loader for PHP, Python, Browser / Node.js / XPCOM JavaScript
  • Contemplate a light-weight template engine for PHP, Python, Browser / Node.js / XPCOM JavaScript
  • HtmlWidget html widgets used as (template) plugins and/or standalone for PHP, Python, Browser / Node.js / XPCOM JavaScript (can be used as plugins for Contemplate engine as well)
  • PublishSubscribe a simple and flexible publish-subscribe pattern implementation for PHP, Python, Browser / Node.js / XPCOM JavaScript
  • Dromeo a flexible, agnostic router for PHP, Python, Browser / Node.js / XPCOM JavaScript
  • Dialect flexible cross-vendor & cross-platform SQL Query Builder for PHP, Python, JavaScript
  • StringTemplate simple and flexible string templates for PHP, Python, Browser / Node.js / XPCOM JavaScript
  • GrammarTemplate versatile and intuitive grammar-based templating for PHP, Python, Browser / Node.js / XPCOM JavaScript
  • Xpresion a simple and flexible eXpression parser engine (with custom functions and variables support) for PHP, Python, Browser / Node.js / XPCOM JavaScript
  • Regex Analyzer/Composer Regular Expression Analyzer and Composer for PHP, Python, Browser / Node.js / XPCOM JavaScript
  • RT client-side real-time communication for Node/XPCOM/JS with support for Poll / BOSH / WebSockets
  • Asynchronous a simple manager for async, linearised, parallelised, interleaved and sequential tasks for JavaScript
  • ModelView a light-weight and flexible MVVM framework for JavaScript/HTML5
  • ModelView MVC jQueryUI Widgets plug-n-play, state-full, full-MVC widgets for jQueryUI using modelview.js (e.g calendars, datepickers, colorpickers, tables/grids, etc..) (in progress)

Dependencies:

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

example (see /test folder)

define('DIR', dirname(__FILE__));
include(DIR.'/../../src/php/DialectORM.php');
include(DIR.'/PDODb.php');

DialectORM::dependencies([
    'Dialect' => DIR.'/Dialect.php',
]);
DialectORM::DBHandler(new PDODb([
    'dsn' => 'mysql:host=localhost;dbname=dialectorm',
    'user' => 'dialectorm',
    'password' => 'dialectorm'
], 'mysql'));

class Post extends DialectORM
{
    public static $table = 'posts';
    public static $pk = 'id';
    public static $fields = ['id', 'content'];
    public static $relationships = [
        'meta' => ['hasOne', 'PostMeta', 'post_id'],
        'comments' => ['hasMany', 'Comment', 'post_id'],
        'authors' => ['belongsToMany', 'User', 'user_id', 'post_id', 'user_post'],
    ];

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

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

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

class PostMeta extends DialectORM
{
    public static $table = 'postmeta';
    public static $pk = 'id';
    public static $fields = ['id', 'status', 'type', 'post_id'];
    public static $relationships = [
        'post' => ['belongsTo', 'Post', 'post_id']
    ];

    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 = [
        'post' => ['belongsTo', 'Post', 'post_id'],
    ];

    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 = [
        'posts' => ['belongsToMany', 'Post', 'post_id', 'user_id', 'user_post'],
    ];

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

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

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

$post = new Post(['content'=>'another post..']);
// alternative
//$post = new Post(); $post->setContent('another post..');

$post->setComments([new Comment(['content'=>'another comment..'])]);
$post->setComments([new Comment(['content'=>'still another comment..'])], ['merge'=>true]);

$post->setAuthors([new User(['name'=>'bar'])], ['merge'=>true]);

// alternative
//$user = new User(['name'=>'bar']);
//$user->save();
//$post->assocAuthors([$user]);

// to simply dissociate from a many-to-many relationship
//$post->dissocAuthors([$user]);

$post->save(['withRelated'=>['comments','authors']]);

//$post->delete(['withRelated'=>true]);

$count = Post::count(['conditions'=>[/../]]);
$posts = Post::fetchAll(['withRelated'=>['comments','authors']]); // eager load of relationships, no N+1 problem

//foreach(Post::fetchAll() as $post) $post->getComments(); // lazy load of relationships, N+1 problem
  Files folder image Files  
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  /  src  
File Role Description
Files folder imagejs (1 file)
Files folder imagephp (1 file)
Files folder imagepython (2 files)

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

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

  Files folder image Files  /  src  /  python  
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  /  test  
File Role Description
Files folder imagejs (4 files)
Files folder imagephp (4 files)
Files folder imagepython (5 files)
  Accessible without login Plain text file dialectorm.sql Data Auxiliary data

  Files folder image Files  /  test  /  js  
File Role Description
  Accessible without login Plain text file Dialect.js Data Auxiliary data
  Accessible without login Plain text file MysqlDb.js Data Auxiliary data
  Accessible without login Plain text file out.txt Doc. Documentation
  Accessible without login Plain text file test.js Data Auxiliary data

  Files folder image Files  /  test  /  php  
File Role Description
  Plain text file Dialect.php Class Class source
  Accessible without login Plain text file out.txt Doc. Documentation
  Plain text file PDODb.php Class Class source
  Plain text file test.php Class Class source

  Files folder image Files  /  test  /  python  
File Role Description
  Accessible without login Plain text file Dialect.py Data Auxiliary data
  Accessible without login Plain text file MysqlDb.py Data Auxiliary data
  Accessible without login Plain text file out.txt Doc. Documentation
  Accessible without login Plain text file test.py Data Auxiliary data
  Accessible without login Plain text file __init__.py Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:26
This week:1
All time:10,067
This week:195Up

For more information send a message to info at phpclasses dot org.