PHP code example of romagny13 / micro-db

1. Go to this page and download the library: Download romagny13/micro-db library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

romagny13 / micro-db example snippets


$settings = [
    'dsn' =>"mysql:host=localhost;dbname=blog",
    'username'=>'root',
    'password' =>''
];
Db::setConnectionStringSettings($settings['dsn'],$settings['username'],$settings['password']);

Db::setTableAndColumnStrategy('[',']');

$posts = Db::getInstance()
    ->select('id','title','content','user_id')
    ->from('posts')
    ->where(Condition::op('user_id',1))
    ->orderBy('title')
    ->limit(10)
    ->fetchAll();

class Post { }

$posts = Db::getInstance()
    ->select('posts.id','title','content','user_id','users.id','users.username')
    ->from('posts','users')
    ->where('user_id=1')
    ->orderBy(Sort::desc('title'),'content desc')
    ->limit(2,10)
    ->fetchAll(Post::class);

$queryString = Db::getInstance()->select('posts.id','title','content','user_id','users.id','users.username')
    ->from('posts','users')
    ->where('user_id=1')
    ->orderBy(Sort::desc('title'),'content desc')
    ->limit(2,10)
    ->build();

var_dump($queryString);

$success = Db::getInstance()
    ->insert_into('posts')
    ->columns('title','content','user_id')
    ->execute(['my title','my content',1]);

$success = Db::getInstance()
    ->insert_into('posts')
    ->columns('title','content','user_id')
    ->values('my title','my content',1)
    ->execute();

$id = Db::getInstance()->lastInsertId();

$success = Db::getInstance()
    ->update('posts')
    ->set([
        'title'=>'new title',
        'content' => 'new content'
    ])
    ->where(Condition::op('id',1))
    ->execute();

$success = Db::getInstance()
    ->delete_from('posts')
    ->where(Condition::op('id',1))
    ->execute();

$posts = Db::getInstance()
    ->query('select * from posts')
    ->fetchAllWithClass(Post::class);

$posts = Db::getInstance()
    ->prepare('select * from posts where id=:id')
    ->setParam(':id',1)
    ->fetchObject(Post::class);

$success = Db::getInstance()
    ->prepare('insert into posts (title,content,user_id) values (?,?,?)')
    ->execute(['My title', 'My content',2]);

$id = Db::getInstance()->lastInsertId();

$success = Db::getInstance()
    ->prepare('insert into posts (title,content,user_id) values (:title,:content,:user_id)')
    ->setParam(':title','My title')
    ->setParam(':content','My content')
    ->setParam(':user_id',2)
    ->execute();

$id = Db::getInstance()->lastInsertId();

use \MicroPHP\Db\Model;

class PostModel extends Model
{
    public function __construct()
    {
        $this->table = 'posts';
    }
}

use \MicroPHP\Db\Model;

class PostModel extends Model
{
    public function __construct()
    {
        $this->table = 'posts';
        $this->columns = ['id','title','content','user_id'];
    }
}

$posts = PostModel::all();

$posts = PostModel::all(10);

$posts = PostModel::all(2,10);

$posts = PostModel::where(Condition::op('user_id',1)->_or_(Condition::op('user_id',2)));

$posts = PostModel::where('user_id=1 or user_id=2');

$posts = PostModel::where('user_id=1 or user_id=2',2,10);

$post = PostModel::find(Condition::op('id',1));

$success = PostModel::create([
    'title' => 'My title',
    'content' => 'My content',
    'user_id' => 1
]);

$success = PostModel::update([
    'title' => 'My new title',
    'content' => 'My new content'
],Condition::op('id',1));

$success = PostModel::delete(Condition::op('id',1));

class UserModel extends Model
{
    public function __construct()
    {
        $this->table = 'users';
    }
}

class CategoryModel extends Model
{
    public function __construct()
    {
        $this->table = 'categories';
    }
}

class PostModel extends Model
{
    public function __construct()
    {
        $this->table = 'posts';
        $this->columns = ['title','content'];

        // relations
        $this->addRelation('users',['user_id' => 'id'],UserModel::class, 'user');
        $this->addRelation('categories',['category_id' => 'id'],CategoryModel::class, 'category');
    }
}

$post = PostModel::find(Condition::op('id',1));