寻找如何显示用户的所有内容,例如新闻和文章等......所有相关的表都有共同的member_id字段(用户的id)和内容(文章的内容)。然后我还有用户表,其中包含成员的id和其他信息。我使用最新版本的CakePHP,我可以列出一个成员的所有新闻和记录以及分页但有很多重复内容,我已经尝试做一个SELECT DISTINCT和GROUP BY但我没有得到所需的结果
这是我在CakePHP中使用的PHP代码:
$this->paginate = array(
'fields' => array(
'User.id',
'User.alias',
'User.login',
'User.name',
'User.email',
'Profil.avatar',
'Profil.facebook',
'Profil.twitter',
'Profil.google_plus',
'Profil.about_me',
'News.id',
'News.alias',
'News.name',
'News.content',
'News.image',
'News.author_id',
'News.created',
'Article.id',
'Article.alias',
'Article.name',
'Article.content',
'Article.image',
'Article.author_id',
'Article.created'
),
'conditions' => array(
'User.alias' => $member,
'User.group > 0',
'User.activated' => 1,
'User.banned' => 0
),
'joins' => array(
array(
'table' => 'News',
'type' => 'LEFT',
'alias' => 'News',
'conditions' => array(
'News.author_id = User.id',
'News.online' => 1,
'News.created <= NOW()'
)
),
array(
'table' => 'Articles',
'type' => 'LEFT',
'alias' => 'Article',
'conditions' => array(
'Article.author_id = User.id',
'Article.online' => 1,
'Article.created <= NOW()'
)
)
),
'order' => array(
'News.created' => 'DESC',
'Article.created' => 'DESC'
),
'recursive' => 0,
'limit' => 12
);
$data = $this->Paginate('User');
2个SQL生成的请求:
SELECT `User`.`id`, `User`.`alias`, `User`.`login`, `User`.`name`, `User`.`email`, `Profil`.`avatar`, `Profil`.`facebook`, `Profil`.`twitter`, `Profil`.`google_plus`, `Profil`.`about_me`, `News`.`id`, `News`.`alias`, `News`.`name`, `News`.`content`, `News`.`image`, `News`.`author_id`, `News`.`created`, `Article`.`id`, `Article`.`alias`, `Article`.`name`, `Article`.`content`, `Article`.`image`, `Article`.`author_id`, `Article`.`created` FROM `matable`.`users` AS `User` LEFT JOIN `matable`.`News` AS `News` ON (`News`.`author_id` = `User`.`id` AND `News`.`online` = 1 AND `News`.`created` <= NOW()) LEFT JOIN `matable`.`Articles` AS `Article` ON (`Article`.`author_id` = `User`.`id` AND `Article`.`online` = 1 AND `Article`.`created` <= NOW()) LEFT JOIN `matable`.`profils` AS `Profil` ON (`Profil`.`user_id` = `User`.`id`) WHERE `User`.`alias` = 'test' AND `User`.`group` > 0 AND `User`.`activated` = 1 AND `User`.`banned` = 0 ORDER BY `News`.`created` DESC, `Article`.`created` DESC LIMIT 12
SELECT COUNT(*) AS `count` FROM `matable`.`users` AS `User` LEFT JOIN `matable`.`News` AS `News` ON (`News`.`author_id` = `User`.`id` AND `News`.`online` = 1 AND `News`.`created` <= NOW()) LEFT JOIN `matable`.`Articles` AS `Article` ON (`Article`.`author_id` = `User`.`id` AND `Article`.`online` = 1 AND `Article`.`created` <= NOW()) LEFT JOIN `matable`.`profils` AS `Profil` ON (`Profil`.`user_id` = `User`.`id`) WHERE `User`.`alias` = 'test' AND `User`.`group` > 0 AND `User`.`activated` = 1 AND `User`.`banned` = 0
感谢您的帮助
PS:抱歉我的英语不好。