Hi!

Please excuse this rather long post; my English isn't that advanced and
therefore it's ahrd to explain things the short way. Maybe I'm going to
say the same thing twice, using another expression or so, hoping you
can get the meaning out of them combined :)

To describe my problem: If I show a news item, the comments are
associated (i show them as well) and for each comment there's a user.
The news and the comments refer to the user via
   'foreignKey' => 'author_id'
because that makes more sense than user_id would do while reading and
writing the templates.

That's no big deal, though. I created a static page, so only the
sidebar contains a query to fetch the logged-in user's details.
I associated news and comments to the user via User::hasMany, changing
the foreignKey value to 'author_id' as well (as proposed in the manual
on hasMany: http://manual.cakephp.org/chapter/6).
For the sidebar there is a query for the user's data which leads to a
query for his news and comments. Using debug mode on level 2, I see the
query, and there are 3 news and 1 comment fetched. That's what there is
in my DB, so everything is fine.


Yeah, now there's a problem though.

Then I show a single news post, written by me. One of the 3 the DB
found for my user data above.
The news post is fetched and the author (refered to as 'Author' in my
relationship, so its "SELECT `Author`.`id` FROM `user` as `Author`"
instead of `User`!) can be found as well.
Remember what I told you about querying the DB for a user? Because of a
high 'recursive' value, the author's contents (news and comments)
should be fetched as well. And there's the problem (see the queries far
below).
The news item is there, then the news' author is there, but the news'
authors' comments don't show up, because the author_id is tested on
NULL isntead of an integer value. I suppose the WHERE-clause is made
like this:
    '.... WHERE `Comments`.`author_id` = ' . $someArray['User']['id']
wheras it should be 'Author' as well (because the news refered to the
user this way).

Sounds like some kind of bug to me. The tables' alias is changed in one
query, but all querys afterwards expect the default alias (`User`).
It's hard to express what I think because I lack a lot of English
vocabulary... The supposedly used array index 'User' is derived from
the model's name (class User extends AppModel), a class/model called
Author is not available but supplied by the news. I hope you can get a
glimpse of what I try to say...


Is there a way to fix this? Using "User" instead of "Author" for
describing the relationship isn't a good solution since the code will
lack readability then :(

Well, thanks in advance for any suggestions!


- Christian


The SQL queries (hopefully you all got fixed fonts :))

**** Fetching the news
SELECT `News`.`id`, [...] ,`News`.`author_id`,
       `Author`.`id`, [...] ,`Author`.`email`
  FROM `news` AS `News`
  LEFT JOIN `users` AS `Author` ON `News`.`author_id` = `Author`.`id`
 WHERE (`News`.`id` = 6)
 LIMIT 1

**** Fetching the news' author (id=2 is me)
SELECT `Author`.`id`, [...], `Author`.`email`
  FROM `users` AS `Author`
 WHERE `Author`.`id` = '2'

**** Fetching the news' author's comments
SELECT `Comments`.`id`, [...] `Comments`.`author_id`
  FROM `comments` AS `Comments` 
 WHERE `Comments`.`author_id` = NULL


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to