miladm/prototype

1.19.0 2023-10-29 19:57 UTC

README

只需使用 composer 安装包并将 autoloader 加载到您的 index.php 文件中即可

composer require miladm/prototype

index.php

<?php

include 'vendor/autoload.php';

创建模型(原型)

use miladm\Prototype;

class UserModel extends Prototype
{

}

这里我们扩展了 Prototype

创建连接

要创建连接,请创建以下类

use miladm\table\Connection;

class MainConnection extends Connection
{
    public $host = "127.0.0.1";
    public $databaseName = "sample";
    public $user = 'root';
    public $password = 'root';
}

并将您的连接设置到 prototype 中如下所示

class UserModel extends Prototype
{
    public function connection()
    {
        return new MainConnection;
    }
}

插入模式

要创建模式,我们必须在类的 init 方法中设置模式

class UserModel extends Prototype
{
    public function connection()
    {
        return new MainConnection;
    }

    public function init()
    {
        // schema goes here
    }
}

创建模式

Prototype 父类中有一个名为 schema 的方法如下所示

    schema($name = false): Schema

name 参数是数据库中此原型的名称。例如,如果您数据库中有一个名为 user_data 的表,并且您想创建 UserModel 原型,则必须将 user_data 作为名称放入模式方法中。

class UserModel extends Prototype
{
    public function init()
    {
        $this->schema('user_data');
    }
}

要创建模式,您必须设置表的架构。要配置表架构,您必须调用 Schema 的方法。为了更好地理解,我们通过一个示例来演示

    public function init()
    {
        $this->schema('user_data')->string('name')->string('family')->int('age');
    }

在这个示例中,我们有三个用于 user_data 表的字段,它们是 name 类型为字符串,family 类型为字符串,以及 age 类型为数字。这里的字符串类型意味着 mariadb 数据库中的 varchar,数字类型意味着 int

重要提示:所有模式默认都有 id:intcreate_time:timestampupdate_time:timestamp。您不需要添加任何。

还有更多字段类型可供您的架构使用。您可以根据以下表格进行参考

示例

    public function init()
    {
        $this->schema('user_data')
            ->string('name')
            ->string('family')
            ->int('age')
            ->email('email')
            ->hash('password')
            ->boolean('verified');
    }

此文档正在建设中,但代码自说自话。请随意阅读和使用代码!