Git Product home page Git Product logo

think-orm's Introduction

ThinkORM

基于PHP8.0+ 和PDO实现的ORM,支持多数据库,3.0版本主要特性包括:

  • 基于PDO和PHP强类型实现
  • 支持原生查询和查询构造器
  • 自动参数绑定和预查询
  • 简洁易用的查询功能
  • 强大灵活的模型用法
  • 支持预载入关联查询和延迟关联查询
  • 支持多数据库及动态切换
  • 支持MongoDb
  • 支持分布式及事务
  • 支持断点重连
  • 支持JSON查询
  • 支持数据库日志
  • 支持PSR-16缓存及PSR-3日志规范

安装

composer require topthink/think-orm

文档

详细参考 ThinkORM开发指南

think-orm's People

Contributors

4352570 avatar 9007967 avatar augushong avatar big-dream avatar evalor avatar gitlilimin avatar hhh0121 avatar ichynul avatar junjunya1995 avatar klinson avatar laterz avatar liu21st avatar liuqiandev avatar liuqiantech avatar lyz7805 avatar moniang avatar nhzex avatar riverdancegit avatar season886 avatar shaukei avatar stylecibot avatar sunkangchina avatar tinywan avatar willove avatar woodongwong avatar yaobiao131 avatar yepyuyu avatar yuanzhihai avatar yunwuxin avatar zoujingli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

think-orm's Issues

每次用Db::name都会重新新建mysql的链接?

如果我mysql用的是长连接,用户极端情况下断开,连接要过一段时间才能被mysql释放,这个时间内如果有多用户来链接,我的mysql链接就被塞满了,是否考虑把链接信息存在静态中复用呢?

model->saveAll() bug

model 下的340后 对比tp5.1的model
/**
* 新增数据是否使用Replace
* @access public
* @param bool $replace
* @return $this
*/
public function replace($replace = true)
{
$this->replace = $replace;
return $this;
}

缺少replace方法,导致saveAll 出错

Cache不支持tag

cache函数不支持tag操作,但是cache函数的第三个参数却是tag,实际上代码实现被删了

长链接重连有失败情况

swoole项目中使用了think-orm,达到28800秒超时后项目日志中收到这种错误,php中内存已经给到2G,无论给多少内存都会有这种错误

[2018-04-09 00:00:00 *52170.12] ERROR zm_deactivate_swoole (ERROR 503): Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 20480 bytes) in /idata/serv/tcp/vendor/topthink/think-orm/src/db/Connection.php on line 735.

Model.php在非composer加载器下会报错

codeigniter4框架,在composer下加载没问题,如果使用自带的加载器加载,遇到报错,Model.php文件中98行到112行,使用$config中auto_timestamp,datetime_format,resultset_type,query四个配置时未做判断,手动抛出警告Undefined index: auto_timestamp,不抛出则导致整个程序终止执行

为何设置数据表前缀不生效?

<?php
require_once './vendor/autoload.php';
use think\Db;

$dbconf = [
    'type' => 'mysql',
    'hostname' => '127.0.0.1',
    'database' => 'job_nad',
    'username' => 'root',
    'password' => 'root',
    'hostport' => '3306',
    'params' => [],
    'charset' => 'utf8mb4',
    'prefix' => 'tb_',
];
Db::setConfig($dbconf);
$data = Db::table('user')->find();
var_dump($data);

报错信息
PS D:\developer\job.app\nad-test> php 1.php
PHP Fatal error:  Uncaught exception 'think\db\exception\PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'job_nad.user' doesn't exist' in D:\developer\job.app\nad-test\vendor\topthink\think-orm\src\db\Connection.php:670
Stack trace:
#0 D:\developer\job.app\nad-test\vendor\topthink\think-orm\src\db\connector\Mysql.php(90): think\db\Connection->query('SHOW COLUMNS FR...', Array, false, true)
#1 D:\developer\job.app\nad-test\vendor\topthink\think-orm\src\db\Connection.php(376): think\db\connector\Mysql->getFields('user')
#2 D:\developer\job.app\nad-test\vendor\topthink\think-orm\src\db\Connection.php(412): think\db\Connection->getTableInfo('user', 'pk')
#3 D:\developer\job.app\nad-test\vendor\topthink\think-orm\src\db\Query.php(1912): think\db\Connection->getPk('user')
#4 D:\developer\job.app\nad-test\vendor\topthink\think-orm\src\db\Connection.php(777): think\db\Query->getPk(Array)
#5 D:\developer\job.app\nad-test\vendor\topthink\think-orm\src\db\Query.php(2453): think\db\Connection->find(Ob in D:\developer\job.app\nad-test\vendor\topthink\think-orm\src\db\Connection.php on line 670

MongoQuery实例化错误

Commit : 84d9b78 / dc72288

此处尝试new一个MongoQuery,MongoQuery继承自Query,构造中规定传入Connection类型

MongoConnector并没有继承自Connection引起错误

