在数据源默认值中找不到模型Post的表帖子?

时间:2013-08-06 21:27:48

标签: cakephp phpmyadmin

尝试学习CakePHP我完成了Blog Tutorial没有问题。 浏览器显示Cake欢迎页面,找到数据库连接文件,Cake成功连接到数据库。 我唯一不同的是,我使用phpMyAdmin在名为posts的数据库中创建default,表,但内容与教程中的内容完全相同。 我为什么要

  

错误:在数据源默认值中找不到模型Post的表帖子。

Blog Tutorial - Adding a layer中的

? 这是模型:

<?php
class Post extends AppModel {
}

这是控制器:

<?php
class PostsController extends AppController {
    public $helpers = array('Html', 'Form');
    public function index() {
        $this->set('posts', $this->Post->find('all'));
    }
}

这是视图:

<!-- File: /app/View/Posts/index.ctp -->    
<h1>Blog posts</h1>
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Created</th>
    </tr>
    <?php foreach ($posts as $post): ?>
    <tr>
        <td><?php echo $post['Post']['id']; ?></td>
        <td>
            <?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
        </td>
        <td><?php echo $post['Post']['created']; ?></td>
    </tr>
    <?php endforeach; ?>
    <?php unset($post); ?>
</table>

这就是Stack Trace:

CORE\Cake\Model\Model.php line 3243 → Model->setSource(string)
CORE\Cake\Model\Model.php line 2696 → Model->getDataSource()
APP\Controller\PostsController.php line 5 → Model->find(string)
[internal function] → PostsController->index()
CORE\Cake\Controller\Controller.php line 486 → ReflectionMethod->invokeArgs(PostsController, array)
CORE\Cake\Routing\Dispatcher.php line 187 → Controller->invokeAction(CakeRequest)
CORE\Cake\Routing\Dispatcher.php line 162 → Dispatcher->_invoke(PostsController, CakeRequest, CakeResponse)
APP\webroot\index.php line 111 → Dispatcher->dispatch(CakeRequest, CakeResponse)

3 个答案:

答案 0 :(得分:1)

代码似乎没问题。检查database.php以确保数据库名称正确无误。

答案 1 :(得分:0)

加入模型

<?php
class Post extends AppModel {
    public $name = 'Post';
}
?>

答案 2 :(得分:0)

确保您的数据库用户对表有选择权限。我有同样的错误,这是我的问题。

相关问题