Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leven-framework/orm
๐๏ธ map pure PHP objects to a database; configure entities with PHP8 attributes, intuitive relationship mapping
https://github.com/leven-framework/orm
abstraction database mapper mapping object-relational-mapper orm php php-attributes php8 relations
Last synced: 13 days ago
JSON representation
๐๏ธ map pure PHP objects to a database; configure entities with PHP8 attributes, intuitive relationship mapping
- Host: GitHub
- URL: https://github.com/leven-framework/orm
- Owner: leven-framework
- Created: 2021-12-28T10:02:25.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-07-13T13:43:32.000Z (over 2 years ago)
- Last Synced: 2024-12-20T03:02:52.809Z (about 2 months ago)
- Topics: abstraction, database, mapper, mapping, object-relational-mapper, orm, php, php-attributes, php8, relations
- Language: PHP
- Homepage:
- Size: 103 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Leven ORM
## Features
- ๐พ easily map pure PHP objects to a database
- โ intuitive relationship mapping
- ๐ง configure entities with PHP8 attributes
- ๐งช easily testable with a mock database - uses [Leven Database Adapter](https://github.com/leven-framework/dba-common)
- ๐ค automatic table and column name mapping
- ๐ automatic eager loading and caching
- ๐ support for auto-incrementing primary props
- ๐ all props are stored JSON-encoded in a single column## Example
```php
require 'vendor/autoload.php';$repo = new \Leven\ORM\Repository(
new \Leven\DBA\MySQL\MySQLAdapter(
database: 'example',
user: 'username',
password: 'password',
)
);(new \Leven\ORM\RepositoryConfigurator($repo))
->scanEntityClasses();class Author extends \Leven\ORM\Entity {
#[\Leven\ORM\Attribute\PropConfig(primary: true)]
public int $id;
public function __construct(
public string $name;
){}
}class Book extends \Leven\ORM\Entity {
#[\Leven\ORM\Attribute\PropConfig(primary: true)]
public int $id;
public function __construct(
// this defines that each Book must belong to an Author
public Author $author;
// we can provide rules for the Book's title
#[\Leven\ORM\Attribute\ValidationConfig(notEmpty: true, maxLength: 256)]
public string $title;
// store this prop in a separate column, so we can search for entities by it
#[\Leven\ORM\Attribute\PropConfig(index: true)]
public string $isbn;
// when storing or reading to the db, we'll use a converter to convert this prop to/from a scalar value
#[\Leven\ORM\Attribute\PropConfig(converter: \Leven\ORM\Converter\DateTimeStringConverter::class)]
public DateTime $releaseDate;
){}
}$john = new Author('John Doe');
$example = new Book($author, 'Example Book', '123456789', new DateTime('2021-01-01'));
$repo->store($john, $example);// later...
$author = $repo->get(Author::class, 1); // get author with id 1
$books = $repo->findChildrenOf($author, Book::class)->get();$book = $repo->find(Book::class)->where('isbn', '123456789')->getFirst();
$book->title = 'New Title';
$repo->update($book);
```