一个简单的PHP Micro MySQL ORM,具有流畅的接口。

dev-master 2019-11-02 18:11 UTC

This package is auto-updated.

Last update: 2024-09-29 05:09:07 UTC


README

一个简单的PHP Micro MySQL ORM,具有流畅的接口

特性

  • 易于使用。
  • 流畅的设计和界面。
  • 所需的最小依赖,即仅PHP >= 5.3.0。
  • 防止SQL注入。
  • 使用AND和OR运算符进行分层WHERE条件。
  • 使用升序和降序的ORDER BY子句。
  • 限制和偏移量。

安装(使用Composer

composer require kundansingh86/creta

要使用绑定,请使用Composer的自动加载

require_once('vendor/autoload.php');

入门指南

添加命名空间并创建上下文。

use Creta\MySqlDbContext;


$properties = [
        'host'=>'localhost',
        'username'=>'',
        'password'=>'',
        'database'=>'test'
];

$context = new MySqlDbContext($properties);

插入语句

将数据插入表,获取记录的生成插入ID;

$personId = $context->table('person')           // table 
                ->insert(['name'=>"Jhon",       // column with name value pair
                        'age'=>25, 
                        'salary' => '3000', 
                        'department' => 'sales', 
                        'position' => 'executive'])
                ->execute();

echo 'Inserted Person ID :: Jhon ', $personId; 

更新语句

使用WHERE条件更新表中的数据

$context->table('person')     // table
        ->update(['position'=>'assistant manager', 'salary' => '2800'])     // column with name value pair
        ->where(['name' => 'David', 'department'=>'finance']) // AND condition with name value pair
        ->execute();

删除语句

使用WHERE条件从表中删除数据

$context->table('person')   // table
        ->delete()
        ->where(['id' => 10])   // condition with name value pair
        ->execute();

要删除表中的所有记录,不要指定WHERE条件

$context->table('person')   //table
        ->delete()
        ->execute();

选择语句

根据WHERE条件从表中选择所有列

$result = $context->table('person') // table
                  ->select()
                  ->where(['id' => 2, 'position' => 'manager'])  // condition with name value pair 
                  ->orderBy('name') // order by name in ascending
                  ->execute();

echo '<pre>';
print_r($result);

从表中选择特定的列

$result = $context->table('person') // table
                  ->select(["id", "name", "position"]) // column name array
                  ->orderByDesc('salary', 'position') // order by salary then by position in descending
                  ->limit(2, 1) // limit with 2 records and skip the 1st record
                  ->execute();

echo '<pre>';
print_r($result);

查询输出

查看任何语句的SQL查询输出

echo $context->table('person') // table
             ->select()
             ->where(['id' => 2, 'position' => 'manager'])  // condition with name value pair 
             ->orderBy('name') // order by name in ascending
             ->query(); // returns the generated sql query

关闭上下文(推荐)

在操作完成后关闭上下文和连接

$context->close();

注意:repo中提供了用于演示的test.sql和test.php文件。

高级主题

在不同场景下使用AND & OR连接和其他运算符(如 <、>、<=、>=、like)形成WHERE条件。

使用AND和OR运算符的WHERE子句

示例1: SELECT * FROM person WHERE id = 2 AND position = 'manager'
$result = $context->table('person')
                  ->select()
                  ->where(['id' => 2, 'position' => 'manager'])  
                  ->execute();
示例2: SELECT * FROM person WHERE (position = 'manager' OR (salary >= 2000 AND salary <= 3000))
$result = $context->table('person')
                  ->select()
                  ->where(['position' => 'manager'])
                  ->withOr(['salary >=' => 2000, 'salary <=' => 3000]) 
                  ->execute();
示例3: SELECT * FROM person WHERE (salary > 3000 AND (position = 'manager' OR position = 'executive'))
$result = $context->table('person')
                  ->select()
                  ->where(['salary >' => 3000])
                  ->withAnd(['position' => ['manager', 'executive']]) 
                  ->execute();
示例4: SELECT * FROM person WHERE position = 'manager' OR position = 'executive'
$result = $context->table('person')
                  ->select()
                  ->whereOr(['position' => ['manager', 'executive']]) 
                  ->execute();
示例5: SELECT * FROM person WHERE (age > 20 AND age < 22) OR (age > 25 AND age < 28) ORDER BY name asc
$result = $context->table('person')
                  ->select()
                  ->where(['age >' => 20, 'age <' => 22]) 
                  ->orWhere(['age >' => 25, 'age <' => 28])
                  ->orderBy('name')  
                  ->execute();

路线图

  • 一次性插入多条记录。
  • 使用GROUP BY和HAVING子句。
  • 聚合函数。
  • 在表中使用JOIN。

贡献

欢迎提交pull请求。对于重大更改,请首先打开一个issue进行讨论您想要更改的内容。

请确保适当更新测试。

许可

MIT