我是非常新尝试一些kohana代码并遇到第一个问题:(
只是为了快点这是我的模特
<?php
class Model_Post extends ORM {
}
这是我的控制器
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Blog extends Controller {
public function action_index()
{
$posts = ORM::factory('Post')->find_all();
$view = View::factory('blog/index')
->bind('posts', $posts);
$this->response->body($view);
}
} // End Blog
这是我的观点
<h2>My list of blog posts</h2>
<? foreach($posts as $post): ?>
<hr />
<h4><?= $post->author ?></h4>
<p><?= $post->body ?></p>
<?endforeach; ?>
现在我收到错误
ErrorException [ Notice ]: Undefined variable: post
APPPATH\views\blog\index.php [ 4 ]
1 <h2>My list of blog posts</h2>
2 <? forech($posts as $post): ?>
3 <hr />
4 <h4><?= $post->author ?></h4>
5 <p><?= $post->body ?></p>
6 <?endforeach; ?>
我正在经历this video
当我将控制器更改为
时,我的结果集不为空
class Controller_Blog extends Controller {
public function action_index()
{
$posts = ORM::factory('Post')->find_all();
// $view = View::factory('blog/index')
// ->bind('posts', $posts);
$this->response->body($posts[0]->body);
}
} // End Blog
它显示了我的第一行
答案 0 :(得分:0)
我试图回忆一下你的设置,一切正常,我的Kohana 3.2的安装。您可以尝试在foreach之前检查是否有帖子?
答案 1 :(得分:0)
我有同样的问题!我尝试像这样更改index.php:
<h2>My list of blog posts</h2>
<?php foreach($posts as $post): ?>
<hr />
<h4><?php echo $post->author ?></h4>
<p><?php echo $post->body ?></p>
<?php endforeach; ?>
它对我有用!