themeszone/wp-orm

使用eloquent的WordPress插件ORM

安装: 11

依赖者: 0

建议者: 0

安全: 0

星星: 1

关注者: 0

分支: 1

类型:wordpress-plugin-helper

1.0.0 2020-03-31 12:01 UTC

This package is not auto-updated.

Last update: 2024-09-26 12:35:41 UTC


README

使用Eloquent的WordPress ORM

使用示例

包含的模型有

  • Post : post_type = post的WordPress帖子
  • Page : post_type = page的WordPress帖子
  • Cpt : 通过类型过滤的WordPress帖子
  • PostMeta : WordPress postmeta表的模型
  • User : WordPress users表的模型
  • UserMeta : WordPress usermeta表的模型
  • Comments : WordPress comments表的模型

获取所有页面并打印其标题

$pages = Page::all();
 
foreach($pages as $page) {

  var_dump($page->ID, $page->post_title);
}
 
Do not forget to include the use statement 
use XS\ORM\Model\Page;
use XS\ORM\Model\Post; 
... 
// Get all the page with its comment
$pages = Page::with('comments')->get();
 
foreach($pages as $page) {
    foreach($page->comments as $comment) {	
        var_dump($comment->comment_content);
    }
}
 
 
// Get all the page with its comments and meta's and titles includes the word "Sample"

$pages = Page::with('comments')->with('meta')->where('post_title', 'LIKE', '%Sample%')->get();
 
foreach($pages as $page) {
    foreach($page->comments as $comment) {
        var_dump($comment->comment_content);
    }
 
    foreach($page->meta as $item) {
        var_dump($item->meta_key, $item->meta_value);
    }
}
 
 
// Reading a specific post by its id
$post = Post::find(24);
 
// filter all the post by author and status and also at least one comment and order by ID
$posts = Post::status('publish')->author(5)->where('comment_count', '>=', 1)->orderBy('ID')->get();

 

Cpt模型指向WordPress帖子表,但不会自动过滤任何类型,在此模式下可以过滤任何类型。一些示例 -

// Get all the published posts
$posts = Cpt::post()->status('publish')->get();
 
// Get all the page with title having 'Sample' word
$posts = Cpt::page()->where('post_title', 'LIKE', '%Sample%')->get();
 
// Get all the page of a custom post type with its meta and comments - 
$posts = Cpt::type('xs-skeleton-plg')->with('meta')->with('comments')->get();
 

您也可以直接查询Post meta表 -

// Get all the meta of post id 2
$pm = PostMeta::where('post_id', 2)->get(); OR
$pm = PostMeta::post(2)->get();
 
// Get all the meta of post id 2 and meta_key = _wp_page_template
$pm = PostMeta::post(2)->key('_wp_page_template')->get();

// If you want to return only the first row then - 
$pm = PostMeta::post(2)->first();
$pm = PostMeta::post(2)->key('_wp_page_template')->first();
 

为您的自定义表创建模型

use XS\ORM\Model\XS_Model;
 
class Player extends XS_Model {

    //protected $table = 'my_player';

}

Eloquent默认将表名作为模型名称的复数形式,在这种情况下,它将指向players表,主键为id列。如果您的表有不同的名称,只需在模型中添加protected $table = 'table_name'属性。如果主键不是id,则在模型中添加protected $primaryKey = 'id';属性。

// Get all players
Player::all();
// Find player by primary key value -
Player::find(5); 
// Count total active player
Player::where('active', 1)->count();
// Find highest income of active players ;
Player::where('active', 1)->max('income');

还需要运行一些原始查询吗?

$player = DB::table('players')->where('name', 'John')->first();
echo $user->name;
 
$players = DB::table('players')->select('name', 'email as player_email')->get();
  
$players = DB::select('select * from players where active = ?', [1]);
$players = DB::select('select * from players where id = :id', ['id' => 1]);
 
DB::insert('insert into players (id, name) values (?, ?)', [1, 'Dayle']);
$affected = DB::update('update players set goal = 10 where name = ?', ['John']);
$deleted = DB::delete('delete from players');
 
DB::statement('drop table players');
  
DB::transaction(function () {
    DB::table('players')->update(['votes' => 1]);

    DB::table('posts')->delete();
});
 
$title = DB::table('roles')->pluck('title');
$roles = DB::table('roles')->pluck('title', 'name');
$price = DB::table('orders')->where('finalized', 1)->avg('price');
 
$users = DB::table('users')
                     ->select(DB::raw('count(*) as user_count, status'))
                     ->where('status', '<>', 1)
                     ->groupBy('status')
                     ->get();
 
$orders = DB::table('orders')
                ->select('department', DB::raw('SUM(price) as total_sales'))
                ->groupBy('department')
                ->havingRaw('SUM(price) > 2500')
                ->get(); 
 
//Need Join ?
$users = DB::table('users')
            ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
            ->get();
 
$users = DB::table('sizes')
            ->crossJoin('colours')
            ->get();
             
$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();
                                     

工作原理

  • Eloquent主要用于此处作为查询构建器
  • 使用WPDB运行由Eloquent构建的查询
  • 它不会创建任何额外的MySQL连接

了解更多可能性,请查看https://laravel.net.cn/docs/5.5/queries & https://laravel.net.cn/docs/5.5/eloquent