与findAll和foreach相关的Cakephp错误

问题描述

| 我在星期一开始我的第一个CakePHP项目时,刚刚从http://www.sitepoint.com/application-development-cakePHP/完成了本教程,并且我收到许多与
findAll()
foreach()
有关的错误和警告。 错误如下:
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].
Warning (2): Invalid argument supplied for foreach() [APP\\views\\notes\\index.ctp,line 11].
这是控制器:     
    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->Note->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\');
        }
    }

}
?>
这是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>
这是正在使用的database.PHP文件
var $default = array(
    \'driver\'   => \'MysqL\',\'connect\'  => \'MysqL_pconnect\',\'host\'     => \'localhost\',\'login\'    => \'root\',\'password\' => \'\',\'database\' => \'memo\'
    );
所有提供的代码均来自http://www.sitepoint.com/application-development-cakePHP/上的教程。 题: 为什么这不起作用? 我正在使用CakePHP 1.3.10。 任何帮助表示赞赏。     

解决方法

        问题是您在运行1.3时正在使用Cake 1.1语法。这篇文章大约有5岁了! 如果我没记错的话,1.1有一个findAll()方法,但是不赞成使用新语法。 Cake 1.3实际上使用以下语法
$this->Note->find(\'all\')
手册:http://book.cakephp.org/view/1021/find-all 出现第二个错误的原因是
foreach
试图循环通过非数组对象(您的
findAll
调用可能返回undefined。)。 Cake仍然具有
findAllBy<field>
语法,但这不是您现在所需要的。 http://book.cakephp.org/view/1025/findAllBy 您可能还希望查看博客应用程序教程:http://book.cakephp.org/view/1528/Blog 它涵盖了许多基础知识,并且应该为您提供足够的入门知识,从Cae开始。