Nevermind. I was using the wrong formatting in the view, causing the
$this->data to also contain the wrong format. Even if you're only
adding a single record, you still must have the following format in
the view:
echo $form->input('Comment.0.comment');
echo $form->input('Comment.0.author');
...which creates the following format for the data array:
Array
(
[Post] => Array
(
[title] => Test Title
[body] => Test Body
[user_id] => 1
)
[Comment] => Array
(
[0] => Array
(
[comment] => Test Comment
[author] => Test Comment Author
)
)
)
...sorry for the confusion. :( I have seen similar posts, and
tutorials using the formatting that I had used. I didn't check closely
enough at the Cookbook as I had initially thought.
On Aug 12, 2:34 pm, BrendonKoz <[email protected]> wrote:
> Before I continue, I'd like to state that I am using the latest
> version of CakePHP v1.2 (not v1.3). I have not checked if the versions
> differ in their implementations for this specific method.
>
> Models:
> Post hasMany Comment
> Comment belongsTo Post
> Post hasAndBelongsToMany Tags
>
> In my tests, the controller's "add" method remains the same:
> <?php
> function add() {
> if(!empty($this->data)) {
> $this->Post->saveAll($this->data);
> }}
>
> ?>
>
> Simple example: I attempt to use saveAll() from the /post/add with an
> associated comment. My view code can be found here:
> <?php
> echo $form->create();
> echo $form->input('Post.title');
> echo $form->input('Post.body');
> echo $form->hidden('Post.user_id', array('value' => 1));
> echo $form->input('Comment.comment');
> echo $form->input('Comment.author');
> echo $form->end('Add Article with Tags and Comment');
> ?>
>
> I receive an error:
> SQL Error: 1364: Field 'post_id' doesn't have a default value [CORE
> \cake\libs\model\datasources\dbo_source.php, line 526
>
> The actual SQL that is run seems to be very skewed (look at number 9):
> 1 DESCRIBE `posts` 5 5 2
> 2 DESCRIBE `users` 3 3 3
> 3 DESCRIBE `comments` 5 5 3
> 4 DESCRIBE `tags` 3 3 2
> 5 DESCRIBE `posts_tags` 3 3 2
> 6 START TRANSACTION 0 0
> 7 INSERT INTO `posts` (`title`, `body`, `user_id`, `created`) VALUES
> ('Test title', 'Test body', 1, '2010-08-12 14:26:40') 1
> 0
> 8 SELECT LAST_INSERT_ID() AS insertID 1 1 0
> 9 INSERT INTO `comments` (`comment`, `author`, `created`) VALUES
> ('2est comment', '2est author', '2010-08-12 14:26:40') 1364: Field
> 'post_id' doesn't have a default value 0
> 10 ROLLBACK
>
> The actual content/value for the fields were "Test comment", "Test
> author"...respectively. The returned ID from the "LAST_INSERT_ID()"
> seems to be getting used in the wrong location.
>
> ....
>
> Alternatively, if I remove the Comments model from the view and work
> with the HABTM association between the Tags and Posts with the
> following view testing this:
> <?php
> echo $form->create();
> echo $form->input('Post.title');
> echo $form->input('Post.body');
> echo $form->hidden('Post.user_id', array('value' => 1));
> echo $form->input('Tag', array('options'=>array(15,20,30),
> 'multiple'=>'checkbox'));
> echo $form->end('Add Article with Tags and Comment');
> ?>
> The save occurs as it should without modification to the post
> controller's add action..
>
> If I'm simply missing something, please let me know. The test project
> was created with cake bake using defaults, so I don't believe my own
> human error in the coding would have caused this (the Post
> controller's add action was reduced in functionality to limit what the
> error could be caused from).
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 [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