PHP code example of aaronjan / academe
1. Go to this page and download the library: Download aaronjan/academe 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/ */
aaronjan / academe example snippets
namespace App\Academe\Blueprints;
use Academe\BaseBlueprint;
use Academe\Casting\CasterMaker;
class User extends BaseBlueprint
{
/**
* @return string
*/
public function primaryKey()
{
return 'id';
}
/**
* @return string
*/
public function subject()
{
return 'user';
}
/**
* @return array
*/
public function castRules()
{
// 增加两个字段的类型配置:
return [
'id' => CasterMaker::integer(),
'name' => CasterMaker::string(),
];
}
}
use Academe\Contracts\Academe;
use Academe\Contracts\Writer;
use App\Academe\Blueprints\User;
class MyController {
public function index(Academe $academe, Writer $writer) {
$userMapper = $academe->getMapper(User::class);
// 查询所有数据,按年龄正序排列
$userMapper->query()->sort('age', 'asc')->all();
// 条件查询并指定字段
$userMapper->query()->equal('id', 1)->first(['name']);
// 查询null值
$userMapper->query()->isNull('money')->notNull('mortgage')->all();
// 新增数据
$userMapper->query()->create([
'name' => 'John',
]);
// 更新数据
$userMapper->query()->equal('id', 1)->update([
'score' => 100,
]);
// 更新数据(更新值使用原生SQL,同样支持MongoDB)
$userMapper->query()->equal('id', 1)->update([
'score' => $writer->raw('`score` + 1'),
]);
// 删除数据
$userMapper->query()->equal('id', 2)->delete();
}
}
use Academe\Contracts\Academe;
use Academe\Contracts\Writer;
use App\Academe\Blueprints\User;
class MyController {
// Writer 实例可以通过依赖注入的方式获取,或者调用 `$academe->getWriter()`
public function index(Academe $academe, Writer $writer) {
$userMapper = $academe->getMapper(User::class);
// 单个查询条件
$condition = $writer->equal('id', 1);
$userMapper->query()->apply($condition)->all();
// 组合查询条件:and
$conditions = $writer->must([
$writer->equal('id', 1),
$writer->notEqual('name', 'John'),
]);
// 组合查询条件:or
$conditions = $writer->any([
$writer->equal('id', 1),
$writer->notEqual('name', 'John'),
]);
// query builder形式,甚至可以在这里指定需要取出的字段
$statement = $writer->query()->equal('id', 1)->fields(['id', 'name']);
$userMapper->query()->apply($statement)->all();
}
}
use Academe\Contracts\Academe;
use Academe\Contracts\Writer;
use App\Academe\Blueprints\Post;
class MyController {
// Writer 实例可以通过依赖注入的方式获取,或者调用 `$academe->getWriter()`
public function index(Academe $academe, Writer $writer) {
$postMapper = $academe->getMapper(Post::class);
$postMapper->query()
// 依然支持条件过滤
->greaterThan('level', 10)
// 也支持排序
->sort('channel_id', 'asc')
->group(
// 聚合条件字段
[
'channel_id',
],
// 值
[
// `count()` 方法得到的值默认是整数,其他方法默认是保留2位小数的浮点数(`BigDecimal` 对象)
'post_count' => $writer->accumulation()->count(),
// 你可以让其他方法也返回整数:
'like_total' => $writer->accumulation()->sum('like_total')->asInteger(),
// 你也可以调整浮点数的精度:
'avg_like' => $writer->accumulation()->avg('like_total')->asDecimal(4),
'min_like' => $writer->accumulation()->min('like_total'),
'max_like' => $writer->accumulation()->max('like_total'),
],
// 支持 limit 和 offset
// Limit:
10,
// Offset:
20
);
// 如果你希望在查询 MySQL 的时候获取更大的灵活性,也可以使用 SQL 语句:
$postMapper->query()
// 依然支持条件过滤
->greaterThan('level', 10)
->group(
// 聚合条件字段
[
// 你还可以转换结果的格式
'channel_id' => [
'channel_id',
CasterMaker::string(),
],
// 更换字段名并指定数据格式
'update_date' => [
$writer->raw('UNIX_TIME(updated_at, "YYYY-mm-dd")'),
CasterMaker::string(),
],
],
// 值
[
'like_total' => [
$writer->raw('SUM(`like_total`)'),
CasterMaker::integer(),
],
]
);
// MongoDB 同样也支持使用原生查询:
$postMapper->query()
// 依然支持条件过滤
->greaterThan('level', 10)
->group(
// 聚合条件字段
[
'channel_id' => [
$writer->raw('$channel_id'),
CasterMaker::string(),
],
],
// 值
[
'like_total' => [
$writer->raw(['$sum' => '$like_total']),
CasterMaker::integer(),
],
]
);
}
}
namespace App\Academe\Blueprints;
use Academe\BaseBlueprint;
use Academe\Casting\CasterMaker;
use Academe\Relation;
class User extends BaseBlueprint
{
/**
* @return string
*/
public function primaryKey()
{
return 'id';
}
/**
* @return string
*/
public function subject()
{
return 'user';
}
/**
* 这里定义数据关系
*
* @return array
**/
public function relations()
{
return [
// 假设我们还有一个名为 Post 的 Blueprint
'posts' => new Relation\HasMany(Post::class, 'user_id', 'id'),
];
}
/**
* @return array
*/
public function castRules()
{
// 增加两个字段的类型配置:
return [
'id' => CasterMaker::integer(),
'name' => CasterMaker::string(),
];
}
}
use Academe\Contracts\Academe;
use Academe\Contracts\Writer;
use Academe\Statement\RelationSubStatement;
use App\Academe\Blueprints\User;
class MyController {
// Writer 实例可以通过依赖注入的方式获取,或者调用 `$academe->getWriter()`
public function index(Academe $academe, Writer $writer) {
$userMapper = $academe->getMapper(User::class);
// 关联所有数据
$users = $userMapper->query()->with('posts')->all();
$posts = array_first($users)->posts;
// 带条件关联所有数据
$users = $userMapper->query()->with([
'posts' => function (RelationSubStatement $statement) {
// 添加关联查询条件
// 注意:这里如果要指定查询字段,一定要包含定义关系时所需要的字段,否则会导致数据无法关联
// 同样,用来聚合的根数据也需要包含定义关系的字段
$statement->equal('state', 2)->fields(['id', 'user_id']);
},
])->all();
}
}
namespace App\Academe\Bonds;
use Academe\BaseBond;
use Academe\Casting\CasterMaker;
use Academe\Relation;
use App\Academe\Blueprints;
class UserAndPost extends BaseBond
{
/**
* @return string
*/
public function primaryKey()
{
return 'id';
}
/**
* @return string
*/
public function subject()
{
return 'relation_user_and_post';
}
/**
* @return string
**/
public function hostBlueprintClass()
{
return Blueprints\User::class;
}
/**
* @return string
**/
public function hostKeyField()
{
return 'id';
}
/**
* @return string
**/
public function guestBlueprintClass()
{
return Blueprints\Post::class;
}
/**
* @return string
**/
public function guestKeyField()
{
return 'id';
}
/**
* @return array
*/
public function castRules()
{
return [
// eg: 'name' => CasterMaker::string()
];
}
}
use Academe\Contracts\Academe;
use Academe\Contracts\Writer;
use Academe\Statement\RelationSubStatement;
use App\Academe\Blueprints\User;
class MyController {
// Writer 实例可以通过依赖注入的方式获取,或者调用 `$academe->getWriter()`
public function index(Academe $academe, Writer $writer) {
$userMapper = $academe->getMapper(User::class);
// Academe 和 Writer 都可以创建 Transaction 对象
$transaction = $writer->newTransaction();
// 创建时可以指定隔离级别:
$transaction = $academe->newTransaction(\Academe\Constant\TransactionConstant::READ_UNCOMMITTED);
// Transaction 可以统一设置查询锁:
$transaction = $transaction->lockSelect();
// 将所有和事务相关的 Mapper 加入到事务对象中(关系数据的 Mapper 也需要手动加入):
$userMapper->involve($transaction);
// 开始事务:
$transaction->begin();
try {
// 对数据进行操作:
// ...
$transaction->commit();
} catch (\Exception $exception) {
$transaction->rollback();
throw $exception;
}
}
}
$mapper->queryAsMongoDB()->elementMatch('array', $writer->query()->equal('inner_field', 'value'))->all();
$ php artisan vendor:publish --provider=Academe\\Laravel\\AcademeServiceProvider