Hello, I've been trying out Cake since yesterday and so far it seems to
be the best framework i've ever used. I started off with the blog
tutorial and it works just fine. I tried to extend my application by
making a Comment system. The problem is that I cant figure out how to
store comments with a foreign key (post_id)

Here's my database SQL (added to the one in the tutorial):
----------------------------------------------------------------------------------------
/* The comments table */
CREATE TABLE comments (
    id INT(11) AUTO_INCREMENT NOT NULL,
    name VARCHAR(50),
    email VARCHAR(50),
    url VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT NULL,
    post_id INT(11),
PRIMARY KEY (id)
FOREIGN KEY (post_id) REFERENCES posts (id)
);

/* Test comments */
INSERT INTO comments (name,email, URL, body, created, post_id)
    VALUES ('Administrator', '[EMAIL PROTECTED]', 'http://ectu.net',
'This is the first comment ever. Yay!', NOW(), '1');
INSERT INTO comments (name,email, URL, body, created, post_id)
    VALUES ('Azad', '[EMAIL PROTECTED]', 'http://globalww.net', 'I
agree with that', NOW(), '1');
INSERT INTO comments (name,email, URL, body, created, post_id)
    VALUES ('Azad', '[EMAIL PROTECTED]', 'http://ectu.net', 'A comment
on the second post', NOW(), '2');
----------------------------------------------------------------------------------------


After dumping the SQL, I coded the CommentsController and used the
$this->requestAction to view the comments in the same page as the
posts.

Here's the View function from PostsController
----------------------------------------------------------------------------------------
function view($id = NULL)
        {
                $this->Post->id = $id;
                $this->set('post', $this->Post->read());

                $this->set('comments',$this->requestAction('comments/view/'.$id,
array('return')));

                
$this->set('commentform',$this->requestAction('comments/add/'.$id,
array('return')));
        }
----------------------------------------------------------------------------------------

And here's the part i coded in the /posts/view.thtml
----------------------------------------------------------------------------------------
<?php if(isset($comments)) echo $comments ?>
</p>

<p class="addcomment">
<?php echo $commentform;?>
---------------------------------------------------------------------------------------------------------------------


I am able to view the comments and also a form for adding comments. So
far so good. I wont paste the View function CommentsController as this
post is already too big.

Now the real problem arises, I couldnt add comments into the database.
I coded the Add function in the CommentsController. Here it is:
----------------------------------------------------------------------------------------
function add($post_id)
        {
                if(!empty($this->data))
                {
                        $this->Comment->save($this->data);

                        $this->redirect('/posts/view/'. $post_id);
                } else {
                        $this->set('post_id', $post_id);
                }
        }
----------------------------------------------------------------------------------------


And here's the add.thtml from views/comments/
----------------------------------------------------------------------------------------
<h1>Add Comment</h1>

<form method="post" action="<?php echo
$html->url('/comments/add/'.$post_id); ?>">
        <dl>
                <dt>Name:</dt>
                <dd>
        <?php echo $html->input('Comment/name', array('size' => '40'))?>
                <?php echo $html->tagErrorMsg('Comment/name', 'Name is 
required.') ?>
                </dd>
        </dl>
        <dl>
                <dt>email:</dt>
                <dd>
        <?php echo $html->input('Comment/email', array('size' => '40'))?>
                <?php echo $html->tagErrorMsg('Comment/email', 'Email is 
required.')
?>
                </dd>
        </dl>
        <dl>
                <dt>URL:</dt>
                <dd>
        <?php echo $html->input('Comment/url', array('size' => '40'))?>
        </dd>
        </dl>
        <dl>
        <dt>Body:</dt>
        <dd>
        <?php echo $html->textarea('Comment/body', array('rows'=>'10'))
?>
                <?php echo $html->tagErrorMsg('Comment/body', 'Body is 
required.') ?>
                </dd>
        </dl>
        <?php echo $html->submit('Save') ?>
</form>
----------------------------------------------------------------------------------------

When I submit the form after adding data it doesnt create a new row in
the Comments table but replaces the the first post and the post_id is
set NULL where it is supposed to be the ID of the post the comment is
added from.

I suggest you try out my code and find a solution. We could then turn
it into a tutorial and add it to the manual.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to