0

可以说我有以下内容:

$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;

我怎么做?

4

1 回答 1

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';
    }
}
于 2012-06-11T06:49:44.103 回答