假设我有以下内容:
$categories = ORM::factory('category')->find_all();
foreach ($categories as $category) :
echo $category->category_title;
foreach ($category->posts->find_all() as $post) :
echo $post->post_title;
endforeach;
endforeach;
打印:
Category One
Post One
Category Two
Category Three
Post Two
Category Four
Post Three
差距意味着那里没有帖子。
我想要打印的是:
Category One
Post One
Category Two
No Post
Category Three
Post Two
Category Four
Post Three
基本上我想要:
foreach ($posts->find_all() as $post) :
if post exists
echo $post->post_title;
else
No Post
endforeach;
我该怎么做?
答案 0 :(得分:1)
我假设你正在使用Kohana 3.2和ORM。
查看指南:http://kohanaframework.org/3.2/guide/orm/using
有一个名为检查ORM是否已加载记录
的部分if ($post->loaded())
{
echo $post->post_title;
}
else
{
echo 'No Post';
}
包括更新问题中的类别:
$categories = ORM::factory('category')->find_all();
foreach ($categories as $category)
{
$posts = $category->posts->find_all();
if (count($posts) > 0)
{
echo $category->category_title;
foreach ($posts as $post)
{
echo $post->post_title;
}
}
else
{
echo 'No Posts';
}
}