定制 carry0987/sanite 二次开发

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

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

carry0987/sanite

最新稳定版本:1.3.10

Composer 安装命令:

composer require carry0987/sanite

包简介

A PHP library that provide base CRUD structure and methods, using PDO.

README 文档

README

Packgist CI
Sanite is a PHP library that provide base CRUD structure and methods, using PDO.

Getting Started

Make sure you have Sanite installed. If not, you can install it with the following command:

composer require carry0987/sanite

After installation, you can include Sanite in your project and start using it.

Establishing a Database Connection

Use Sanite to establish a database connection:

use carry0987\Sanite\Sanite;

// Database connection settings
$config = array(
    'host' => 'mariadb',
    'database' => 'dev_sanite',
    'username' => 'test_user',
    'password' => 'test1234',
    'port' => 3306, // Optional
    'charset' => 'utf8mb4' // Optional
);

// Create a database connection
$sanite = new Sanite($config);

Using a Data Model

Create your own data models to perform CRUD operations. Here's an example of using UserModel to retrieve user data.

First, ensure your model extends DataReadModel (or corresponding DataCreateModel, DataDeleteModel, DataUpdateModel):

namespace carry0987\Sanite\Example;

use carry0987\Sanite\Models\DataReadModel;

class UserModel extends DataReadModel
{
    // Implement your methods, for example:
    public function getUserById(int $userId)
    {
        $queryArray = [
            'query' => 'SELECT * FROM user WHERE uid = ? LIMIT 1',
            'bind'  => 'i',  // This value needs to be relative when using DBUtil::getPDOType
        ];
        $dataArray = [$userId];

        return $this->getSingleData($queryArray, $dataArray);
    }

    public function getAllUsers()
    {
        $queryArray = [
            'query' => 'SELECT * FROM user'
        ];

        return $this->getMultipleData($queryArray);
    }
}

Then, you can use your model like so:

use carry0987\Sanite\Example\UserModel;

// Instantiate UserModel
$userModel = new UserModel($sanite);

// Retrieve user information for user with ID 1
$user = $userModel->getUserById(1);
$users = $userModel->getAllUsers();

print_r($user);
print_r($users);

Exception Handling

Sanite defines a specific exception class DatabaseException. Capture and handle it appropriately in your code:

try {
    // ... attempt some database operations ...
} catch (\carry0987\Sanite\Exceptions\DatabaseException $e) {
    // ... handle database exception ...
    echo "Error: " . $e->getMessage();
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-30