Hey, I am back,

So, I found out something interesting, This is everything.

First, It is only a problem when an object belongs to another object.
Example, Post and Comment and the Comment has a post_id.

Now, what I had in the view looked like the following...

<form method="post" action="<?php echo $html->url('/posts/add/'.
$params['pass'][0]) ?>">

  <?php echo $html->hidden('Comment/post_id', array('value' =>
$params['pass'][0])); ?>
  <?php echo $html->textArea('Comment/body'); ?>

</form>

This way, I had the Posts ID passed in the url, so that after the
Comment was added, the controller knew which post to redirect the user
back too.

My controller would look something like this

function add($post_id = null) {
  if ($this->Comment->save($this->data)) {
    $this->redirect('/posts/view/'.$post_id);
  }
}

And for some reason, only when there is a comment in the database in
which the id and post_id are the same (id = 2, post_id = 2), cake will
overwrite this post instead of adding a new one.

So, if I just remove the post_id from the url in the form like this in
the view...

<form method="post" action="<?php echo $html->url('/posts/add') ?>">

  <?php echo $html->hidden('Comment/post_id', array('value' =>
$params['pass'][0])); ?>
  <?php echo $html->textArea('Comment/body'); ?>

</form>

And use the post data to redirect the user (thus removing the
parameter in the action) in the controller...

function add() {
  if ($this->Comment->save($this->data)) {
    $this->redirect('/posts/view/'.$this->data['Comment']['post_id']);
  }
}

Everything works just fine.  Really, no issues anymore!

I am very interested in this, as both methods should work, and the
first works except in a strange case.  I guess it may be a bug.

What is everyones thoughts?

On Dec 26, 10:22 pm, "Chris Hartjes" <[EMAIL PROTECTED]> wrote:
> On Dec 26, 2007 10:07 AM,RobertSosinski<[EMAIL PROTECTED]> wrote:
>
>
>
> > Hello again,
>
> > I am having this problem.  I have a Thread model and a Post model.
> > What is interesting is whenever I have a post that has the same id as
> > the thread_id (so, id = 4 and thread_id = 4), cake will no longer add
> > new records.  It will only overwrite the same record over and over
> > again (like it is editing it).
>
> > Anyone else have this problem?
>
> > Thanks.
>
> I would suggest looking at two things:
>
> 1) make sure your associations are set properly
> 2) make sure you do $this->ModelNameHere->create() before you do a
> save.  That way you can insure a new record is supposed to be created.
>
> I also agree with davidgregan's suggestion about making sure you have
> the id field for your tables either an auto-increment field or
> generate your own unique ID's
>
> Hope that helps.
>
> --
> Chris Hartjes
>
> My motto for 2007:  "Just build it, damnit!"
>
> @TheKeyboard -http://www.littlehart.net/atthekeyboard

--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to