PHP code example of wtframework / orm

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

    

wtframework / orm example snippets


use WTFramework\ORM\Model;

class User extends Model {}

class User extends Model
{
  public const TABLE = 'x_users';
}

class User extends Model
{
  public const PRIMARY_KEY = 'id';
}

class User extends Model
{
  public const CONNECTION = 'mirror';
}

$user = User::create(['user_id' => 1]);

$user = User::get(1);

$user = User::get([
  'id1' => 1,
  'id2' => 2,
]);

$user->exists();

$user = User:::: 'id2' => 2,
]);

$user->name = 'Michael';

$user->save(['user_role_id' => 1]);

$user->name = 'Michael';

$user->refresh();

$user->name = 'Michael';

$fresh = $user->fresh();

$users = User::where('user_role_id', 1)->all();

User::select();
User::insert();
User::replace();
User::update();
User::delete();
User::truncate();

use WTFramework\ORM\Model;
use WTFramework\ORM\Relationships\BelongsTo;
use WTFramework\ORM\Relationships\Has;
use WTFramework\ORM\Relationships\HasMany;
use WTFramework\ORM\Relationships\HasManyThrough;
use WTFramework\ORM\Relationships\HasThrough;

class User extends Model
{

  public function role(): BelongsTo
  {
    return $this->belongsTo(UserRole::class);
  }

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

  public function revisions(): HasMany
  {
    return $this->hasMany(UserRevision::class);
  }

  public function owner(): HasOneThrough
  {
    return $this->hasOneThrough(Owner::class, UserOwner::class);
  }

  public function permissions(): HasManyThrough
  {
    return $this->hasManyThrough(Permission::class, UserPermission::class);
  }

}

class User extends Model
{

  public const PRIMARY_KEY = 'id';

  public function role(): BelongsTo
  {
    return $this->belongsTo(UserRole::class, local_key: 'role_id');
  }

  public function profile(): Has
  {
    return $this->has(Profile::class, foreign_key: 'user_id');
  }

  public function revisions(): HasMany
  {
    return $this->hasMany(UserRevision::class, foreign_key: 'user_id');
  }

  public function owner(): HasOneThrough
  {
    return $this->hasOneThrough(Owner::class, UserOwner::class, foreign_key: 'owner_id', pivot_local_key: 'user_id');
  }

  public function permissions(): HasManyThrough
  {
    return $this->hasManyThrough(Permission::class, UserPermission::class, foreign_key: 'permission_id', pivot_local_key: 'user_id');
  }

}

$role = $user->role->name;

$user->clearCache();

$revisions = $user->revisions()->orderBy('created')->all();

User::eager('revisions')->all();

class User extends Model
{

  public const EAGER = ['revisions'];

  public function revisions(): HasMany
  {
    return $this->hasMany(UserRevision::class);
  }

}

User::lazy('revisions')->where('user_id', 1)->get();