controllers index function with related tables.

2010-06-25 Thread pswiggers
Hi,

I'm new to cakephp , still really a lot to learn about it, but already
convinced cake is a good thing.

So this is what I'm struggling with;
On my website I can list all my posts via the index function ($this-
Post-recursive = 0).
But I've also comments and tags I already want to show on the index;
meaning number of comments (a count) and a list of tags. I can see
this data via the view function, but that requires an ID. As I can see
all data in the view for an ID I assume I build the tables (and
models) following cakephp convention.
Long story short:  How could I to use the index but with related
information or the view but for all posts (not specific ID)?


Kind regards,
Patrick


Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: controllers index function with related tables.

2010-06-25 Thread Jeremy Burns | Class Outfit
Welcome to Cake.

The secret is to (in this order):
1) build your database tables to the conventions (lower case plural table 
names, `id` and `name` fields etc)
2) create indexes and referential integrity in the database
3) build your Cake models - you can use Bake for this as its pretty good, or do 
it yourself
4) learn and implement the Containable behaviour (it's much more powerful, 
controllable and friendly than relying on recursive)

Your Cake models echo the database joins using $belongsTo, $hasMany and 
$hasAndBelongsToMany. So, to use the good old blog model, a Post can have many 
comments ($hasMany), a Comment belongs to a Post ($belongsTo), a Tag has many 
Posts and a Post has many Tags ($hasAndBelongsToMany), and so on. To see your 
Posts with associated Comments and Tags you do this search:

$posts = $this-Post-find(
'all',
array(
'contain' = array(
'Comment',
'Tag'
)
)
);

This will give you an array with an element for each Post, and nested in it an 
array with its Comments and Tags. You can then read these out and display them 
on your index view. To get the count of comments you can either do a count of 
comments from within the array, or use the counterCache function. This adds a 
comment_count field to your posts table and Cake will keep that up to date as 
Comments are added and removed to the database through the website (it can't 
track changes made directly in the database, or course).

Pages that might help:

http://book.cakephp.org/view/903/Model-and-Database-Conventions
http://book.cakephp.org/view/1323/Containable
http://book.cakephp.org/view/1042/belongsTo (has some details on counterCache)


Jeremy Burns
Class Outfit

jeremybu...@classoutfit.com
http://www.classoutfit.com

On 25 Jun 2010, at 12:30, pswiggers wrote:

 Hi,
 
 I'm new to cakephp , still really a lot to learn about it, but already
 convinced cake is a good thing.
 
 So this is what I'm struggling with;
 On my website I can list all my posts via the index function ($this-
 Post-recursive = 0).
 But I've also comments and tags I already want to show on the index;
 meaning number of comments (a count) and a list of tags. I can see
 this data via the view function, but that requires an ID. As I can see
 all data in the view for an ID I assume I build the tables (and
 models) following cakephp convention.
 Long story short:  How could I to use the index but with related
 information or the view but for all posts (not specific ID)?
 
 
 Kind regards,
 Patrick
 
 
 Check out the new CakePHP Questions site http://cakeqs.org and help others 
 with their CakePHP related questions.
 
 You received this message because you are subscribed to the Google Groups 
 CakePHP group.
 To post to this group, send email to cake-php@googlegroups.com
 To unsubscribe from this group, send email to
 cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
 http://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: controllers index function with related tables.

2010-06-25 Thread cricket
On Fri, Jun 25, 2010 at 12:47 PM, Jeremy Burns | Class Outfit
jeremybu...@classoutfit.com wrote:

 To get the count of comments you can either do a
 count of comments from within the array, or use the counterCache function.

If you don't go with counterCache you might want to adjust the
contains array so that you're only fetching the ID for each comment.

$posts = $this-Post-find(
'all',
array(
'contain' = array(
'Comment' = array(
'fields' = array(
'Comment.id'
)
),
'Tag'
)
)
);

This way, you're not fetching a bunch of unnecessary data and you can
use sizeof() to get the number of comments for each post.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en