Componette

Componette

modul-is

modul-is / orm

Simple lightweight ORM

download-cloud-line composer require modul-is/orm

badge-version badge-licence badge-stars badge-issues

Licence

This repository is an overhaul of YetORM under MIT licence. Original fork kravcik/core will not be maintained for Nette3. You can find the unfinished PR for YetORM and Nette 3 here.

Abstract

This is a hybrid of a simple scalable database layer with ORM principles.

We are using entities as data objects for structured work (forms, details, routing, etc.).

class TestEntity extends \ModulIS\Entity
{
	public int $a;

	public ?string $b;

	public \Nette\Utils\DateTime $c;
}

It is also possible to make a repository call $repository->getTable() and inject it into Ublaboo/Datagrid.

$grid = new \Ublaboo\DataGrid\DataGrid;

$grid->setDataSource($this->TestRepository->getTable()
	->select('a, b')
	->where('b IS NOT NULL');

For some edge-cases we are using $repository->query() with custom SQL.

$this->TestRepository->query("SELECT `c` FROM `test` WHERE (`b` IS NOT NULL)")->fetch();

Readonly

The Readonly attribute can be used for properties that should not be written into, for example columns with auto increment.

#[\ModulIS\Attribute\Readonly]
public int $id;

Special types & behavior

  • array - stored in database as json
  • bool - stored in database as int
  • \Nette\Utils\DateTime - save and load \Nette\Utils\DateTime
Input Entity Nullable Output
int(1) int ✓ || ✗ int(1)
int(0) int ✓ || ✗ int(0)
string("0") int ✓ || ✗ Exception
float(1.0) float ✓ || ✗ float(1.0)
float(0.0) float ✓ || ✗ float(0.0)
string("0.0") float ✓ || ✗ Exception
string("a") string ✓ || ✗ string("a")
string("") string ✓ || ✗ string("")
array(["a" => "b"]) array ✓ || ✗ string("{"a":"b"}")
array([]) array ✓ || ✗ string("[]")
string("{"a":"b"}") array ✓ || ✗ Exception
string("[]") array ✓ || ✗ Exception
bool(false) bool ✓ || ✗ bool(false)
string("false") bool ✓ || ✗ Exception
DateTime("2021-01-01 12:34:56") DateTime ✓ || ✗ string("2021-01-01 12:34:56")
string("2021-01-01 12:34:56") DateTime ✓ || ✗ Exception
int(0) float || string || array || bool || DateTime ✓ || ✗ Exception
float(0.0) int || string || array || bool || DateTime ✓ || ✗ Exception
string("") int || float || array || bool || DateTime ✓ || ✗ Exception
array([]) int || float || string || bool || DateTime ✓ || ✗ Exception
bool(false) int || float || string || array || DateTime ✓ || ✗ Exception
null int || float || string || array || bool || DateTime null
null int || float || string || array || bool || DateTime Exception

Custom types

You can also use custom types, you just have to create a class that extends \ModulIS\Datatype\Datatype and implements both of its static functions:

  1. input($value) - save logic (conversion into a database-compatible type)
  2. output($value) - read logic (conversion back into the original type)
class File extends \ModulIS\Datatype\Datatype
{
	public static function input($value): string
	{
		if($value instanceof \SplFileInfo)
		{
			$value = $value->getPathname();
		}
		else
		{
			throw new \ModulIS\Exception\InvalidArgumentException("Instance of '\SplFileInfo' expected, '" . get_debug_type($value) . "' given.");
		}

		return $value;
	}


	public static function output($value): self
	{
		$value = new self(new \SplFileInfo($value));

		return $value;
	}
}

Then you can use your type with a property just like all the usual types.

public \App\Datatype\File $file;

Just make sure the data is always wrapped in the specified class to avoid errors.

$entity->file = new \App\Datatype\File(new \SplFileInfo('../app/Datatype/File.php'));

bdump($entity->file); //App\Datatype\File(value: SplFileInfo(path: '../app/Datatype/File.php'));
bdump($entity->file->value->getFilename()); //'File.php'

No release at this moment. Try to create first one.

bar-chart-fill

Statistics

download-cloud-fill
3916
star-fill
9
bug-fill
1
flashlight-fill
73d
price-tag-2-line

Badges

guide-fill

Dependencies

php (^8.0)
Componette Componette felix@nette.org