Componette

Componette

modul-is

modul-is / orm v2.2.2

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.

For usage and examples refer to quickstart.

Readonly

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

#[\ModulIS\Attribute\ReadonlyProperty]
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(string $name, $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(string $name, $value): string
	{
		if($value instanceof \SplFileInfo)
		{
			$value = $value->getPathname();
		}
		else
		{
			throw new \ModulIS\Exception\InvalidArgumentException("Invalid type for column '{$name}' - 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'
price-tag-2-line

Badges

guide-fill

Dependencies

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