pulunomoe/datamapper

PHP 的超级简单的数据映射 ORM

v0.2 2020-12-30 06:11 UTC

This package is auto-updated.

Last update: 2024-09-14 16:50:25 UTC


README

PHP 的超级简单的数据映射 ORM。

要求

  • PHP 8+
  • PDO

功能

  • CRUD (哇!)
  • 更多即将到来(很快)

使用方法

1. 使用 composer 安装

composer require pulunomoe/datamapper

2. 创建你的实体类

<?php

// src/Entity/Car.php

namespace YourApp\Entity;

use Pulunomoe\DataMapper\Entity;
use Pulunomoe\DataMapper\EntityClass;
use Pulunomoe\DataMapper\Property;

#[Entity('tests')] // Put your table name here
class Car extends EntityClass
{
	#[Property('id')] // Put your column name here
	public int $id;   // "$id" is a special column for primary key

	#[Property('brand')]
	public string $brand;

	#[Property('model')]
	public string $model;
}

3. 调用 DataMapper

<?php

// Initialize the data mapper by passing a PDO instance and the entity class name
$dm = new DataMapper($pdo, Car::class);

// Retrieve all cars
$dm->findAll();

// Retrieve all cars by brand
$dm->findAllBy('brand', 'Danke Motoren Werke');

// Retrieve a single car with the id = 1
$dm->findOne(1);

// Save a car to the database
$car = new Car();
$car->brand = 'Honyabishi';
$car->model = 'Super 9001';
$car = $dm->create($car);

// Update a car
$car->model = 'Super 9001 Mark II Type R GT-MAXXX';
$car = $dm->update($car);

// Delete a car with the id = 1
$dm->delete(1);

API

  • 查找全部

findAll(string $orderBy = '', bool $desc = false, int $limit = 10, int $offset = 0): array

  • 按条件查找全部

findAllBy(string $column, string $value, string $orderBy = '', bool $desc = false, int $limit = 10, int $offset = 0): array

  • 查找单个

function findOne(int $id): ?EntityClass

  • 创建

create(EntityClass $object): EntityClass

  • 更新

update(EntityClass $object): EntityClass

  • 删除

function delete(int $id): void

更新日志

  • v0.1 : 初始版本
  • v0.2 : 添加了排序、限制和按条件查找全部

无耻的广告

喜欢这个库吗?请我喝杯咖啡请我吃份甜点