PHP code example of nrawe / wabi-orm

1. Go to this page and download the library: Download nrawe/wabi-orm 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/ */

    

nrawe / wabi-orm example snippets




use function WabiORM\{
    belongs_to,
    connect, 
    delete, 
    find_one, 
    has_many, 
    global_read, 
    global_write, 
    mysql, 
    save
};

$connect = connect(mysql($host, $db, $user, $pwd));

global_read($connect);
global_write($connect);

class User {
    public $id;

    public function posts() {
        return has_many(Post::class, $this);
    }
}

class Post {
    public $id;

    public $user_id;

    public $title;

    public $content;

    public function user() {
        return belongs_to(User::class, $this);
    }
}

$newPost = new Post();
$newPost->title = 'My first post';
$newPost->content = 'WabiORM put the fun back into database usage!';

$id = save($newPost);

$post = find_one(Post::class, $id);
$post->title = 'My first post (edited!)';

save($post);
delete($post);

$user = find_one(User::class, 1);

foreach ($user->posts() as $post) {
    echo $post->title, PHP_EOL;
}