Skip to content

Instantly share code, notes, and snippets.

@qwqcode
Created October 7, 2017 14:11
Show Gist options
  • Save qwqcode/40697755641547185762c291803cd9ca to your computer and use it in GitHub Desktop.
Save qwqcode/40697755641547185762c291803cd9ca to your computer and use it in GitHub Desktop.
php 闭包
function getCategories(Request $request, Response $response, $args) {
$categoriesTable = $this - >tableCategory();
$withBooks = $request - >getParam('withBooks');
$data = [];
foreach($categoriesTable - >get() as $num = >$item) {
$data[$num] = [];
foreach($item as $key = >$value) $data[$num][$key] = $value;
// 附加
$data[$num]['update_at_format'] = date('Y-m-d H:i:s', $item - >update_at);
$data[$num]['created_at_format'] = date('Y-m-d H:i:s', $item - >created_at);
// 参数:With Books
if ( !! $withBooks) {
// 查询 Book Table
$data[$num]['books'] = (function($item) {
if (empty($item - >update_at)) return [];
$book = $this - >tableBook() - >where(['category_name' = >$item - >name, 'created_at' = >$item - >update_at]);
if (!$book - >exists()) return [];
$book = $book - >get() - >first();
// 处理 Books
$booksJson = $book - >category_book_data;
$booksArr = @json_decode($booksJson, true);
if (json_last_error() !== JSON_ERROR_NONE) return [];
$booksData = $this - >handleBooks($booksArr);
return $booksData;
})($item);
}
}
return $this - >resultTrue($response, '类目数据获取成功', ['categories_total' = >count($data), 'categories' = >$data]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment