jarivas/no-orm

该包已被废弃且不再维护。未建议替代包。

简单轻量级的SQL库

2.0.2 2021-06-20 16:44 UTC

This package is auto-updated.

Last update: 2022-09-20 19:38:08 UTC


README

你可能需要一个简单轻量级的SQL库,但你不知道

  • 生成实体
  • 需要基本的Mysql知识
  • 没有晦涩的代码
  • Pascal cased getter 和 setter
  • 列名作为常量
  • 自动处理关系

入门

  • composer require jarivas/no-orm
  • $host 是mysql的主机
  • $dbname 读取表以生成辅助类时的模式名称
  • $username mysql用户
  • $password mysql密码
  • $targetFolder 在文件系统中你想要文件所在的位置
  • $namespace,是一个标准PHP命名空间字符串,例如 'Db\Book'

生成类

  • use NoOrm\EntityGenerator;
  • $result = EntityGenerator::process($host, $dbname, $username, $password, $targetFolder, $namespace)
  • 我的建议是创建php命令,以便每次需要时生成文件

示例

  • 我已经使用这个数据库示例 https://github.com/datacharmer/test_db
  • 选择前1000行 $result = Entity\CurrentDeptEmp::get();
  • 获取第一行的列emp_no的值 $result[0]->getEmpNo()
  • 插入
$columns = [
    Entity\Departments::COL_DEPT_NO,
    Entity\Departments::COL_DEPT_NAME
];
   
$values = [
    'd010',
    'Test'
];
   
$result = Entity\Departments::insertOne($columns, $values);
  • 更新
$assignments = [
    Entity\Departments::COL_DEPT_NAME . ' = "HOLA"'
];

$where = [
    [
        'operator' => 'AND',
        'condition' => Entity\Departments::COL_DEPT_NO . ' = "d010"'
    ]
];

$result = Entity\Departments::update($assignments, $where);
  • 删除 $result = Entity\Departments::delete($where);

API

  • 所有生成的类都包含以COL开头的const,代表数据库中的该列,例如 CurrentDeptEmp::COL_DEPT_NO
  • where 和 having 语法在所有方法中都是相同的
  • 投影意味着查询中选择的字段
  • 如果你未指定任何投影,默认将生成所有字段PascalCased,而不是snake_cased emp_no => EmpNo,同时也会包括关系中的字段
  • 关系字段将具有大写表名前缀 DEPARTMENTS_DeptNoEMPLOYEES_BirthDate
  • EntityBody trait 将在每个生成的类中,它将负责生成和执行查询
  • 这些方法是 getinsertOneupdatedelete
/**
 * Retrieves an array of Entities (same class where was invoked)
 * @param array $projection (array of class's columns constant)
 * @param array $where (array of ['operator' => AND|OR, 'condition' => COL_CONSTANT . ' =, like ....'])
 * @param string $group (an string that you would place after GROUP BY)
 * @param array $having (same as $where)
 * @param string $order (an string that you would place after ORDER BY)
 * @param int $limit (in case you need it)
 * @param int $offset (in case you need it)
 * @return array[Entity]
 */
public static function get(array $projection = [], array $where = [],
    string $group = '', array $having = [], string $order = '',
    int $limit = 1000, int $offset = 0): array


/**
 * Inserts a new row in the table
 * @param array $columns (array of class's columns constant)
 * @param array $values (array of values in the same order as $columns)
 * @param string $onDuplicateKeyUpdate (an string that you would place after ON DUPLICATED KEY UPDATE)
 * @return int (number of rows affected)
 */
public static function insertOne(array $columns, array $values,
        string $onDuplicateKeyUpdate = ''): int

/**
 * Updates one or more rows in the table
 * @param array $assignments
 * @param array $where (array of ['operator' => AND|OR, 'condition' => COL_CONSTANT . ' =, like ....'])
 * @param string $order (an string that you would place after ORDER BY)
 * @param int $limit (in case you need it)
 * @return bool (true in case of success)
 */
public static function update(array $assignments, array $where = [],
    string $order = '', int $limit = 0): bool

/**
 * Deletes one or more rows in the table
 * @param array $where (array of ['operator' => AND|OR, 'condition' => COL_CONSTANT . ' =, like ....'])
 * @param string $order (an string that you would place after ORDER BY)
 * @param int $limit (in case you need it)
 * @return bool (true in case of success)
 */
public static function delete(array $where = [], string $order = '', int $limit = 0): bool