承接 j0113/odb 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

j0113/odb

Composer 安装命令:

composer require j0113/odb

包简介

Object Oriented database wrapper for PHP. Designed to be(come) compatible with multiple database. Queries are written in PHP and tables are connected to models.

README 文档

README

Object-oriented database wrapper for PHP. Designed to be(come) compatible with multiple database. Queries are written in PHP and tables are connected to models.

Prerequisites/Requirements

  • PHP >=7.4 (for now)
  • A MySQL database (with tables)
  • PDO

Installation

J0113/ODB can be installed using composer.

composer require j0113/odb *

Getting started

1. Database connection

The first thing you want to do is set up the connection to your database. This will setup the database connection for the whole application.

use J0113\ODB\PDODatabase;
PDODatabase::connect("server.localhost", "myusername", "mypassword", "mydatabase");

2. Mapping a model

All models must extend J0113\ODB\PDODatabaseObject.

use J0113\ODB\PDODatabaseObject;
class User extends PDODatabaseObject
{
    // First you may define a table (defaults to the className)
    protected const TABLE = "users";

    // Than some fields:
    protected ?string $username = null;
    protected ?string $firstname = null;
    protected ?string $lastname = null;
    
    // Maybe some relations (more about that later)
    protected const RELATIONS = [
        "orders" => ["toOne", "Order", "user_id", "id"],
    ];
    
    // And getters and setters
    public function getUsername() : ?string
        ... 
}

That's it, now you can access your database from PHP in an object-oriented way.

Usage

1. Inserting

$user = new User();
$user->setFirstname("John");
$user->setLastname("Smith");
$user->setUsername("john_smith");

$user->save();

echo $user->getId();

2. Retrieving data

To get data you must use the J0113\ODB\QueryBuilder, if more databases are supported queries will be supported.

use J0113\ODB\QueryBuilder;

/**
 * Get some users and display info
 * @param QueryBuilder
 * @return User[]|null
 */
$users = User::get(
    (new QueryBuilder())
        ->where("firstname", "John")
        ->andWhere("lastname", "Smith")
        ->limit(5)
);

if (!empty($users)){
    foreach ($users as $user){
        echo "Hello " . $user->getFirstname() . "!\n";
    }
}

/**
 * Get one user
 * @param QueryBuilder
 * @return User|null
 */
$user = User::getOne(
    (new QueryBuilder())->where("username", "oliver12345")
);

3. Updating a row

To update data the proces is about the same, just leave the ID as is and use save().

use J0113\ODB\QueryBuilder;

/**
 * Get one user
 * @param QueryBuilder
 * @return User|null
 */
$user = User::getOne(
    (new QueryBuilder())->where("username", "oliver12345")
);
$user->setFirstname("Oli");
$user->save();

Relations

One issue with mapping a database to PHP objects is when relations come into play. ODB uses takes a JIT approach, the related object is 'accessible' from PHP but only when it gets accessed the query will get executed.

Releations are defined in the model object using the protected const RELATIONS array, relations can be toOne or toMany.

use J0113\ODB\PDODatabaseObject;
use J0113\ODB\QueryBuilder;
class User extends PDODatabaseObject
{
    /**
     * Stores all the relations, these are read only and results in more than one query IF the relation is queried.
     * Must be an sub array with "key" => ["type", "class", "column", "property"].
     * - Key: is under what $obj->key the relation can be accessed.
     * - Type: can be toOne (this-1) or toMany (this-multiple)
     * - Class: the class it should be connected to (must be an child of PDODatabaseObject)
     * - Column: the column it should search in
     * - Property: the value to match against, is a property (or variable) of the current object
     *
     * SQL will generated by:
     * - LIMIT: Type
     * - FROM: Class
     * - WHERE: column = this->property
     *
     * protected const RELATIONS = ["customer" => ["toOne", "Model\User", "id", "user_id"] ];
     * $order->customer = Model\User(...)
     *
     * Tip: Use the PHPDoc '@ property' to let the IDE know about the relation.
     *
     * @var array
     */
    protected const RELATIONS = [
        "orders" => ["toMany", "Order", "user_id", "id"]
        // $this->orders would result in Order::get((new QueryBuilder())->where("user_id", $this->id));
    ];
    
    /**
     * You may use a getter
     * @return Order[]
     */
    public function getOrders() : array
    {
        return $this->orders;
    }
}

$user = User::getOne((new QueryBuilder()));
$user_orders = $user->getOrders();

License

J0113/ODB is released under the Apache 2.0 license. See the enclosed LICENSE for details.

j0113/odb 适用场景与选型建议

j0113/odb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 67 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 02 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 j0113/odb 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2021-02-21