使用RedBeansPHP获取其类别的博客文章列表的最佳方式

时间:2017-09-24 22:07:09

标签: php mysql sql redbean

我有3张这样的表:

CREATE TABLE IF NOT EXISTS blog_post (
  id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  title varchar(100) NOT NULL,
  image varchar(250),
  content TEXT NOT NULL,
  created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  edited DATETIME ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_general_ci;

CREATE TABLE IF NOT EXISTS blog_category (
  id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name varchar(100) NOT NULL UNIQUE,
  icon varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_general_ci;

CREATE TABLE IF NOT EXISTS blog_post_blog_category (
  blog_post_id int(11) NOT NULL,
  blog_category_id int(11) NOT NULL,
  CONSTRAINT blog_post_blog_category_pkey PRIMARY KEY (blog_post_id, blog_category_id),
  CONSTRAINT blog_post_blog_category_fkey FOREIGN KEY (blog_post_id) REFERENCES blog_post (id)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT blog_category_blog_post_fkey FOREIGN KEY (blog_category_id) REFERENCES blog_category (id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
) ENGINE=INNODB CHARACTER SET ascii COLLATE ascii_general_ci;

所以基本上一篇博文可以是许多类别的一部分,一个类别可以是很多帖子的一部分,非常直接,我的问题是什么是获得所有帖子列表的最佳方式。结果我期待一系列的帖子,我希望每个帖子都有这样的类别数组。

{
  "0": {
    "id": "1",
    "title": "first post",
    "image": "https://image.flaticon.com/icons/svg/102/102833.svg",
    "content": "this should be some content",
    "created": "2017-09-24 16:21:28",
    "edited": null,
    "categories": [{
      "id": 1,
      "icon": 'www.url.com',
      "name": 'category name'
    }, {
      "id": 5,
      "icon": 'www.url1.com',
      "name": 'category6 name'
    }]
  },
  "1": {
    "id": "2",
    "title": "second post",
    "image": "https://image.flaticon.com/icons/svg/102/102833.svg",
    "content": "this is some text from post",
    "created": "2017-09-24 16:29:53",
    "edited": null,
    "categories": [{
      "id": 5,
      "icon": 'www.url.com',
      "name": 'category name'
    }, {
      "id": 3,
      "icon": 'www.url1.com',
      "name": 'category6 name'
    }]
  }
}

编辑:这就是我做到的,这是正确和良好的解决方案吗?

$app->get('/blog/posts', function (Request $request, Response $response, array $args) {
    $QUERY = 'SELECT bp.id, bp.title, bc.name FROM blog_post bp 
              LEFT JOIN blog_post_blog_category bpbc ON bpbc.blog_post_id = bp.id 
              LEFT JOIN blog_category bc ON bc.id = bpbc.blog_category_id ORDER BY bp.id';

    $result = R::getAll( $QUERY );
    $resultMap = array();

    foreach ($result as $value) {
        if(empty($resultMap[$value['id']])) {
            unset($value['name']);
            $resultMap[$value['id']] = $value;
            $resultMap[$value['id']]['categories'] = array();
        }
    }

    foreach ($result as $value) {
        $category = array();
        $category['name'] = $value['name'];
        array_push($resultMap[$value['id']]['categories'], $category);
    }

    echo json_encode($resultMap);
});

0 个答案:

没有答案