mcred/

phpred

PHP 的关系、实体和数据源

0.0.5 2017-08-30 22:43 UTC

This package is auto-updated.

Last update: 2024-09-06 16:30:22 UTC


README

Build Status Code Climate Test Coverage Issue Count

描述

PHPRed 是一个有观点的轻量级 ORM。尽管有许多可用的 PHP ORM,但许多都包含我从未使用过的功能。PHPRed 包含非常基本的方法和用法。

要求

  • PHP 7.1+
  • Composer
  • Mysqli

安装

composer require mcred/phpred

设置

除了下面的示例之外,`tests/mocks` 文件夹中还有其他示例。设置模型非常简单:创建一个扩展 `PHPRed/Models/Model` 类的模型类,然后在构造函数中定义该模型的属性。例如

<?php
class MyClass extends \PHPRed\Models\Model
{
    public function __construct(\MysqliDb $mysql)
    {
        $this->model = 'MyClass';
        $this->table = 'my_class';
        $this->primaryKey = 'id';
        $this->foreignKey = 'my_class_id';
        $this->fields = ['id', 'name'];
        $this->requiredFields = ['name'];
        $this->uniqueFields = ['name'];
        $this->hasMany = ['MyClassProperties'];
        $this->hasAndBelongsToMany = ['Users'];

        parent::__construct($mysql);
    }
}

构造函数属性

  • model: 字符串
  • table: 字符串
  • primaryKey: 字符串
  • foreignKey: 字符串
  • fields: 数组
  • requiredFields: 数组
  • uniqueFields: 数组
  • hasMany: 数组
  • belongsTo: 数组
  • hasAndBelongsToMany: 数组

方法

  • getAll() : 数组
  • getById(int $modelId) : 数组
  • getBySearch(array ['key' => 'value']) : 数组
  • insert(array $data) : 数组
  • updateById(int $modelId, array $data) : 数组
  • deleteById(int $modelId): void