For example, I have 3 sheets.
Article (id,title,author,content)-Article table
tags (id,name)-label table
Rationship (article_id,tags_id)-Article Label Association table
The goal is to get a result set, with the result set containing the label.
The original approach:
$articleList = db::table (' article ')->get (); Get all articles
Article Information collation
for($i = 0;$i < count($articleList);$i++){ $artagslist = $articletagsModel->getRelateTags($articleList[$i]->id);//获取关系表中对应文章id的所有标签id if(isset($artagslist)) { $tagsArray = array(); foreach ($artagslist as $values) { //遍历标签id,从标签表中查找结果.. $singletags = $tagsModel->getTagsById($values->tags_id); //把结果追加到数组$tagsArray中.. array_push($tagsArray, $singletags); } $articleList[$i]->tags = $tagsArray; //在最开始的数据增加一个字段保存标签数组 }}
The last array to get is as follows:
Array:1 [
0 = {#176
+"id": "1"+"title": "这是标题"+"author": "这是作者"+"content": "这是文章正文"+"tags": array:2 [▼ 0 => {#182 ▼ +"id": 5 +"name": "nodejs" +"create_time": null +"update_time": null +"user_id": null +"num": 1 } 1 => {#183 ▼ +"id": 4 +"name": "python" +"create_time": null +"update_time": null +"user_id": null +"num": 1 }]
}
]
Although my original practice can achieve the goal, but always feel that this approach is a bit redundant, so I would like to ask if there is a better way? Or, you can solve this problem directly with a SQL sentence. Thank you!
Reply content:
For example, I have 3 sheets.
Article (id,title,author,content)-Article table
tags (id,name)-label table
Rationship (article_id,tags_id)-Article Label Association table
The goal is to get a result set, with the result set containing the label.
The original approach:
$articleList = db::table (' article ')->get (); Get all articles
Article Information collation
for($i = 0;$i < count($articleList);$i++){ $artagslist = $articletagsModel->getRelateTags($articleList[$i]->id);//获取关系表中对应文章id的所有标签id if(isset($artagslist)) { $tagsArray = array(); foreach ($artagslist as $values) { //遍历标签id,从标签表中查找结果.. $singletags = $tagsModel->getTagsById($values->tags_id); //把结果追加到数组$tagsArray中.. array_push($tagsArray, $singletags); } $articleList[$i]->tags = $tagsArray; //在最开始的数据增加一个字段保存标签数组 }}
The last array to get is as follows:
Array:1 [
0 = {#176
+"id": "1"+"title": "这是标题"+"author": "这是作者"+"content": "这是文章正文"+"tags": array:2 [▼ 0 => {#182 ▼ +"id": 5 +"name": "nodejs" +"create_time": null +"update_time": null +"user_id": null +"num": 1 } 1 => {#183 ▼ +"id": 4 +"name": "python" +"create_time": null +"update_time": null +"user_id": null +"num": 1 }]
}
]
Although my original practice can achieve the goal, but always feel that this approach is a bit redundant, so I would like to ask if there is a better way? Or, you can solve this problem directly with a SQL sentence. Thank you!
SQL may not be able to do multiple nesting, if you just want the article tag is OK, but also to tag associated with the article list is not.
But there's usually no such demand? All articles listed on the page are very common, it is common to provide the brief content of the article, and it is common to list the tag of the article, but I have not seen the tag-related articles listed at the same time. Instead of trying to read all the data, you should consider the optimization of SQL based on whether the final display is needed.
In addition, from the data you listed, tag has a name enough, create time to modify the creator of such things such as this is really useless, if not some of the framework of the ID has mandatory requirements, the ID of this table is useless.
If you need tag related articles suggest using Ajax to get two times, the article uses the tag can consider the use of MySQL group_conact (like, lazy to check), the role is to concatenate the group by the content into a string. For example
Select Articles.*, Group_conact ( tag
. name
, ', ') from the articles left join tags ...
Check out the MySQL manual for yourself.
select ar.*,t.* from article as ar left join relationship as rs on ar.id = rs.article_id left join tags as t on rs.tags_id =t.id
, which is a SQL statement that is stitched together when the model operation database is called.