【发布时间】:2015-04-16 00:14:53
【问题描述】:
我是 Kohana 和整个 ORM 方法的新手,在 ORM 中是否可以接受此代码的任何地方都找不到。 所以,模型中的这个函数返回一个对象
public function get_comments()
{
$comments = ORM::factory('comment')->find_all();
if ($comments)
{
return $comments;
}
else
return array();
}
Controller 将此对象发送给 View
$content = View::factory('/index')
->bind('comments', $comments);
$comments = Model::factory('comment')->get_comments();
$this->response->body($content);
这是一个问题:我可以像这样在视图中使用模型中的对象吗:
<?php foreach($comments as $comment): ?>
<div>
<h5><?php echo HTML::chars($comment->user->login); ?>:</h5>
<?php echo HTML::chars($comment->text); ?>
</div>
<?php endforeach; ?>
在 ORM 中是否可以接受,或者我应该以某种方式从对象创建一个数组并将其发送到 View? 谢谢
【问题讨论】:
-
我不明白你为什么需要这个额外的方法?不能直接在控制器里做
$comments = ORM::factory('comment')->find_all()吗? -
如果您使用
find_all()方法,请使用if (count($comments > 0))而不是if ($comments)。