Cakephp errors relating to findAll() and foreach()
I have just completed this tutorial from http://www.sitepoint.com/application-development-cakephp/ as I start my first cakephp project on monday, and I am getting a number of errors and warnings relating to findAll()
and foreach()
.
Here are the errors:
Warning (512): SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'findAll' at line 1 [CORE\cake\libs\model\datasources\dbo_source.php, line 684].
and
Warning (2): Invalid argument supplied for foreach() [APP\views\notes\index.ctp, line 11].
Here is the controller:
function index(){
$this->set('notes', $this->Note->findAll());
}
function view($id){
$this->Note->id = $id;
$this->set('data', $this->Note->read());
}
function add(){
if (!empty($this->data['Note']))
{
if($this->Note->save($this->data['Note'])){
$this->flash('Your note has been updated.','/notes/');
}
}
}
function edit($id = null){
if (empty($this->data['Note'])){
$this->Note->id = $id;
$this->data = $this->No开发者_StackOverflow社区te->read();
}
else{
if($this->Note->save($this->data['Note']))
{
$this->flash('Your note has been updated.','/notes/');
}
}
}
function delete($id){
if ($this->Note->del($id)){
$this->flash('The note with id: '.$id.' has been deleted.', '/notes');
}
}
}
?>
Here is index.ctp:
<h1>My Notes</h1>
<p>
<?php echo $html->link('Add Note', '/notes/add') ?>
</p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<?php foreach ($notes as $note): ?>
<tr>
<td><?php echo $note['Note']['id']; ?></td>
<td>
<?php echo $html->link($note['Note']['title'], "/notes/view/{$note['Note']['id']}")?>
[<?php echo $html->link('Edit', "/notes/edit/{$note['Note']['id']}")?>,
<?php echo $html->link('Delete', "/notes/delete/{$note['Note']['id']}", null, 'Are you sure?')?>]
</td>
<td><?php echo $note['Note']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table>
And here is the database.php file that is in use:
var $default = array(
'driver' => 'mysql',
'connect' => 'mysql_pconnect',
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'memo'
);
All supplied code is taken from the tutorial at http://www.sitepoint.com/application-development-cakephp/ .
QUESTION: Why is this not working? I am using Cakephp 1.3.10.
Any help is appreciated.
The problem is that you're using Cake 1.1 syntax while running 1.3. The article is about 5 years old!
If I remember correctly, 1.1 had a findAll() method but was deprecated in favour of the new syntax. Cake 1.3 actually uses the below syntax
$this->Note->find('all')
Manual : http://book.cakephp.org/view/1021/find-all
The reason you're getting the second error is that foreach
is trying to loop through something that is not an array (your findAll
call probably returns undefined.).
Cake still had the findAllBy<field>
syntax, but this is not what you need right now.
http://book.cakephp.org/view/1025/findAllBy
You might also want to look at the blog application tutorial : http://book.cakephp.org/view/1528/Blog
It covers a lot of the basics and should give you enough of a primer to start with Cae.
精彩评论