PHP code example of hbclare / model-helper

1. Go to this page and download the library: Download hbclare/model-helper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hbclare / model-helper example snippets


php7 artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config


#Game.php
namespace App\Models\Desktop;
use Hbclare\ModelHelper\Model;

class Game extends Model
{
    //设置表名
    protected $table = 'game';

    //设置修改字段
    protected $fillable = [
        'game_category',
        'game_name',
        'game_license_key',
        'reviewed_status',
        'active_begin_data',
        'active_end_data',
    ];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        //开启自动原子缓存,设置缓存10小时
        $this->startAutoEachCache(600);
        //开启自动分页缓存,实验方法,暂时默认设置5分钟
        $this->startAutoPageCache(5);
        //改后,需要处理的缓存
        $this->setAfterUpdateFlushKey(
            ['gameinfo_by_name_{game_name}']
        );
        //删后,需要处理的缓存
        $this->setAfterDeleteFlushKey(['gameinfo_by_name_{game_name}']);
        //新增后,需要处理的缓存
        $this->setAfterInsertFlushKey(['getGameList*']);
        //增删改,都需要处理的缓存
        $this->setFlushKeys(['gameinfo_by_name_{game_name}']);
    }
    
    public function hasManyImage()
    {
        return $this->hasMany('App\Models\CMGameImg', 'game_id');
    }

	public function getGameInfo($id){
		return $this->findOne($id);
	}

	public function changeGame($id, $game_name){
		$return $this->saveInfo(['id'=>$id, 'game_name'=>$game_name]);
	}
...
}
	

$model->getOne($id);

$model->findOne(['username'=>'testname'], ['age'=>'desc']);

$where = [
    'client_type'   => $clientType,
    'real_time'     => ['between', [$startDay, $endDay]]
];
if( !empty($provCode) && 'all' != $provCode){
    $where['prov_code'] = $provCode;
}
if( !empty($cityCode) ){
    $where['city_code'] = $cityCode;
}

$fields = [
    DB::raw('DATE_FORMAT(real_time, \'%Y-%m-%d\') as show_date') ,
    DB::raw('count(id) as kdump_num'),
    DB::raw('count(DISTINCT gid) as kdump_gid_num')
];
$predicate = [
    'groupBy' => 'show_date',
    'orderBy' => ['id'=>asc]
];
return $this->KDumpModels->getListUpgraded($where, $predicate, $fields);

$where = [];
$where['city_id'] = 207;
$where['borough_id'] = 2126;
$where['ip'] = 33333;
$where['contact_number'] = 123123;
$where['soft_name'] = $condition['soft_name'];
$where['netbar_name'] = ['or', ['like', '%123%']];
$where['netbar_address'] = ['or', ['like', '%123%']];
$where['license_key'] = ['or', ['like', '%123%']];
$mode->getList($where);

'Eloquent' => array('Illuminate\Database\Eloquent\Builder', 'Illuminate\Database\Query\Builder'),
#修改为
'Eloquent' => array('Illuminate\Database\Eloquent\Builder', 'Illuminate\Database\Query\Builder', 'Hbclare\ModelHelper\HelperQueryBuilder'),

php artisan make:eachmodel Models/User

php artisan make:repository Repository/UserRepository


#GameImg.php
namespace App\Models;

use Hbclare\ModelHelper\Model;

class GameImg extends Model {
    //设置表名
    protected $table = 'game_img';

    //设置修改字段
    protected $fillable = [
        'game_id',
        'game_name',
        'game_img_url',
    ];
    //设置外键
    protected $foreignKeyArr = [
        'game_id',
    ];
    //关闭自动更新时间戳
    #public $timestamps = false;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->startAutoEachCache(120);#设置120分钟缓存
    }

    public function belongsToGame()
    {
        return $this->belongsTo(CMGame::class, 'game_id', 'id');
    }
}