doomy / ormtopus
Requires
- php: ^8.3
- dibi/dibi: *
- doomy/entitycache: >= 1.0.0
- doomy/repository: ^7.0.0
Requires (Dev)
- doomy/testing: ^1.0.2
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^11.2
- symplify/easy-coding-standard: ^12.3
README
simple ORM库,与Nette框架配合使用,具有实体缓存以提高性能。
先决条件
为了使orm工作,首先需要建立工作dibi连接。在您的本地配置文件(local.neon
)中,您应按照以下类似方式设置连接:
parameters: dibi: host: localhost username: db_username password: db_password database: db_schema
然后,在您的全局配置文件(common.neon
/ config.neon
)中设置以下服务
services: connection: Dibi\Connection(%dibi%) entityFactory: Doomy\Repository\EntityFactory(@connection) repoFactory: Doomy\Repository\RepoFactory(@connection, @entityFactory) entityCache: Doomy\EntityCache\EntityCache data: Doomy\Ormtopus\DataEntityManager(@repoFactory, @entityCache)
实体模型
对于您计划与之合作的每个实体,都需要手动创建相应的PHP实体模型类。下面是示例
<?php namespace App\Client\Model; use Doomy\Repository\Model\Entity; use Doomy\Repository\TableDefinition\Attribute\Column\Identity; use Doomy\Repository\TableDefinition\Attribute\Column\PrimaryKey; use Doomy\Repository\TableDefinition\Attribute\Table; #[Table('test_table')] class Client extends Entity { #[PrimaryKey] #[Identity] private ?int $id; public function __construct(?int $id = null) { $this->id = $id; } public function getId(): ?int { return $this->id; } }
从版本3.0.0开始,无需指定列或列属性为大写。
用法
通过将DataEntityManager
服务注入到您的演示者或所需位置来使用orm,如下所示
final class DashboardPresenter extends Nette\Application\UI\Presenter { private $data; public function __construct(DataEntityManager $data) { $this->data = $data; parent::__construct(); } public function renderIndex() { $this->template->clients = $this->data->findAll(Client::class); } }
数据检索方法
findById(string $entityClass, int $entityId)
根据其id查找单个实体。
findOne(string $entityClass, array|string $where = [])
查找并返回$where
中满足条件的第一个实体。以下是如何组成$where
数组的说明。
findAll(string $entityClass, array|string $where = [])
查找并返回给定类中所有实体(如果指定了where,则返回所有满足条件的实体)的数组。以下是如何组成$where
数组的说明。
数据操作方法
[已弃用]
create(string $entityClass, array $values)
从方法参数中值的关联数组创建给定实体类的实例。
示例:$clientEntity = $this->data->create(Client::class, ['name' => 'Microsoft', 'ADDRESS' => 'Tasmania']);
注意:自版本3.0.0以来,创建新实体的首选方式现在是通过其构造函数
$clientEntity = new Client(id: 123);
此操作不会将实体保存到数据库中
deleteById(string $entityClass, int $id)
根据提供的id从数据库中删除实体
delete(string $entityClass, array|string $where)
删除数据库中满足提供的所有条件的实体。以下是如何组成$where
数组的说明。
组成where条件
基本上有两种条件组装风格。
$where作为简单的字符串值
您可以使用整个条件,您本来会使用它在SQL语句中作为单个字符串
$client = $this->data->findOne(Client::class, 'id > 15 AND address = 'New York')
$where作为关联数组
条件也可以指定为关联数组。请看以下示例
$client = $this->data->findOne( Client::class, ['id' => 15, 'address' == 'New York'] )`
通常,多个条件将被视为通过AND子句连接。因此,关联示例在先前的示例中等同于简单的字符串条件。
在关联的$where中使用LIKE表达式
$client = $this->data->findOne(Client::class, ['name' => '~Micros']);
将与以下内容相同
$client = $this->data->findOne(Client::class, "name LIKE '%Mircros%'");
可以使用单个字符串$where条件实现更复杂的LIKE条件。
在关联的$where中设置条件
$clients = $this->data->findAll(Client::class, ['id' => [1, 2, 3]]);
等于
$clients = $this->data->findAll(Client::class, "WHERE id IN(1, 2, 3)");