Answer the question
In order to leave comments, you need to log in
What is the best way to pull tags to posts, in one request or in a set?
Hello everyone)
Standard situation. Posts + tags to them. Looking for the best choice of tags.
So far, options 2.
1. Pull everything with one request and convert it to JSON in the SQL query itself ( CONCAT + GROUP_CONCAT ).
2. Pull tags for a post in a loop.
In general, I need ideas)
Answer the question
In order to leave comments, you need to log in
There are a lot of options here:
You can pull with one query with JOIN.
You can make 2 requests: the 1st selection of posts, then we collect the id of the posts and select tags using them with the second request. In principle, relays in yii work like this by default (by the way, can you use them?)
In the variant with group_concat, note that it is limited by default in the length of the resulting string. The request will not fail, it will simply truncate the tag string.
If there is no complicated work with tags (for example, collecting statistics), then you can generally save them in the posts table in some form (for example, separated by a space or in a json array)
In a loop - a very bad idea, but ... In principle, it does not matter at all how you do it - the main thing is that there is enough performance. If you do a deliberately long version (like a request in a loop). that is, most likely, sooner or later you will have to redo it, then it is important to make some abstract way to get tags from the post in the code, for example, $post->getTags() and use it so that when you change the way to get tags, you don’t have to rewrite code where they are used.
Personally, I would save posts in the table, or use relays...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question