PHP code example of mattdwyercool / porm

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

    

mattdwyercool / porm example snippets


use PORM\Model;

final class Person
{
	use Model;

	public int $id;
	public string $name;
	public string $email;
}

final class Person
{
	public static function get( int $id ): ?static
	{
		$stmt = DB::select( <<<SQL
			SELECT
				p.`id`,
				p.`name`,
				p.`email`
			FROM `people` p
			WHERE p.`id` = :id;
			SQL,
			id: $id );

		return static::one( $stmt );
	}
}

final class Person
{
	/**
	 * @return static[]
	 */
	public static function all(): array
	{
		$stmt = DB::select( <<<SQL
			SELECT
				p.`id`,
				p.`name`,
				p.`email`
			FROM `people` p;
			SQL );

		return static::many( $stmt );
	}
}

final class Person
{
	private static function sql(): string
	{
		return <<<SQL
			SELECT
				p.`id`,
				p.`name`,
				p.`email`
			FROM `people` p;
			SQL;
	}

	public static function get( int $id ): ?static
	{
		$sql = static::sql();

		$stmt = DB::select( <<<SQL
			{$sql}
			WHERE p.`id` = :id;
			SQL,
			id: $id );

		return static::one( $stmt );
	}

	/**
	 * @return static[]
	 */
	public static function all(): array
	{
		$sql = static::sql();

		$stmt = DB::select( $sql );

		return static::many( $stmt );
	}
}

final class Post
{
	public int $id;

	private string $_tags;
	private string $_data;

	/** @var string[] */
	public array $tags;

	public object $data;


	public function __construct()
	{
		$this->tags = explode( ',', $this->_tags );

		$this->data = json_decode( $this->_data );
	}

	public static function get( int $id ): ?static
	{
		$stmt = DB::select( <<<SQL
			SELECT
				p.`id`,
				p.`tags` AS `_tags`,
				p.`data` AS `_data`
			FROM `posts` p
			WHERE p.`id` = :id;
			SQL,
			id: $id );

		return static::one( $stmt );
	}
}

final class Post
{
	public int $id;

	public string $title;

	public string $author_name;

	public static function get( int $id ): ?static
	{
		$stmt = DB::select( <<<SQL
			SELECT
				p.`id`,
				p.`title`,
				a.`name` AS `author_name`
			FROM `posts` p
			INNER JOIN `authors` a ON (a.`id` = p.`author_id`)
			WHERE p.`id` = :id;
			SQL,
			id: $id );

		return static::one( $stmt );
	}
}

return <<<SQL
	WITH x AS (
		SELECT
			a.`id`
		FROM `table_a` a

		UNION ALL

		SELECT
			b.`id`
		FROM `table_b` b
	)
	SELECT
		x.*
	FROM x
	SQL;

$sqlAbc = static::sqlAbc();
$sqlXyz = static::sqlXyz();

$stmt = DB::select( <<<SQL
	{$sqlAbc}
	WHERE ...

	UNION ALL

	{$sqlXyz}
	WHERE ...
	SQL );

$records = [
	...Abc::all(),
	...Xyz::all(),
];

use PORM\Relationships\BelongsTo;

public int $parent_id;

#[BelongsTo]
public Parent $parent;

use PORM\Relationships\HasOne;

#[HasOne]
public Child $child;

use PORM\Relationships\HasMany;

/** @var Child[] */
#[HasMany( Child::class )]
public array $children;