定制 rnr1721/le7-db-redbean 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rnr1721/le7-db-redbean

Composer 安装命令:

composer require rnr1721/le7-db-redbean

包简介

Redbean PHP that allow easy use of this ORM in any PHP project

README 文档

README

Allow easy use pagination, validation and normalisation in any PHP project that use Redbean PHP

Requirements

  • PHP 8.1 or higher.
  • Composer 2.0 or higher

Dependencies

All dependencies installs automatic by composer

What it can?

  • Easy-to-use models classes in some namespace
  • Pagination using Entify entity framework
  • Validation using rules (using Entify entity framework)
  • use arrow RedbeanPHP method calls instead static methods
  • Contain very simple lightweight SQL query builder

Installation

composer require rnr1721/le7-db-redbean

Testing

composer test

How it works?

After installing, you need decide where will be your models. For example in \Model. Any model is class, that contain entity rules. IMPORTANT: validation rules must contain information about all entity fields. Required fields is "label" and "validate". Any other fields is optional. All models must be extends of Model and implements ModelInterface same as in this example below:

Example of model (in namespace 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
            ]
        ];
    }

}

You can read more about entity field rules options on Entify project page https://github.com/rnr1721/le7-entify (validation etc). Here it should be said that the specific field option for this package, unlike other fields, is "unique", that must be true or false. Also, when saving entities the "hide" filter will be skipped.

This software is convenient to use along with a dependency container. In the container, you can pre-configure everything, and then conveniently and easily use it in your code using dependency injection (DI).

So, now we can use this model.

Basic usage

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

If you need multi-lingual validator messages, please send me translations for this projects (or make commit requests) and I add them: https://github.com/rnr1721/le7-validator

Multiple entities

Now we can try to work with arrays of entities. For example, we need to paginate SQL request or filter it after recieving.

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

Of course you can use tokens, something like this:

$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

About this knows all, but for same, I strongly recommend use bindings instead direct values!

// 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

Connection

You can easy add databases, and manage connections. Example:

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

rnr1721/le7-db-redbean 适用场景与选型建议

rnr1721/le7-db-redbean 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 05 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「database」 「component」 「redbean」 「le7」 「le7-framework」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rnr1721/le7-db-redbean 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rnr1721/le7-db-redbean 我们能提供哪些服务?
定制开发 / 二次开发

基于 rnr1721/le7-db-redbean 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 39
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 12
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-16