PHP code example of breier / mykrorm

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

    

breier / mykrorm example snippets




class Session extends \Breier\MykrORM
{
  protected $token;
  protected $email;
  protected $startTime;

  protected function getDSN(): string
  {
    return 'pgsql:host=localhost;port=5432;dbname=test;user=test;password=1234';
  }

  public function __construct()
  {
    parent::__construct();

    $this->dbProperties = [
      'token' => 'CHAR(64) PRIMARY KEY',
      'email' => 'VARCHAR(64) NOT NULL',
      'start_time' => 'TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP',
    ];
  }

  public function setToken(string $value = ''): string
  {
    if (strlen($value) === 64) {
      return $this->token = $value;
    }
    $secret = 'my-app-hash-secret-123';
    $this->token = hash('sha256', "{$secret}-{$this->email}-" . microtime(true));
    return $this->token;
  }

  public function setEmail(string $value): string
  {
    $value = filter_var($value, FILTER_VALIDATE_EMAIL);
    return $this->email = $value;
  }

  public function setStartTime($value): DateTime
  {
    if ($value instanceof DateTime) {
      return $this->startTime = $value;
    }
    return $this->startTime = new \DateTime($value);
  }
}

  class Test extends MykrORM
  {
    protected function getDSN(): string
    {
      return 'pgsql:host=localhost;port=5432;dbname=test;user=test;password=1234';
      // return 'sqlite:messaging.sqlite3'; // local option ;)
    }
  }
  

  class Test extends MykrORM
  {
    public function test(): void
    {
      $this->getDBProperties()->keys()->join('/'); // 'token/email/start_time'
    }
  }
  

  class Test extends MykrORM
  {
    public function test(): void
    {
      $this->getConnection()->query('SELECT * FROM test');
    }
  }
  

  class Test extends MykrORM
  {
    protected $test = 1234;
    protected $other = "no-getter";
    public __construct()
    {
      $this->dbProperties = [
        'test' => 'INT NOT NULL PRIMARY KEY',
      ];
    }
  }
  print((new Test())->test); // 1234
  print((new Test())->other); // throws DBException property is not DB property!
  

  class Test extends MykrORM
  {
    protected $testName = 'test';
    public __construct()
    {
      $this->dbProperties = [
        'test_name' => 'CHAR(4) NOT NULL PRIMARY KEY',
      ];
    }
    public setTestName(string $value): string
    {
      $this->testName = $value;
    }
    public test(): void
    {
      $preparedStatement = $this->getConnection()->prepare("SELECT * FROM {$this->dbTableName}");
      $preparedStatement->execute();

      $likeThis = $preparedStatement->fetchObject(static::class);

      if (!empty($likeThis)) {
        print($likeThis->testName); // works because __set mapped 'test_name' to 'setTestName'
      }
    }
  }
  

  class Test extends MykrORM
  {
    public function test(): void
    {
      print(self::camelToSnake('anotherTestName')); // another_test_name
    }
  }
  

  class Test extends MykrORM
  {
    public function test(): void
    {
      printt(self::snakeToCamel('test_name')); // TestName
    }
  }
  

  $test = new Test();
  $test->testName = 'what';
  $test->create();
  

  $testModel = new Test();
  $test = $testModel->find(['test_name' => 'what']); // ExtendedArray
  $test->first()->element(); // Test Model instance (or null)
  $test->next()->element(); // Test Model instance of second row (or null)
  

  $test = (new Test())->find(['test_name' => 'what']);
  if ($test->count()) {
    $testModel = $test->first()->element();
    $testModel->testName = 'soap';
    $testModel->update(['test_name' => 'what']);
  }
  

  $test = (new Test())->find(['test_name' => 'soap']);
  if ($test->count()) {
    $testModel = $test->first()->element();
    $testModel->delete();
  }
  

  $test = new Test();
  $test->testName = 'soap';
  print($test->getProperties()); // {"test_name":"soap"}
  

  class Test extends MykrORM
  {
    protected $testName = 'test';
    public __construct()
    {
      $this->dbProperties = [
        'test_name' => 'CHAR(4) NOT NULL PRIMARY KEY',
      ];
    }
    public setTestName(string $value): string
    {
      $this->testName = $value;
    }
    public testUpdate(): void
    {
      $query = "UPDATE {$this->dbTableName} SET test_name = ? WHERE test_name = ?";
      $parameters = new ExtendedArray(['test_name' => 'newValue', 0 => 'oldValue']);
      $preparedStatement = $this->getConnection()->prepare($query);
      $this->bindIndexedParams($preparedStatement, $parameters);
      $preparedStatement->execute();
    }
  }
  

  class Test extends MykrORM
  {
    public function test(): void
    {
      $this->validateCriteria([]); // true
      $this->validateCriteria(['test_name' => null]); // true
      $this->validateCriteria(['test_name' => 'soap']); // true
      $this->validateCriteria(['test_non_existent' => 'soap']); // Throws DBException
    }
  }
  

  class Test extends MykrORM
  {
    public __construct()
    {
      $this->dbProperties = [
        'test_name' => 'CHAR(4) NOT NULL PRIMARY KEY',
      ];
    }
    public function test(): void
    {
      print($this->findPrimaryKey()); // {"as_db_field":"test_name","asProperty":"testName"}
    }
  }
  

  class Test extends MykrORM
  {
    ...
    public function test(): void
    {
      $this->dbTableName = 'different_test';
      $this->createTableIfNotExists(); // creates new table with same DB properties
      ...
    }
  }