×

eloquentphp

关于eloquentphp的信息

admin admin 发表于2023-04-01 06:11:08 浏览81 评论0

抢沙发发表评论

本文目录一览:

如何在 ThinkPHP 中整合 Laravel Eloquent ORM

安装 illuminate/database根据自己使用的 PHP 版本,通过 composer 安装对应的 illuminate/database 版本,例如

composer require illuminate/database:5.3.*

接入到 TP 中

在 ThinkPHPLibraryThinkThink.class.php 文件中的 start方法的最后一行的 App::run(); 上方添加如下代码:

$capsule = new \Illuminate\Database\Capsule\Manager;

$capsule-addConnection([

'driver' = C('DB_TYPE'),

'host' = C('DB_HOST'),

'database' = C('DB_NAME'),

'username' = C('DB_USER'),

'password' = C('DB_PWD'),

'charset' = C('DB_CHARSET'),

'collation' = C('DB_COLLATION'),

'prefix' = C('DB_PREFIX'),

]);

$capsule-setAsGlobal();

$capsule-bootEloquent();

解决 E 方法冲突 illuminate/database 的 vendorilluminatesupporthelpers.php 方法中存在一个方法

/**

* Escape HTML special characters in a string.

*

* @param \Illuminate\Contracts\Support\Htmlable|string $value

* @return string

*/

function e($value)

{

if ($value instanceof Htmlable) {

return $value-toHtml();

}

return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);

}

与 TP 的 E 方法冲突。

/**

* 抛出异常处理

* @param string $msg 异常消息

* @param integer $code 异常代码 默认为0

* @throws Think\Exception

* @return void

*/

function E($msg, $code=0) {

throw new Think\Exception($msg, $code);

}

我选择注释了 illuminate/database 的方法,搜索后发现没有其他地方用到这个方法,故注释。

完成后就可以愉快地使用 Laravel 的 ORM 来 coding 了。

PHP框架 Laravel Eloquent ORM 批量插入数据,怎么实现

PHP框架 Laravel Eloquent ORM 批量插入数据是通过传入数组实现的。

比如:

DB::table('users')-insert(array(

array('email' = 'taylor@example.com', 'votes' = 0),

array('email' = 'dayle@example.com', 'votes' = 0),

));

以上是操作表users,执行insert语句,参数是一个数组,封装了两条数据,这里可以自定义数据,insert内部就编程批量插入了。

然后调用save方法:

public static function create(array $attributes)

{

$model = new static($attributes);

$model-save();

return $model;

}

Laravel Eloquent save 触发了哪些事件

Illuminate/Database/Eloquent/Model.php

public function save(array $options = [])

{

$query = $this-newQueryWithoutScopes();

if ($this-fireModelEvent('saving') === false) {

return false;

}

if ($this-exists) {

$saved = $this-performUpdate($query, $options);

} else {

$saved = $this-performInsert($query, $options);

}

if ($saved) {

$this-finishSave($options);

}

return $saved;

}

首先触发的当然是saving,如果saving返回的是false,那么save就失败了,返回false

接着如果$this-exists,就是这个model不是新创建的,那么就需要进行更新操作

protected function performUpdate(Builder $query, array $options = [])

{

$dirty = $this-getDirty();

if (count($dirty) 0) {

if ($this-fireModelEvent('updating') === false) {

return false;

}

if ($this-timestamps Arr::get($options, 'timestamps', true)) {

$this-updateTimestamps();

}

$dirty = $this-getDirty();

if (count($dirty) 0) {

$numRows = $this-setKeysForSaveQuery($query)-update($dirty);

$this-fireModelEvent('updated', false);

}

}

return true;

}

看看更新干了什么,

首先如果这个模型dirty了,也就是脏了,也就是有属性改变了,那么才需要更新

先触发updating,更新失败了就false,

更新时间戳

这里又判断了count($dirty) 0,为什么??更updateTimestamps有什么关系吗?没有仔细看,以后再说吧

然后执行update,也就是写入了数据库

最后触发updated

那么如果这个对象是新建的,也就是需要执行插入操作,performInsert也做了差不多的事,

触发creating,

执行insert,插入数据库,

最后created。

最后回到save方法,如果更新或者插入操作成功了,那么就finishSave来结束save,

finishSave 触发了saved事件, 最后syncOriginal。

小结一下

新创建的对象,save依次触发 saving-creating-created-saved

已存在的对象,save依次触发 saving-updating-updated-saved

php eloquent 怎么安装orm

PHP框架 Laravel Eloquent ORM 批量插入数据是通过传入数组实现的。

比如:

DB::table('users')-insert(array(

array('email' = 'taylor@example.com', 'votes' = 0),

array('email' = 'dayle@example.com', 'votes' = 0),

));

以上是操作表users,执行insert语句,参数是一个数组,封装了两条数据,这里可以自定义数据,insert内部就编程批量插入了。