PHP code example of birkanoruc / simple-orm

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

    

birkanoruc / simple-orm example snippets


$config = [
'host' => '127.0.0.1',
'port' => 'your_db_port',
'dbname' => 'your_database',
'charset' => 'utf8mb4',
];

use Birkanoruc\SimpleOrm\Database;

$config = [
    'host' => '127.0.0.1',
    'port' => 'your_db_port',
    'dbname' => 'your_database',
    'charset' => 'utf8mb4',
];

$db = new Database($config);

namespace App\Models;

use Birkanoruc\SimpleOrm\Model;

class User extends Model
{
    protected $table = 'users';
}

$userModel = new User($db);
$users = $userModel->all();

$user = $userModel->first();

$user = $userModel->find(1);

$activeUsers = $userModel->where('active', '=', 1)->all();

$newUser = [
    'name' => 'John Doe',
    'email' => '[email protected]',
];

$userModel->create($newUser);

$userModel->update(1, [
    'name' => 'John Smith',
]);

$userModel->delete(1);

$userModel->destroy([1, 2, 3]);

public function profile()
{
    return $this->hasOne(Profile::class);
}

public function posts()
{
    return $this->hasMany(Post::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}

public function roles()
{
    return $this->belongsToMany(Role::class, 'user_roles');
}

$count = $userModel->count();
$sum = $userModel->sum('age');
$avg = $userModel->avg('salary');
$min = $userModel->min('age');
$max = $userModel->max('age');