PHP code example of asko / orm

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

    

asko / orm example snippets


use Asko\Orm\BaseModel;
use Asko\Orm\Drivers\MysqlDriver;

/**
 * @template T
 * @extends BaseModel<T>
 */
class Model extends BaseModel
{
  public function __construct()
  {
    parent::__construct(new MysqlDriver(
      host: "",
      name: "",
      user: "",
      password: "",
      port: 3006
    ));
  }
}

use Asko\Orm\Column;

/**
 * @extends Model<User>
 */
class User extends Model
{
  protected static string $_table = "users";
  protected static string $_identifier = "id";

  #[Column]
  public int $id;

  #[Column]
  public string $name;

  #[Column]
  public string $email;
}

$user = (new User)
  ->query()
  ->where('id', '=', 1)
  ->first();

$user = (new User)->find(1);

(new User)->query()->where('id', '=', 1);
// or
(new User)->query()->where('id', '>', 1);

(new User)->query()->where('id', '=', 1)->andWhere('email', '=', '[email protected]');

(new User)->query()->where('id', '=', 1)->orWhere('email', '=', '[email protected]');

(new User)->query()->orderBy('id', 'asc');

(new User)->query()->limit(10);

(new User)->query()->offset(10);

(new User)->query()->join('posts', 'posts.user_id', '=', 'users.id');

(new User)->query()->leftJoin('posts', 'posts.user_id', '=', 'users.id');

(new User)->query()->rightJoin('posts', 'posts.user_id', '=', 'users.id');

(new User)->query()->innerJoin('posts', 'posts.user_id', '=', 'users.id');

(new User)->query()->outerJoin('posts', 'posts.user_id', '=', 'users.id');

(new User)->query()->raw('WHERE id = ?', [1]);

(new User)->query()->get();

(new User)->query()->first();

(new User)->query()->last();

$user = new User;
$user->name = "John Smith";
$user->email = "[email protected]"
$user->store();

$user = (new User)->find(1);
$user->name = "John Doe";
$user->store();

$user = (new User)->find(1);
$user->delete();