我最近没有这样做,但在2014年,在一些需要额外表格的WordPress项目中,我使用了Laravel组件。
在WordPress中,如果您想使用wpdb和mysql查询创建自己的表。但通过使用Laravel数据库类,我成功地做到了这一点。
简要介绍我是如何做到这一点的:(注意:当时,我使用了Laravel 4组件,但还没有尝试使用5组件)
"require": {
"illuminate/database": "*",
"illuminate/container": "*",
"shuber/curl": "dev-master",
"sunra/php-simple-html-dom-parser": "v1.5.0"
},
我在函数中安装了必要的包(前两个)和composer。php(或在功能插件中)
我用它来保存杂志文章内容的索引,一个Recorder类(抱歉,不是很可靠的方法)
<?php
namespace Dion\\Volumes;
use Illuminate\\Database\\Capsule\\Manager as Capsule;
class Recorder {
public static $table = \'volume_content\';
public function __construct(Page $page){
$this->recordPage($page);
}
public static function createTables()
{
if( ! Capsule::schema()->hasTable(static::$table) ) {
Capsule::schema()->create(static::$table, function($table) {
$table->increments(\'id\');
$table->string(\'volume\');
$table->integer(\'month\');
$table->integer(\'year\');
$table->integer(\'page\');
$table->longtext(\'page_content\');
});
}
}
public function recordPage($page)
{
//check if exists
$exists = Capsule::table(static::$table)->where(\'volume\',$page->volume)
->where(\'page\',$page->page)->get();
if( empty( $exists ) ) {
$insert = Capsule::table(static::$table)->insert($page->toArray());
} else {
$update = Capsule::table(static::$table)->where(\'volume\',$page->volume)
->where(\'page\',$page->page)
->update($page->toArray());
}
}
}
在函数中。php我添加了Laravel数据库组件,并使其像这样工作。
global $wpdb;
$capsule = new Capsule;
$capsule->addConnection(array(
\'driver\' => \'mysql\',
\'host\' => DB_HOST,
\'database\' => DB_NAME,
\'username\' => DB_USER,
\'password\' => DB_PASSWORD,
\'prefix\' => $wpdb->prefix,
\'charset\' => DB_CHARSET,
\'collation\' => \'utf8_unicode_ci\',
));
$capsule->bootEloquent();
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
//this one creates table if not exists. Its better to run it in a WP action, like admin_init
$createVolumes = Dion\\Volumes\\Recorder::createTables();
希望有帮助,如果您喜欢此解决方案并有疑问,我很乐意补充更多。