top-think / think-orm Goto Github PK
View Code? Open in Web Editor NEWThink ORM——the PHP Database&ORM Framework
License: Apache License 2.0
Think ORM——the PHP Database&ORM Framework
License: Apache License 2.0
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();
<?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
think-orm/src/db/Builder.php文件中protected function parseOrder(Query $query, $order)
第798行
parseRand方法不存在。
think\Paginator 141行 156行
think\ModelEvent 55行
上述三处仍然依赖5.1的专有容器类Container
现在最低版本需要7.1.X才能正常运行,readme没有更新过来
自减操作并没有正常的工作
代码:
// 查询商品
$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"
希望增加close方法手动关闭连接.
rt
Line 246 in a577493
lazyWrite不存在
使用sithSum最后的处理结果是count
可以及时同步最新的修改么?感谢。
$key = $this->getCacheKey($query, $options['where']['AND'][$pk]); 改行应该是忘记删除了吧?
Db::table('think_user')
->where([
'title' => ['like','%thinkphp']
])
->select();
比如Decimal128 怎么处理
findOrFail时可以添加错误信息
例如
$userCompany->where(['code' => $company_code])->findOrFail(‘公司不存在’);
在catch异常DataNotFoundException时,可以得到对应的错误信息
如题.我发现 composer 还是 0.9 ...
如果我mysql用的是长连接,用户极端情况下断开,连接要过一段时间才能被mysql释放,这个时间内如果有多用户来链接,我的mysql链接就被塞满了,是否考虑把链接信息存在静态中复用呢?
$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->parseWhereItem(Object(think\db\Query), '>=', Array, 'AND', Array)
#1 /data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Builder.php(270): think\db\Builder->buildWhere(Object(think\db\Query), Array)
#2 /data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Builder.php(959): think\db\Builder->parseWhere(Object(think\db\Query), Array)
#3 /data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Connection.php(882): think\db\Builder->select(Object(think\db\Query))
#4 /data/web/we7-new/viewarticle/vendor/topthink/think-orm/src/db/Query.php(2239): think\db\Connection->select(Object(think\db\Query))
#5 /data/web/we7-new/viewarticle/view.php(51): think\db\Query->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 />
Line 885 in 8915fcf
项目需要多个数据库连接
更新语句的时候这儿报错。 我是先查询
$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的。 希望官方能能够确认下。谢谢。
我把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)) { 这里
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); }
在swoole等常驻型应用,如何主动关闭链接?
我fork出去,改源码,主动触发close函数,在mysql下的processlst还是存在的
in think\Model Line: 84
配置项未处理合并默认配置 建议改进 否则初始化时需要带入resultset_type
、query
等已有的默认配置
codeigniter4框架,在composer下加载没问题,如果使用自带的加载器加载,遇到报错,Model.php文件中98行到112行,使用$config中auto_timestamp,datetime_format,resultset_type,query四个配置时未做判断,手动抛出警告Undefined index: auto_timestamp,不抛出则导致整个程序终止执行
强烈建议增加 Validate.php,这样数据操作前,可以做到校验
cache函数不支持tag操作,但是cache函数的第三个参数却是tag,实际上代码实现被删了
在chace里面依赖的think\App了类,但是compoer并没有包含.
2.0.0正常使用
在使用leftjoin的情况下,两表如果都有同字段,并且该字段为查询条件时,会出现冲突,具体表现为,表A使用某字段作为leftjoin关联字段,另一表使用id被关联,两表都有id时必定会报错。生成的sql语句中出现了未加alias的id字段
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
--
mysql下, pdo强制转换为string, 会导致出现数据类型过长的错误.
public function toCollection($collection,)
缺少第二个参数
public function toCollection($collection,$resultSetType)
上面这个错误是昨晚最新包1.2的
这个一直有,不知是环境问题还是用法错误,我的理解是
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');
}`
后下面正常使用
Db类的IDE注释中,因最上方没有use think\db\Query,编辑器无法进行方法智能补全,建议加上
建议对字段支持MySQL 内置方法
比如GeomFromText
升级到1.2.5 mongo之后where用不了 查不出来数据
// 数据库类型 '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
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.
使用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);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.