rnr1721 / le7-db-redbean
Redbean PHP,允许在任意PHP项目中轻松使用此ORM
1.0.8
2023-12-03 17:57 UTC
Requires
- php: >=8.1
- gabordemooij/redbean: ^5.7
- rnr1721/le7-entify: ^1.0
Requires (Dev)
- phpunit/phpunit: ^10.0
- vimeo/psalm: ^5.7
Replaces
README
允许在任意使用Redbean PHP的PHP项目中轻松使用分页、验证和规范化
要求
- PHP 8.1或更高版本。
- Composer 2.0或更高版本
依赖项
所有依赖项将由Composer自动安装
- RedbeanPHP (https://github.com/gabordemooij/redbean)
- Entify Entity Framework (https://github.com/rnr1721/le7-entify)
它能做什么?
- 在某个命名空间中提供易于使用的模型类
- 使用Entify实体框架进行分页
- 使用规则进行验证(使用Entify实体框架)
- 使用箭头调用RedbeanPHP方法而不是静态方法
- 包含非常简单轻量级的SQL查询构建器
安装
composer require rnr1721/le7-db-redbean
测试
composer test
它是如何工作的?
安装后,您需要决定模型将放在哪里。例如,在\Model中。任何模型都是一个包含实体规则的类。重要:验证规则必须包含所有实体字段的信息。必填字段是"label"和"validate"。其他字段是可选的。所有模型都必须扩展Model并实现ModelInterface,就像以下示例一样
模型示例(在命名空间Model中)
<?php declare(strict_types=1); namespace Model; use Core\Entify\Interfaces\ModelInterface; use Core\Database\Redbean\Model; class Contact extends Model implements ModelInterface { public function getRules(): array { return [ 'id' => [ 'label' => 'ID', 'validate' => '' ], 'name' => [ 'label' => 'Name', 'validate' => '' ], 'lastname' => [ 'label' => 'Lastname', 'validate' => '' ], 'another' => [ 'label' => 'Another field', 'validate' => 'required|minlength:1', 'unique' => true // This field must be unique ] ]; } }
您可以在Entify项目页面上阅读有关实体字段规则选项的更多信息 https://github.com/rnr1721/le7-entify(验证等)。这里应该指出的是,与其它字段不同,本包特定字段选项为"unique",必须为真或假。另外,在保存实体时将跳过"hide"过滤器。
此软件方便与依赖容器一起使用。在容器中,您可以预先配置一切,然后方便地在代码中使用依赖注入(DI)轻松地使用它。
因此,现在我们可以使用此模型。
基本用法
use Core\Database\Redbean\Db; use Core\Database\Redbean\DbConn; use Core\Entify\Entification; use Core\Entify\RulesLoaderClass; use Core\Database\Redbean\Drivers\DbSql; // Create rules loader for classes, and set namespace for models $loader = new RulesLoaderClass('\\Model\\'); // Create instance of Entify framework $entification = new Entification($loader); // Now, create array with parametres // But you can configure DB driver with methods if need $connectionArray = [ 'namespace' => '\\Model\\', // Importsnt! Namespace for models 'driver' => 'mysql', // mysql, pgsql, curbid 'host' => 'localhost', // Db host 'port' => '3306', // Your Db port 'user' => 'user', 'name' => 'database', 'pass' => '123' ]; // Now we create driver $driver = new DbSql($connectionArray); // Create object that connect, disconnect and switch between DBs $connection = new DBConn($driver, $entification); // Create database object wrapper $db = new Db($connection); // Code above you can run in DI container so it not scarry :) // Now we can use non-static Redbean methods with $db object $bean = $db->dispense('contact'); // See in model class above $bean->name = 'John'; $bean->lastname = 'Doe'; $bean->another = ''; try { // Now this make invalidArgumentException because in rules another is // required field $db->store($bean); } catch (\InvalidArgumentException $e) { // Get errors $errors = $bean->getErrors(); } // See the errors if (isset($errors)) { print_r($errors); } // But now, we try to make correct saving $bean->another = '777'; $db->store($bean);
如果您需要多语言验证器消息,请发送此项目的翻译给我(或发起提交请求),我将添加它们:https://github.com/rnr1721/le7-validator
多个实体
现在我们可以尝试处理实体数组。例如,我们需要在接收到数据后对SQL请求进行分页或过滤。
use Core\Database\Redbean\EntificationSql; use Core\Database\Redbean\Db; use Core\Database\Redbean\DbConn; use Core\Entify\Entification; use Core\Entify\RulesLoaderClass; use Core\Database\Redbean\Drivers\DbSql; $loader = new RulesLoaderClass('\\Model\\'); $entification = new Entification($loader); $connectionArray = [ 'namespace' => '\\Model\\', // Importsnt! Namespace for models 'driver' => 'mysql', // mysql, pgsql, curbid 'host' => 'localhost', // Db host 'port' => '3306', // Your Db port 'user' => 'user', 'name' => 'database', 'pass' => '123' ]; $driver = new DbSql($connectionArray); $connection = new DBConn($driver, $entification); $db = new Db($connection); $entificationSql = new EntificationSql($loader, $db); // Now we end DI container part. And now we can use repository entities // Get repository data provider // You can also set bindings and custom query $provider = $entificationSql->getDataProvider('contact'); // Case1 - get paginated result: // Per page 5 items, and current page 1 $entity = $provider->paginate(5, 1)->getEntity(); $data = $entity->export(); // Get data as array $info = $entity->getInfo(); // Get pagination data and rules info // Case2 - get paginated custom result $entity = $provider->paginate(5, 1)->select()->from()->where('id = 1')->getEntity(); $data = $entity->export(); // Get data as array $info = $entity->getInfo(); // Get pagination data and rules info
当然,您可以使用令牌,例如这样
$bindings = [1,2,3]; $provider = $entificationSql->getDataProvider('contact',$bindings); $entity = $provider->paginate(5, 1)->select()->from()->where(' id = ? OR id = ? OR id = ? ')->getEntity(); $data = $entity->export(); // Get data as array $info = $entity->getInfo(); // Get pagination data and rules info
关于这一点,所有都知道,但出于同样的原因,我强烈建议使用绑定而不是直接值!
// Unpaginated result $entity = $provider->select()->from()->where('id = 1')->getEntity(); $data = $entity->export(); // Get data as array $info = $entity->getInfo(); // Empty pagination data and rules info
连接
您可以轻松添加数据库并管理连接。示例
// We have already created $db, see above // Switch to another (in this case Sqlite) database $driver = new DbSqlite(['path'=>'./db.sqlite']); $db->getConntection()->switchDatabase($driver,'mydb2'); // Get connection status (after use it), bool $status = $db->getConnection()->isConnected(); // Disconnect from Db $db->getConnection()->disconnect();