Uncaught TypeError: Argument 1 passed to think\db\Query::__construct() must be an instance of think\db\Connection or null, instance of think\db\connector\Mongo given

parseRand方法不存在

think-orm/src/db/Builder.php文件中protected function parseOrder(Query $query, $order)
第798行
parseRand方法不存在。

自动时间戳 错误

Uncaught Error: Class 'think\model\concern\DateTime' not found in /vendor/topthink/think-orm/src/model/concern/TimeStamp.php:49

建议修改为

if ($timestamp) { $dateTime = new \DateTime(); $dateTime->setTimestamp($time); } else { $dateTime = new \DateTime($time); }

模型不能执行更新操作

我把think-orm放在yaf里面 ,模型不能正常使用更新,这两种方式都报错Fatal error: Uncaught think\Exception: where express error
1、$res = UserModel::update(['name' => 'wwang'],['uid'=>18]);

2、$user = new UserModel();
$user->allowField(true)->save(['name' => 'wwang'],['uid'=>18]);

报错源码是在Builder.php line 304 elseif (!($value instanceof \Closure)) { 这里

['ext', 'aa+1'] 方式无法正常添加值

如题:

        $data['remnant_inventory'] = ['exp', 'remnant_inventory + ' . $invent];

selection_027

之前是好好的. 将 0.9 版本更新到1.x 版本后就不行了... inc 方式也不行.

    if ($invent > 0) {
            $data['remnant_inventory'] = ['inc', 'remnant_inventory + ' . $invent];
        }

selection_028

Error at file[/home/wwwroot/easySwoole/vendor/topthink/think-orm/src/db/Connection.php] line[1140] message:[Undefined index: id]

更新语句的时候这儿报错。 我是先查询
$machine = UserMachine::where(['user_id' => $uid, 'machine_code' => $machineCode])->find();
if ( empty($machine) ) {

            $machine = new UserMachine();
            
            $machine->user_id           = $uid;
            $machine->machine_code      = $machineCode;
            $machine->is_online         = UserMachine::STATUS_ONLINE;
            $machine->real_power        = $power;
            $machine->latest_heart_time = time();
            $machine->user_id           = $uid;//考虑到客户端的时间不可靠,这里采用服务器时间作为依据
            $machine->create_time       = date('Y-m-d H:i:s');
            //初始化用户机器
            if ( !$machine->save() ) {
                return $this->outputError('上报失败');
            }
            $machineId = $machine->id;
            
        }else{
            $machine->real_power        = $power;
            $machine->latest_heart_time = time();
            $machine->update_time       = date('Y-m-d H:i:s');
            if(!$machine->save()){
                return $this->outputError('上报失败');
            }
            $machineId = $machine->id;
        }

在调用else 分支下 的 $machine->save() save方法时报错。 可以肯定的是我打印出来了, 前后的 $machine->id 都是有主键id的。 希望官方能能够确认下。谢谢。

IDE缺少智能提示

Db类的IDE注释中,因最上方没有use think\db\Query,编辑器无法进行方法智能补全,建议加上

配置合并

in think\Model Line: 84

配置项未处理合并默认配置 建议改进 否则初始化时需要带入resultset_typequery等已有的默认配置

最新2.0.1的bug

在chace里面依赖的think\App了类,但是compoer并没有包含.
2.0.0正常使用

模型setDec方法不能正常工作

自减操作并没有正常的工作
代码:

// 查询商品
$product = Product::get(1);
// 查询用户
$user = Users::get(1);
// 减用户余额
var_dump($user->setDec('points', $product['points']));
var_dump($user->getLastSql());
var_dump($product['points']);

运行结果:

int(0)
string(52) "UPDATE `users`  SET `points`=points  WHERE  `id` = 1"
string(5) "88888"

继承Model后里边直接定义$connection 后报错 Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Undefined db type' in D:\wamp\vhosts\think.orm\vendor\topthink\think-orm\src\db\Connection.php on line 178

use think\Model;

class BaseModel extends Model
{
protected $pk = 'user_id';
protected $table = 'f_users';
protected $connection = [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '172.3.0.74',
// 数据库名
'database' => 'f_houses',
// 用户名
'username' => 'f_houses',
// 密码
'password' => 'f_houses',
// 端口
'hostport' => '3306',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => 'f_',
// 数据库调试模式
'debug' => true,
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效
'rw_separate' => false,
// 读写分离后 主服务器数量
'master_num' => 1,
// 指定从服务器序号
'slave_no' => '',
// 是否严格检查字段是否存在
'fields_strict' => true,
// 数据集返回类型 array 数组 collection Collection对象
'resultset_type' => 'collection',
// 是否自动写入时间戳字段
'auto_timestamp' => false,
//自动时间格式转换
'datetime_format' => false,
// 是否需要进行SQL性能分析
'sql_explain' => false,
];

public function __construct($data = [])
{
    parent::__construct($data);
}

}
$db = new BaseModel();
$db->select();

缺少依赖类

think\Paginator 141行 156行
think\ModelEvent 55行

上述三处仍然依赖5.1的专有容器类Container

where查询语法bug

$where = [
    'a' => ['>=',10]
];
$where = [
    'b' => ['<=',10]
];

where函数无法使用这种查询语法
报错

<br />
<b>Notice</b>:  Undefined offset: 1 in <b>/data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Builder.php</b> on line <b>375</b><br />
<br />
<b>Fatal error</b>:  Uncaught think\Exception: where express error:0000-00-00 in /data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Builder.php:436
Stack trace:
#0 /data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Builder.php(354): think\db\Builder-&gt;parseWhereItem(Object(think\db\Query), '&gt;=', Array, 'AND', Array)
#1 /data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Builder.php(270): think\db\Builder-&gt;buildWhere(Object(think\db\Query), Array)
#2 /data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Builder.php(959): think\db\Builder-&gt;parseWhere(Object(think\db\Query), Array)
#3 /data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Connection.php(882): think\db\Builder-&gt;select(Object(think\db\Query))
#4 /data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Query.php(2239): think\db\Connection-&gt;select(Object(think\db\Query))
#5 /data/web/we7-new/viewarticle/view.php(51): think\db\Query-&gt;select()
#6 {main}
  thrown in <b>/data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Builder.php</b> on line <b>436</b><br />

where条件解析失败

使用Mongo驱动查询时,进行任何含有where操作都将得到一个异常

Undefined offset: 1 in file /vendor/topthink/think-orm/src/db/builder/Mongo.php line 216

#1 /vendor/topthink/think-orm/src/db/builder/Mongo.php(472): think\db\builder\Mongo->parseWhere(Object(think\db\Mongo), Array)
#2 /vendor/topthink/think-orm/src/db/connector/Mongo.php(1083): think\db\builder\Mongo->select(Object(think\db\Mongo))
#3 /vendor/topthink/think-orm/src/db/Query.php(2297): think\db\connector\Mongo->find(Object(think\db\Mongo))
#4 /App/Controller/Index.php(25): think\db\Query->find()

查询代码

$users = new Users;
$user  = $users->where('username', 'admin')->find();
var_dump($user);

withSum BUG

使用sithSum最后的处理结果是count

php 7+ CLI下 报两处错误

image

image
public function toCollection($collection,)
缺少第二个参数
public function toCollection($collection,$resultSetType)
上面这个错误是昨晚最新包1.2的

image
image

这个一直有,不知是环境问题还是用法错误,我的理解是
use think\Collection; use think\db\connector\Mongo as Connection;
这里重复了,导致下面的__construct 中的Connection错误,反正我改成
use think\db\connector\Mongo as mConnection;

`/**
* 架构函数
* @access public
*/
public function __construct(mConnection $connection = null)
{
if (is_null($connection)) {
$this->connection = mConnection::instance();
} else {
$this->connection = $connection;
}

    $this->prefix = $this->connection->getConfig('prefix');
}`

后下面正常使用

sqltie 配置是什么 我配置的连不上

// 数据库类型 'type' => 'sqlite', // 服务器地址 'hostname' => '', // 数据库名 'database' => 'data', // 用户名 'username' => '', // 密码 'password' => '', // 端口 'hostport' => '', // 连接dsn 'dsn' => 'sqlite:data.db',
data.db文件在public

控制器调用
dump(\think\Db::table("order")->select());

报错
SQLSTATE[HY000]: General error: 1 near "order": syntax error

不支持bit类型

mysql下, pdo强制转换为string, 会导致出现数据类型过长的错误.

写入大量数据行时报错!

Db::table('t_company_info2')->insertAll($data);
此文件有: 117 条数据
正在写入数据...
写入成功!

此文件有: 4999 条数据
正在写入数据...                

Fatal error: Uncaught think\db\exception\PDOException: SQLSTATE[HY000]: General error:  1390 Prepared statement contains too many placeholders in D:\wamp64\www\ex\vendor\top think\think-orm\src\db\Connection.php:737
Stack trace:
 #0 D:\wamp64\www\ex\vendor\topthink\think-orm\src\db\Connection.php(1036): think\db\Co nnection->execute('INSERT INTO `t_...', Array, Object(think\db\Query))
#1 D:\wamp64\www\ex\vendor\topthink\think-orm\src\db\Query.php(2724): think\db\Connect ion->insertAll(Object(think\db\Query), Array, false, NULL)
#2 D:\wamp64\www\ex\index.php(172): think\db\Query->insertAll(Array)
#3 D:\wamp64\www\ex\index.php(159): insert(Array)
#4 D:\wamp64\www\ex\index.php(175): read_all('./file_path')
#5 {main}
thrown in D:\wamp64\www\ex\vendor\topthink\think-orm\src\db\Connection.php on line 7 37
--

findOrFail时如何可以添加错误信息

findOrFail时可以添加错误信息
例如
$userCompany->where(['code' => $company_code])->findOrFail(‘公司不存在’);在catch异常DataNotFoundException时,可以得到对应的错误信息

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.