PHP code example of assegaiphp / orm

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

    

assegaiphp / orm example snippets




use App\Entities\NoteEntity;
use Assegai\Orm\DataSource\DataSource;
use Assegai\Orm\DataSource\DataSourceOptions;
use Assegai\Orm\Enumerations\DataSourceType;
use Assegai\Orm\Support\OrmRuntime;

OrmRuntime::configure([
  'databases' => [
    'sqlite' => [
      'app' => [
        'path' => __DIR__ . '/storage/app.sqlite',
      ],
    ],
  ],
]);

$dataSource = new DataSource(new DataSourceOptions(
  name: 'app',
  type: DataSourceType::SQLITE,
  database: 'app',
));

$notes = $dataSource->getRepository(NoteEntity::class);

$note = $notes->create((object)[
  'title' => 'First note',
  'body' => 'Stored without a framework',
]);

$created = $notes->save($note);
$allNotes = $notes->find()->getData();

$notes = $dataSource->getRepository(NoteEntity::class);

$note = $notes->findOne([
  'where' => ['id' => 1],
  'relations' => ['author'],
  'hydrate' => true,
])->getData();



return [
  'databases' => [
    'sqlite' => [
      'app' => [
        'path' => 'storage/database/app.sqlite',
      ],
    ],
  ],
];



namespace App\Entities;

use Assegai\Orm\Attributes\Columns\Column;
use Assegai\Orm\Attributes\Columns\PrimaryGeneratedColumn;
use Assegai\Orm\Attributes\Entity;
use Assegai\Orm\Enumerations\DataSourceType;
use Assegai\Orm\Queries\Sql\ColumnType;

#[Entity(
  table: 'notes',
  dataSource: 'app',
  driver: DataSourceType::SQLITE,
)]
class NoteEntity
{
  #[PrimaryGeneratedColumn]
  public ?int $id = null;

  #[Column(type: ColumnType::VARCHAR, nullable: false)]
  public string $title = '';

  #[Column(type: ColumnType::TEXT, nullable: true)]
  public ?string $body = null;
}



namespace App\Entities;

use Assegai\Orm\Attributes\Entity;
use Assegai\Orm\Attributes\SqlEntityOptions;
use Assegai\Orm\Enumerations\DataSourceType;

#[Entity(
  table: 'audit_logs',
  dataSource: 'reporting',
  driver: DataSourceType::POSTGRESQL,
)]
#[SqlEntityOptions(schema: 'analytics')]
class AuditLogEntity
{
}



use App\Entities\NoteEntity;
use Assegai\Orm\DataSource\DataSource;
use Assegai\Orm\DataSource\DataSourceOptions;
use Assegai\Orm\Enumerations\DataSourceType;

$dataSource = new DataSource(new DataSourceOptions(
  entities: [],
  name: 'app',
  type: DataSourceType::SQLITE,
));

$dataSource->manager->query(<<<SQL
CREATE TABLE IF NOT EXISTS `notes` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `title` TEXT NOT NULL,
  `body` TEXT
)
SQL);

$notes = $dataSource->getRepository(NoteEntity::class);

$newNote = $notes->create([
  'title' => 'First note',
  'body' => 'Stored in SQLite',
]);

$notes->insert($newNote);

$allNotes = $notes->find()->getData();
$firstNote = $notes->findOne(['id' => 1])->getFirst();



namespace App\Notes;

use App\Entities\NoteEntity;
use Assegai\Core\Attributes\Injectable;
use Assegai\Orm\Attributes\InjectRepository;
use Assegai\Orm\Management\Repository;

#[Injectable]
class NotesService
{
  public function __construct(
    #[InjectRepository(NoteEntity::class)]
    private readonly Repository $notes,
  ) {
  }

  public function all(): array
  {
    return $this->notes->find()->getData();
  }
}