cakephp:缺少数据库表博客示例

时间:2013-07-16 01:23:09

标签: php cakephp cakephp-2.0

刚开始使用Cake PHP。我正在关注官方网站上提供的这本书。 对于第一个博客示例,我确实创建了控制器,模型和视图,即PostsController.phpPost.phpindex.ctp

LearnCake \应用\控制器\ PostsController.php

<?php

class PostsController extends AppController{
    public $helpers = array('Html', 'Form');

    public function index(){
        $this->set('posts', $this->Post->find('all'));
    }
}
?>

LearnCake \应用\视图\帖子\ index.ctp

<h1>Blog posts</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<!-- Here is where we loop through our $posts array, printing out post info -->
<?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>

模型留空,如示例中所示:

LearnCake \应用\模型\ post.php中     class Post扩展AppModel {     }

我尝试使用此网址http://localhost/LearnCake/posts/index访问该网页,但收到错误消息:

Missing Database Table

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

至于示例,我确实创建了一个名为posts的表。我无法理解为什么它会搜索名为p_o_s_t的表。

我的连接文件是:

<?php

class DATABASE_CONFIG {

    public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'root',
        'password' => 'password',
        'database' => 'cakephp',
        'prefix' => '',
        //'encoding' => 'utf8',
    );

    public $test = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'root',
        'password' => 'password',
        'database' => 'cakephp',
        'prefix' => '',
        //'encoding' => 'utf8',
    );
}

关于我做错了什么的任何建议? 我正在使用netbeans 7.2.1。创建了一个名为LearnCake

的项目

2 个答案:

答案 0 :(得分:2)

像Jimmie Lin所说,数据库表的名称应该是复数。但是,似乎它正在寻找一个匹配名为'POST'(全部大写)的模型的表。

在你的控制器中,放置public $name = 'Posts';或在你的模型public $name = 'Post';中,以确保它不是在寻找p_o_s_ts。

和Jimmie Lin说的一样,请确保表格名称是复数(即“帖子”)。

如果您想使用名为“post”的表格,只需在模型中添加public $useTable = 'post';即可。

答案 1 :(得分:0)

您是否创建了一个名为postspost的表格?数据库表的名称应该是复数,但您根据您的问题使用了单数。