Bonjour,
Je cherche comment faire pour afficher tout les contenus d'un utilisateur, c'est à dire les news qui l'a écrit mais également les dossiers etc... Toutes les tables en question possèdent en commun les champs member_id (id du membre) et content (contenu de l'article). Puis, j'ai également la table users qui contient donc l'id du membre et les autres informations. J'utilise la dernière version de CakePHP, et j'arrive à lister tout les news et dossiers d'un membre en même temps avec la pagination mais il y a plein de doublons, j'ai déjà essayer de faire un SELECT DISTINCT et un GROUP BY mais je n'obtient pas le résultat voulu.
Voici donc le code PHP que j'utilise dans CakePHP :
$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');
Voici les deux requêtes SQL générer :
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
Merci d'avance de votre aide.
Cordialement
Pourquoi ne pas tout simplement créer une table articles_news_profiles_users(id, article_id, new_id, profile_id, user_id) ?