coffeecode2/datalayer2

数据层是数据库中PDO的持久抽象组件

1.1.8 2021-03-21 14:00 UTC

This package is not auto-updated.

Last update: 2024-10-02 04:46:59 UTC


README

Maintainer Source Code PHP from Packagist Latest Version Software License Build Quality Score Total Downloads

数据层是数据库中PDO的持久抽象组件,PDO已经为执行常见操作如注册、读取、编辑和删除数据准备了指令。

数据层是使用PDO和预处理语句执行常见操作(如注册、读取、编辑和删除数据)的持久抽象组件。

关于CoffeeCode

CoffeeCode 是一套用于常见任务的小型且优化的 PHP 组件。由 Robson V. Leite 和 UpInside 团队维护。与他们一起,您可以用更少的行数执行常规任务,编写更少代码并完成更多工作。

CoffeeCode 是一套用于常见任务的小型且优化的 PHP 组件。由 Robson V. Leite 和 UpInside 团队维护。与他们一起,您可以用更少的行数执行常规任务,编写更少代码并完成更多工作。

亮点

  • 易于设置(Fácil de configurar)
  • 完整的 CRUD 抽象(Asbtração total do CRUD)
  • 创建安全模型(Crie de modelos seguros)
  • Composer 就绪(Pronto para o composer)
  • PSR-2 兼容(Compatível com PSR-2)

安装

数据层可以通过 Composer 获取

"coffeecode/datalayer": "1.1.*"

或者运行

composer require coffeecode/datalayer

文档

有关如何使用数据层的详细信息,请参阅组件目录中带有详细信息的示例文件夹

有关如何使用数据层的详细信息,请参阅组件目录中的示例文件夹,其中包含详细信息

连接

要开始使用数据层,您需要连接到数据库(MariaDB / MySql)。有关更多连接信息,请参阅 PHP.net 上的 PDO 连接手册

为了开始使用数据层,我们需要连接到您的数据库。有关可能的连接信息,请访问 PHP.net 上的 PDO 连接手册

define("DATA_LAYER_CONFIG", [
    "driver" => "mysql",
    "host" => "localhost",
    "port" => "3306",
    "dbname" => "datalayer_example",
    "username" => "root",
    "passwd" => "",
    "options" => [
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
        PDO::ATTR_CASE => PDO::CASE_NATURAL
    ]
]);

您的模型

数据层基于 MVC 结构,采用 Layer Super Type 和 Active Record 设计模式。要使用它,您需要创建表的模型并继承数据层。

数据层基于 MVC 结构,采用 Layer Super Type 和 Active Record 设计模式。要使用它,您需要创建表的模型并继承数据层。

class User extends DataLayer
{
    /**
     * User constructor.
     */
    public function __construct()
    {
        //string "TABLE_NAME", array ["REQUIRED_FIELD_1", "REQUIRED_FIELD_2"], string "PRIMARY_KEY", bool "TIMESTAMPS"
        parent::__construct("users", ["first_name", "last_name"]);
    }
}

查找

<?php
use Example\Models\User;
$model = new User();

//find all users
$users = $model->find()->fetch(true);

//find all users limit 2
$users = $model->find()->limit(2)->fetch(true);

//find all users limit 2 offset 2
$users = $model->find()->limit(2)->offset(2)->fetch(true);

//find all users limit 2 offset 2 order by field ASC
$users = $model->find()->limit(2)->offset(2)->order("first_name ASC")->fetch(true);

//looping users
foreach ($users as $user) {
    echo $user->first_name;
}

//find one user by condition
$user = $model->find("first_name = :name", "name=Robson")->fetch();
echo $user->first_name;

//find one user by two conditions
$user = $model->find("first_name = :name AND last_name = :last", "name=Robson&last=Leite")->fetch();
echo $user->first_name . " " . $user->first_last;

findById

<?php
use Example\Models\User;

$model = new User();
$user = $model->findById(2);
echo $user->first_name;

安全参数

请参阅 find_example.php 示例和模型类

请参阅 find_example.php 示例和模型类

$params = http_build_query(["name" => "UpInside & Associated"]);
$company = (new Company())->find("name = :name", $params);
var_dump($company, $company->fetch());

连接方法

请参阅 find_example.php 示例和模型类

请参阅 find_example.php 示例和模型类

$addresses = new Address();
$address = $addresses->findById(22);
//get user data to this->user->[all data]
$address->user();
var_dump($address);

计数

<?php
use Example\Models\User;
$model = new User();

$count = $model->find()->count();

保存创建

<?php
use Example\Models\User;
$user = new User();

$user->first_name = "Robson";
$user->last_name = "Leite";
$userId = $user->save();

保存更新

<?php
use Example\Models\User;
$user = (new User())->findById(2);

$user->first_name = "Robson";
$userId = $user->save();

销毁

<?php
use Example\Models\User;
$user = (new User())->findById(2);

$user->destroy();

失败

<?php
use Example\Models\User;
$user = (new User())->findById(2);

if($user->fail()){
    echo $user->fail()->getMessage();
}

自定义数据方法

class User{
    //...

    public function fullName(): string 
    {
        return "{$this->first_name} {$this->last_name}";
    }
    
    public function document(): string
    {
        return "Restrict";
    }
}

echo $this->full_name; //Robson V. Leite
echo $this->document; //Restrict

贡献

有关详细信息,请参阅 CONTRIBUTING

支持

安全:如果您发现任何与安全相关的问题,请通过电子邮件 cursos@upinside.com.br 联系我们,而不是使用问题跟踪器。

如果您发现任何与安全相关的问题,请通过电子邮件 cursos@upinside.com.br 联系我们,而不是使用问题跟踪器。

谢谢

致谢

许可协议

MIT 许可协议 (MIT)。请参阅 许可文件 以获取更多信息。