Re: Is there a good solution for this?
Try something like this: $stories = $this-Story-findAll(array('complete' = 0), null, null, null, null, -1); foreach ($stories as $story) { $comments = $this-Story-Comment-findAll(array('story_id' = $story['Story']['id']), 'content', 'created_at DESC', $story['viewablecomments'], null, -1); $story += array('Comments' = $comments); } $this-set(compact('stories')); Now bear in mind I haven't tried this and I'm just typing code into a little textarea, so I've probably got it slightly wrong... but here's what it should do: First, fetch all the stories, as you were doing it. It's in a sensibly named array, on a fairly easy to read line that does nothing else. Then loop through every story. The $story bit, with the ampersand, means that as it's looping, the code in the loop can alter the story rather than just passively view it. The loop itself builds up an array of comments related to just that story. I think your use of the array() function in this line is unnecessary, though it's too late in the night for me to work out if it'd cause a problem or not. :) Also, $story['story']['id'] should be $story['Story']['id'] as model names are always UpperCamelCase, but again, I'm not sure if that would cause a problem or not. What the loop then does is takes all these comments and appends them to the appropriate row of the original $stories array. (Again, it's late, so I may not have this quite right, but hopefully you should be able to see what I'm trying to do here.) This way, in your view's $stories foreach, you'll already be able to use $story['Comments'] as a handy list of comments just related to that particular story - which is probably easier than what's currently in your view, finding stories in the $comments array that have the right index. Although this is probably a bit neater, I'm still not entirely sure why your original code failed, but maybe this will have better luck? Let me know how it goes! It took me ages to wrap my head around arrays at first, so if you're new to PHP you're doing well already! Hope that helps, Zoe. --~--~-~--~~~---~--~~ 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?hl=en -~--~~~~--~~--~--~---
Re: Is there a good solution for this?
AFAIK you can't use variable after LIMIT so u can't get your result set in one call. I would make model function which at first will retrieve visiblecomments number and then use it as value for 'limit' key in options array for find function. On 9. Feb, 03:18 h., freespac [EMAIL PROTECTED] wrote: I have to tables, stories and comments. They have a relationship story has many comments. comment belongs to story. In stories, I have a field named visiblecomments (yes it's long ;) I want to return only a number of comments (that is defined in visiblecomments) in every story. Example: Story 1: id: 1 title: this is first story. visiblecomments: 2 has 4 comments. Story 2: id: 2 title: this is second story visiblecomments: 1 has 5 comments. I want it to return the following: 2 comments from the first story and 1 comment from the second story. hope that made any sense.. -thanks --~--~-~--~~~---~--~~ 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?hl=en -~--~~~~--~~--~--~---
Re: Is there a good solution for this?
I want to return only a number of comments (that is defined in visiblecomments) in every story. This seems like an odd way of doing it, as you'd be able to specify how many comments to retrieve, but not which ones... but yes, it's certainly possible. In your story controller, if your use of findAll is set to recurse, it will automatically pull out all of the comments relating to that story. Presumably, you're using a foreach on this information in the view, so you could just limit it to stop once the visiblecomments value has been reached. There are more advanced ways of doing it, such as overriding afterFind in the stories model to append the right number of comments, but it's probably easier to do it the first way instead. :) Hope that helps, Zoe. --~--~-~--~~~---~--~~ 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?hl=en -~--~~~~--~~--~--~---
Re: Is there a good solution for this?
Ummm it does give allot of overhead to request all comments and only display a few... especially on a high traffic site. On Feb 9, 8:00 pm, MonkeyGirl [EMAIL PROTECTED] wrote: I want to return only a number of comments (that is defined in visiblecomments) in every story. This seems like an odd way of doing it, as you'd be able to specify how many comments to retrieve, but not which ones... but yes, it's certainly possible. In your story controller, if your use of findAll is set to recurse, it will automatically pull out all of the comments relating to that story. Presumably, you're using a foreach on this information in the view, so you could just limit it to stop once the visiblecomments value has been reached. There are more advanced ways of doing it, such as overriding afterFind in the stories model to append the right number of comments, but it's probably easier to do it the first way instead. :) Hope that helps, Zoe. --~--~-~--~~~---~--~~ 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?hl=en -~--~~~~--~~--~--~---
Is there a good solution for this?
I have to tables, stories and comments. They have a relationship story has many comments. comment belongs to story. In stories, I have a field named visiblecomments (yes it's long ;) I want to return only a number of comments (that is defined in visiblecomments) in every story. Example: Story 1: id: 1 title: this is first story. visiblecomments: 2 has 4 comments. Story 2: id: 2 title: this is second story visiblecomments: 1 has 5 comments. I want it to return the following: 2 comments from the first story and 1 comment from the second story. hope that made any sense.. -thanks --~--~-~--~~~---~--~~ 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?hl=en -~--~~~~--~~--~--~---
Re: Is there a good solution for this?
On Feb 9, 2008 7:48 AM, freespac [EMAIL PROTECTED] wrote: 2 comments from the first story and 1 comment from the second story. Without having a flag in the comments for visibility I don't think there is a way to return only the visible comments T -- = Cheesecake-Photoblog: http://cheesecake-photoblog.org PHP for E-Biz: http://sanisoft.com = --~--~-~--~~~---~--~~ 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?hl=en -~--~~~~--~~--~--~---