marcojetson / freckle
基于 Doctrine DBAL 构建的极简 ORM,深受 Spot2 启发
v1.0
2016-06-22 20:31 UTC
Requires
- php: >=5.6.0
- doctrine/dbal: 2.5.*
Requires (Dev)
- phpunit/phpunit: 5.*
This package is not auto-updated.
Last update: 2024-09-14 19:36:40 UTC
README
Freckle 是基于 Doctrine DBAL 构建的 Object-Relational-Mapper。
Freckle 受 Spot2 启发。
目录
安装
使用 Composer 安装
composer require marcojetson/freckle
配置
您可以通过 Freckle\Manager 类获取连接。
$connection = Freckle\Manager::getConnection([ 'driver' => 'pdo_sqlite', ]);
实体
实体必须扩展 Freckle\Entity 并实现 definition 方法。
/** * @method int getId() * * @method string getTitle() * @method setTitle(string $title) * * @method string getBody() * @method setBody(string $body) */ class Post extends Freckle\Entity { public static function definition() { return [ 'table' => 'post', 'fields' => [ 'id' => ['integer', 'sequence' => true, 'primary' => true], 'title' => 'string', 'body' => 'string', ], ]; } }
定义
定义一个实体需要一个表名及其字段。字段由一个具有必定位置参数和可选命名参数的数组定义。
[
string $type,
mixed default=null, // default value, callables supported!
bool primary=false,
bool require=false,
bool|string sequence=false, // specify sequence name if required by database
]
生成
Freckle 可以为您生成实体。使用 Freckle\Connection::import() 自动为您的表生成映射。
foreach ($connection->generate() as $mapping) { file_put_contents($mapping->entityClass() . '.php', (string)$mapping); }
数据操作
使用映射器与您的实体交互。您可以使用之前创建的连接获取映射器。
$postMapper = $connection->mapper(Post::class);
插入
// create entity and insert $post1 = $postMapper->entity([ 'title' => 'Hello World', 'body' => 'My very first post', ]); $postMapper->insert($entity); // ...or do it in a single step $post2 = $postMapper->create([ 'title' => 'Lorem Ipsum', 'body' => 'My second post', ]);
更新
$post2->setTitle('Lorem ipsum dolor'); $postMapper->update($post2);
不确定是否是新的实体?那么请使用 Freckle\Mapper::save()。
删除
$postMapper->delete($post2);
检索
使用 Freckle\Mapper::find() 初始化查询
$query = $postMapper->find(['title like' => '% post']); // queries are lazy, keep attaching parts till ready $query->not('id', 1)->gte('score', 10); foreach ($query as $post) { echo $post->getName(), PHP_EOL; } // or retrieve a single result $postMapper->find(['id' => 1])->first();
Where 操作符
在使用 Freckle\Query::where() 或作为查询方法执行时,可以将 Where 操作符附加到字段上。
- eq, 等于, =
- not, 不等于, !=
- gt, 大于, >
- gte, 大于等于, >=
- lt, 小于, <
- lte, 小于等于, <=
- like
自定义操作符
通过扩展 Freckle\Operator 添加您自己的操作符。
class JsonExists extends Operator { public function __invoke(Query $query, $column, $value = null) { return 'jsonb_exists(' . $column . ', ' . $query->parameter($value) . ')'; } } Freckle\Operator::add('json_exists', JsonExists::class); $postMapper->find([ 'properties json_exists' => 'author', ]); // or use it as a method $postMapper->find()->json_exists('properties', 'author');
关系
支持相关实体检索。
/** * @method int getId() * * @method string getBody() * @method setBody(string $body) * * @method int getPostId() * @method setPostId(int $postId) * * @method Post getPost() */ class Comment extends Freckle\Entity { public static function definition() { return [ 'table' => 'comment', 'fields' => [ 'id' => ['integer', 'sequence' => true, 'primary' => true], 'body' => 'string', 'post_id' => 'integer', ], 'relations' => [ 'post' => ['one', Post::class, ['id' => 'this.id']], }, ], ]; } }
定义
与字段定义类似,定义一个关系需要在一个具有必定位置参数和可选命名参数的数组中。
[
string $type,
string $entityClass,
array $conditions,
string through=null, // "table.column" for many-to-many relations
string field='id', // related entity primary column
]