使用laravel Eloquent ORM 速度太慢 返回的数据量大
public function getUnprincipal() {
$now = date('Y-m-d H:i:s');
/** @var $investList array */
$investList = DB::select(
"SELECT money FROM invest WHERE DATE_ADD(time,INTERVAL day day) >= ? AND uid=?",
[$now, $this->getId()]);
$money = 0;
foreach ($investList as $i) {
$money += $i['money'];
}
return round($money, 2);
}
$investList = DB::select(
"SELECT money FROM invest WHERE DATE_ADD(time,INTERVAL day day) >= ? AND uid=?",
[$now, $this->getId()]);
返回数据格式是php原生数组 array, 数组元素是stdClass类型
array(1) {
[0]=>
object(stdClass)#1322 (1) {
["money"]=>
string(4) "2000"
}
}
使用ORM返回的对象类型是 Illuminate\Support\Collection
数组元素是自定义类 继承Illuminate\Database\Eloquent\Model的实例
类似pdo参数绑定的语法
插入数据:
DB::insert('insert into test (id, name, email, password) values (?, ?, ? , ? )',[1, 'Laravel','laravel@test.com','Laravel']);
更新数据:
$result = DB::update('update test set name="laraveltest" where name = ?', ['laravel']);
删除语句:
$deleted = DB::delete('delete from test');