rmtram/text-database

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Text-file based database.

dev-master 2015-12-22 09:00 UTC

This package is auto-updated.

Last update: 2021-10-21 13:40:09 UTC


README

Build Status

Sorry!! the application has been creating does not work.

Introduction

Create Table.

use Rmtram\TextDatabase\Schema\Builder;
use Rmtram\TextDatabase\Schema\Schema;

require __DIR__ . '/vendor/autoload.php';

Rmtram\TextDatabase\Connection::setPath('path/to/');

Builder::make()
    ->table('users', function(Schema $schema) {
    $schema->integer('id')->autoIncrement()->primary();
    $schema->string('name')->notNull();
    $schema->dateTime('created_at');
    $schema->dateTime('updated_at');
});

Builder::make()
    ->table('posts', function(Schema $schema) {
    $schema->integer('id')->autoIncrement()->primary();
    $schema->string('title')->notNull();
    $schema->string('description')->notNull();
    $schema->integer('user_id');
    $schema->dateTime('created_at');
    $schema->dateTime('updated_at');
});

Builder::make()
    ->table('comments', function(Schema $schema) {
    $schema->integer('id')->autoIncrement()->primary();
    $schema->text('comment')->notNull();
    $schema->integer('post_id');
    $schema->dateTime('created_at');
    $schema->dateTime('updated_at');
});

Create EntityManager

class UserEntityManager extends BaseEntityManager
{
    /**
     * @var string
     */
    protected $table = 'users';

    /**
     * @var string
     */
    protected $entity = User::class;

    /**
     * @var array
     */
    protected $hasMany = [
        'posts' => Post::class
    ];

}

class PostEntityManager extends BaseEntityManager
{
    /**
     * @var string
     */
    protected $table  = 'posts';

    /**
     * @var string
     */
    protected $entity = Post::class;

    /**
     * @var array
     */
    protected $belongsTo = [
        'user' => User::class
    ];

    /**
     * @var array
     */
    protected $hasMany = [
        'comments' => Comment::class
    ];
}

class CommentEntityManager extends BaseEntityManager
{
    /**
     * @var string
     */
    protected $table  = 'comments';

    /**
     * @var string
     */
    protected $entity = Comment::class;

    /**
     * @var array
     */
    protected $belongsTo = [
        'post' => Post::class
    ];
}

Create Entity

class User extends BaseEntity
{
    /**
     * @var int
     */
    public $id;

    /**
     * @var string
     */
    public $name;

    /**
     * @var \DateTime|string
     */
    public $created_at;

    /**
     * @var \DateTime|string
     */
    public $updated_at;

    /**
     * @var string
     */
    protected static $entityManager = UserEntityManager::class;
}

class Post extends BaseEntity
{
    /**
     * @var int
     */
    public $id;

    /**
     * @var string
     */
    public $title;

    /**
     * @var string
     */
    public $description;

    /**
     * @var int
     */
    public $user_id;

    /**
     * @var \DateTime|string
     */
    public $created_at;

    /**
     * @var \DateTime|string
     */
    public $updated_at;

    /**
     * @var string
     */
    protected static $entityManager = PostEntityManager::class;
}

class Comment extends BaseEntity
{
    /**
     * @var int
     */
    public $id;

    /**
     * @var string
     */
    public $comment;

    /**
     * @var int
     */
    public $post_id;

    /**
     * @var string|\DateTime
     */
    public $created_at;

    /**
     * @var string|\DateTime
     */
    public $updated_at;

    /**
     * @var string
     */
    protected static $entityManager = CommentEntityManager::class;
}

Create, Read, Update, Delete

Create and Update.

// Create
$user = new User();
$user->name = 'NickName'
$user->created_at = new DateTime();
$user->updated_at = new DateTime();
UserEntityManager::save($user);

// Update
$user = new User();
$user->id = 1; // primary key.
$user->name = 'NickName'
$user->created_at = new DateTime();
$user->updated_at = new DateTime();
UserEntityManager::save($user);

Read.

$users = UserEntityManager::find()->all();
foreach ($users as $user) {
    echo $user->id;
    echo $user->name;
}

// sort
$users = UserEntityManager::find()
    ->order(['name' => 'asc'])
    ->all();

// relation
$user = UserEntityManager::find()->first();
foreach ($user->posts as $post) {
    echo $post->title;
    foreach ($post->comments as $comment) {
        echo $comment->comment;
        echo $comment->user->name;
    }
}

Delete.

// all
UserEntityManager::delete();

// where
$userRepository = new UserRepository();
UserEntityManager::delete(['id' => 1])

// entity
$user = UserEntityManager::find()->first();
UserEntityManager::delete($user);