1

Trying to learn CakePHP I completed the Blog Tutorial without issue. The browser shows the Cake welcome page, the database connection file is found, and Cake successfully connects to the database. The only thing I did differently is that I used phpMyAdmin to create the posts table in a database named default, but with all of the same content as in the tutorial. Why do I get

Error: Table posts for model Post was not found in datasource default.

in the Blog Tutorial - Adding a layer? This is the Model:

<?php
class Post extends AppModel {
}

This is the Controller:

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

This is the View:

<!-- 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>

And this is the 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)
4

3 回答 3

1

代码似乎还可以。检查database.php以确保数据库名称正确。

于 2013-08-08T00:54:31.210 回答
0

放入模型

<?php
class Post extends AppModel {
    public $name = 'Post';
}
?>
于 2013-11-25T22:17:43.770 回答
0

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

于 2014-03-23T01:22:38.420 回答