У меня есть 7 таблиц для хранения пользовательских данных, таких как публикации, изображения, обновления, комментарии, лайки, репосты и сам пользователь.
И вот мои вопросы: Как использовать правильный запрос для выполнения объединяющей таблицы?
Я использую этот запрос:
foreach (getTimelines($page['userID'], $limit, $offset) as $row)
{
if($row['updateID'] != null) // Updating Status
{
// This status updates
}
elseif($row['postID'] != null) // Writing Article
{
// This is posts
}
elseif($row['imageID'] != null) // Uploading Image
{
// This is images
}
elseif($row['commentID'] != null) // Commented on Post
{
// This is comments
}
elseif($row['likeID'] != null) // Liking User Post
{
// This is likes
}
elseif($row['repostID'] != null) // Reposting User Post
{
// This is reposts
}
}
Когда я использую приведенный выше запрос, результаты отображаются, но я понятия не имею, как разделить типы контента. Он всегда отображается в виде обновлений статуса, и все уникальные идентификаторы, такие как postID, imageID, updateID, repostID, LikeID и commentID, имеют одинаковое значение.
У меня есть 7 таблиц для хранения пользовательских данных, таких как публикации, изображения, обновления, комментарии, лайки, репосты и сам пользователь. [img]https://i.sstatic.net/0aCVm.png[/img]
И вот мои вопросы: Как использовать правильный запрос для выполнения объединяющей таблицы? Я использую этот запрос: [code]if ( ! function_exists('getTimeline')) { function getTimelines($contributor = null, $limit = 10, $offset = 0) { $CI =& get_instance(); $CI->db->select(' abycms_posts.*, abycms_images.imageID, abycms_images.contributor as owner, abycms_images.imageContent, abycms_images.imageFile, abycms_images.imageSlug, abycms_images.timestamp as date, abycms_images.visits_count as visits, abycms_updates.updateID, abycms_updates.userID as updater, abycms_updates.updateContent, abycms_updates.visibility, abycms_updates.timestamp as update_time, abycms_likes.likeID, abycms_likes.userID as userLike, abycms_likes.type as likeType, abycms_likes.timestamp as like_time, abycms_comments.commentID, abycms_comments.userID as commentUser, abycms_comments.type as commentType, abycms_comments.timestamp as comment_time, abycms_reposts.repostID, abycms_reposts.userID as repostUser, abycms_reposts.type as repostType, abycms_reposts.timestamp as repost_time '); $CI->db->from('abycms_users'); $CI->db->join('abycms_posts', 'abycms_posts.contributor = abycms_users.userID', 'left'); $CI->db->join('abycms_images', 'abycms_images.contributor = abycms_users.userID', 'left'); $CI->db->join('abycms_updates', 'abycms_updates.userID = abycms_users.userID', 'left'); $CI->db->join('abycms_likes', 'abycms_likes.userID = abycms_users.userID', 'left'); $CI->db->join('abycms_comments', 'abycms_comments.userID = abycms_users.userID', 'left'); $CI->db->join('abycms_reposts', 'abycms_reposts.userID = abycms_users.userID', 'left'); $CI->db->where('abycms_users.userID', $contributor); $CI->db->limit($limit, $offset);
// How to order results by newest `timestamp` for posts, images, updates, comments, likes or reposts? $CI->db->order_by('abycms_posts.timestamp', 'desc');
// How to handle not duplicate `postID` or `imageID` also group it by different `type`s? $CI->db->group_by('abycms_posts.postID, abycms_images.imageID');
$query = $CI->db->get();
if($query->num_rows() > 0) { return $query->result_array(); } else { return array(); } } } [/code] И я считаю, что можно обрабатывать результаты разного типа: [code]foreach (getTimelines($page['userID'], $limit, $offset) as $row) { if($row['updateID'] != null) // Updating Status { // This status updates } elseif($row['postID'] != null) // Writing Article { // This is posts } elseif($row['imageID'] != null) // Uploading Image { // This is images } elseif($row['commentID'] != null) // Commented on Post { // This is comments } elseif($row['likeID'] != null) // Liking User Post { // This is likes } elseif($row['repostID'] != null) // Reposting User Post { // This is reposts } } [/code] Когда я использую приведенный выше запрос, результаты отображаются, но я понятия не имею, как разделить типы контента. Он всегда отображается в виде обновлений статуса, и все уникальные идентификаторы, такие как postID, imageID, updateID, repostID, LikeID и commentID, имеют одинаковое значение.