Kohana/ORM - 将关系分配给视图

发布于 2024-11-17 15:43:05 字数 499 浏览 4 评论 0原文

我在控制器的方法中有一个对象:

$post = ORM::factory('post', array('slug' => $slug);  

该对象被发送到视图:

$this->template->content = View::factory('view')->bind('post', $post);

我已经在帖子和评论之间创建了 1-n 关系。到目前为止一切都很好。

主要问题是:我应该如何将帖子评论传递给视图?目前我正在视图中获取它们($post->comments->find_all()),但我不认为这是最好的方法(并且以我的愚见不符合 MVC 标准) )。我也在考虑将它们分配给控制器中的属性($post->comments),但是我收到了有关未定义属性的错误(这对我来说很有意义)。

您建议如何解决该问题?

I have an object in a controller's method:

$post = ORM::factory('post', array('slug' => $slug);  

that is sent to the view:

$this->template->content = View::factory('view')->bind('post', $post);

I've created 1-n relation between post and comments. So good so far.

The main issue is: how should I pass post comments to the view? Currently I'm getting them in the view ($post->comments->find_all()) but I don't feel it's the best method (and not matching MVC standards in my humble opinion). I was also thinking about assigning them to the property in the controller ($post->comments) however I get an error about undefined property (which makes sense to me).

How would you recommend to solve that issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2024-11-24 15:43:05

为什么不抓取控制器中的注释并将其传递到视图中进行演示呢?至少我会这么做。

$post = ORM::factory('post', array('slug' => $slug));
$comments = $post->comments->find_all();
$this->template->content = View::factory('view')->bind('comments', $comments);

关于您的多页评论,我认为您指的是帖子......
这是我通常会做的。

$posts = ORM::factory('post', array('slug' => $slug))->find_all();
$view = new View('view');
foreach ($posts as $post) {
    $view->comments = $post->comments;
    $this->template->content .= $view->render();
}

尽管这可能不是实现这一目标的最资源友好的方式,特别是如果您正在处理许多帖子。因此,将帖子传递到视图,然后在视图内执行 foreach 循环可能是解决此问题的更好方法。

$posts = ORM::factory('post', array('slug' => $slug))->find_all();
$this->template->content = View::factory('view')->bind('posts', $posts);

尽管我也不一定认为从视图运行选择查询是世界上最糟糕的事情。虽然我不是专家...;)

不久前我曾问过这个关于 CodeIgniter 的问题...将数组传递给视图并循环遍历它似乎是首选的响应...

使用 CodeIgniter 加载视图是一种不好的做法一个循环

Why not grab the comments in the controller and pass them to the view for presentation? At least that's how I'd go about this.

$post = ORM::factory('post', array('slug' => $slug));
$comments = $post->comments->find_all();
$this->template->content = View::factory('view')->bind('comments', $comments);

In regards to your multiple pages comment, which I assume you mean posts...
This is what I would usually do.

$posts = ORM::factory('post', array('slug' => $slug))->find_all();
$view = new View('view');
foreach ($posts as $post) {
    $view->comments = $post->comments;
    $this->template->content .= $view->render();
}

Though this may not be the most resource friendly way to accomplish this, especially if you're working with many posts. So passing the posts to the view and then doing the foreach loop inside the view may be a better way to go about this.

$posts = ORM::factory('post', array('slug' => $slug))->find_all();
$this->template->content = View::factory('view')->bind('posts', $posts);

Though I also don't necessarily think running a select query from the view is the worst thing in the world. Though I'm no expert... ;)

I had asked this question a while ago in regards to CodeIgniter... passing an Array to the view and looping through it seemed to be the favored response...

Using CodeIgniter is it bad practice to load a view in a loop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文