carry0987/sanite
最新稳定版本:1.3.10
Composer 安装命令:
composer require carry0987/sanite
包简介
A PHP library that provide base CRUD structure and methods, using PDO.
README 文档
README
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(); }
统计信息
- 总下载量: 132
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-30