Hi,

I am very new to Cake and have searched for an answer to this but
can't quite get my head around it enough to ask ask the right
question.  I'm hoping someone here can point me in the right
direction.

I have 2 tables, Users and Comments.  Each User has many comments.  I
have defined the Comments table as follows:
Field                    Type
id                       int(11)          auto_increment
title                    varchar(150)
comment          text
user_id          int(11)
comment_id       int(11)

The comment_id refers back to the id field in the same table to track
the parent comment vs the child comment.  The problem is when I
retrieve
the data the user_id and user table aren't joined for the
childcomments.


An example of the data for a
parent comment might be:
id:   1
Title:  This is a parent Comment Title
comment:  This is the first comment.  The Parent.
user_id:  1
comment_id:

The Child comment of this might be:
id:   2
Title:
comment:  This is the child comment to comment number 1
user_id:  121
comment_id:  1

The comment_id is 1 to indicate this comment belongs to the parent
comment with ID = 1

My user.php model is:
class User extends AppModel {
var $name = 'User';

        var $hasMany = array(
                'Comment' => array(
                        'className' => 'Comment',
                        'foreignKey' => 'user_id',
                        'dependent' => false
                )
        );
}

My comment.php model is:
class Comment extends AppModel {
        var $name = 'Comment';
        var $belongsTo = array(
                'User' => array(
                        'className' => 'User',
                        'foreignKey' => 'user_id'
                ),
                'ParentComment' => array(
                        'className' => 'Comment',
                        'foreignKey' => 'comment_id'
                ),
        );


        var $hasMany = array(
                 'ChildComment' => array(
                         'className' => 'Comment',
                         'foreignKey' => 'comment_id'
                )
        );
}


The comments controller view action is as follows:
function view($id = null) {
        if (!$id) {
                $this->Session->setFlash(__('Invalid Comment.', true));
                $this->redirect(array('action'=>'index'));
        }
        $this->set('comment', $this->Comment->read(null, $id));
}



The debug($comment) results in the following:
Array
(
    [Comment] => Array
        (
            [id] => 1
            [title] => New Comments Site
            [comment] => Hi all,
I am working on a new comments site.  Please be patient while I get
everything working properly.
Thanks
            [user_id] => 1
            [comment_id] => 0
        )

    [User] => Array
        (
            [id] => 1
            [login] => gedm
            [fullname] => gedm
            [role_id] => 1
            [password] => 245cb65084c076918943eaeb73dec56f2e1a3e54
            [email] => [email protected]
            [active] => 1
        )

    [ParentComment] => Array
        (
            [id] =>
            [title] =>
            [comment] =>
            [user_id] =>
            [comment_id] =>
        )

    [ChildComment] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [title] => Sub comment
                    [comment] => This is a sub comment of one.
                    [user_id] => 2
                    [comment_id] => 1
                )
        )
)

Any help or suggestions would be appreciated.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to