Books belongsTo Authors and Authors hasMany Books.
Books table have various field, most intersting:
id (PK)
author_id (FK for authors table)
title
and so on.
This is the view when I want to add a book:
<?php
echo $form->create('Book');
echo $form->input('Book.author_id', array( 'type' => 'select',
'multiple'=>false, 'label' => 'Choice the right author:' ));
echo "<br />";
echo $form->input('Book.title', array('maxLength' => 150, 'type'
=> 'text', 'label' => 'Book title:' ));
// ...
echo $form->end('Save Album');
?>
and add function in books_controller.php
<?php
function add()
{
$this->set('authors', $this->Author->find('list'));
if (!empty($this->data))
{
debug($this->data);
if ($this->Book->save($this->data))
{
$this->Session->setFlash('Your book has been saved.');
$this->redirect(array('action'=>'index'));
}
else
{
$this->Session->setFlash("An error has occured, check
all form fields.");
}
}
}
?>
debug($this->data) return this outupt:
Array
(
[_Token] => Array
(
[key] => 3b4bdf...c0a4373f
[fields] => 391875c...7D
)
[Book] => Array
(
[author_id] => Array
(
[0] => 13
)
[title] => my Title
)
)
I have two questions:
1. How can I add 13 correctly in author_id field??? I have error: SQL
Error: 1054: Unknown column 'Array' in 'field list' Why author id
array has this nested form?
2. What is the _Token array? How can I use it?
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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
No comments:
Post a Comment