此包最新版本(1.4.0)的许可信息不可用。

1.4.0 2022-07-12 12:01 UTC

This package is auto-updated.

Last update: 2024-09-13 18:27:28 UTC


README

特性

  • 💾 简单地将纯PHP对象映射到数据库
  • ⛓ 直观的关联映射
  • 🔧 使用PHP8属性配置实体
  • 🧪 使用模拟数据库轻松测试 - 使用Leven数据库适配器
  • 🔤 自动映射表和列名
  • 🐌 自动懒加载和缓存
  • 🔝 支持自动递增主属性
  • 📁 所有属性存储在单个列中,以JSON编码

示例

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);