Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony][Propel]left join + cpunt
Silver_Fletcher
post
Post #1





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 20.12.2010

Ostrzeżenie: (0%)
-----


Mam w bazie 3 tabele:

Users (user_id, ...)
News (news_id, user_id ref users, ...)
comments (comment_id, ...)

Zamierzam skonstrować tego typu zapytanie:
  1. SELECT news.*, users.*, COUNT(comments.comment_id) AS nb_comment
  2. FROM news
  3. LEFT JOIN users ON (news.USER_ID=users.USER_ID)
  4. LEFT JOIN comments ON (news.NEWS_ID=comments.ID_OF AND comments.TO_WHAT=1 AND comments.VISIBLE=1)


Napisałem kod:
  1. $c = new Criteria();
  2. $c->addMultipleJoin(array(
  3. array(NewsPeer::NEWS_ID, CommentsPeer::ID_OF),
  4. array(CommentsPeer::TO_WHAT, '1'),
  5. array(CommentsPeer::VISIBLE, '1')
  6. ), Criteria::LEFT_JOIN);
  7. //c->addAsColumn('nb_comments', 'Count(' . CommentsPeer::COMMENT_ID . ')');
  8. $this->Newss = NewsPeer::doSelectJoinUsers($c);

Ten kod działa, dopóki nie odkomentuję linii, gdzie zliczam komentarze. Gdy to zrobię propel generuje zapytanie, które pobiera tylko tą jedną kolumnę, nie pytając o wszystkie dane z tabel news i users. Jak to naprawić?


//OK, jakoś to poprawiłem. Ale jak teraz przy wyświetlaniu w template:
  1. foreach($Newss as $news){
  2. ...
  3. }
dostać się do kolumny z ilością komentarzy (nb_comments)?

Ten post edytował Silver_Fletcher 20.12.2010, 16:12:29
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 14:20