在Kohana中列出类别和产品时,分离SQL,PHP和视图

时间:2009-09-30 15:29:17

标签: php mysql kohana kohana-orm

我想通过我的小型网站迁移到Kohana,我正在尝试将SQL,PHP和视图分开,但我对此有一些问题。

我必须去桌子。每个类别都可以有多个产品。

类别表

  • ID
  • 类别

产品表

  • ID
  • CATEGORY_ID
  • 产品

这是我以前的代码(转换为Kohana的查询构建器):

        $categories = DB::select('id', 'category')
            ->from('categories')
            ->as_object()
            ->execute();

    foreach ($categories as $category)
    {
        echo '<b>'.$category->category.'</b><br />';
        $products = DB::select('product')
                ->from('products')
                ->where('category_id', '=', $category->id)
                ->as_object()
                ->execute();

        foreach($products as $product)
        {
            echo $product->product.'<br />';
        }
        echo '<hr />';
    }

我想做同样的事情,只是在视图文件中我不想使用而不是回显变量。

更新 我更喜欢不使用Kohana的ORM模块的解决方案。 顺便说一句,我正在使用Kohana 3.0

更新2:

我已经接受了Lukasz的最后一个解决方案,但需要做一些修改才能完全按照我想要的方式进行修改(请注意,这是针对Kohana 3.0,而Lukasz正在使用旧版本):

SQL代码:

$products = DB::select(array('categories.category', 'cat'), array('products.product', 'prod'))
                ->from('categories')
                ->join('products','RIGHT')
                ->on('products.category_id','category.id')
                ->as_object()
                ->execute();

视图文件中的代码(参见注释):

        // Let's define a new variable which will hold the current category in the foreach loop
        $current_cat = '';
    //We loop through the SQL results
    foreach ($products as $product)
    {
        // We're displaying the category names only if the $current_cat differs from the category that is currently retrieved from the SQL results - this is needed for avoiding the category to be displayed multiple times
        // At the begining of the loop the $current_cat is empty, so it will display the first category name
        if($curren_cat !== $product->cat)
        {
            // We are displaying a separator between the categories and we need to do it here, because if we display it at the end of the loop it will separate multiple products in a category
            // We avoid displaying the separator at the top of the first category by an if statement
            if($current_cat !== '')
            {
                //Separator
                echo '<hr />';
            }
            // We echo out the category
            echo '<b>'.$product->cat.'</b><br />';
            // This is the point where set the $current_cat variable to the category that is currently being displayed, so if there's more than 1 product in the category the category name won't be displayed again
            $current_cat = $product->cat;
        }
        // We echo out the products
        echo $product->prod.'<br />';
    }

我希望这对其他人也有帮助,如果有人有更好的解决方案,请继续分享!

2 个答案:

答案 0 :(得分:0)

这应该有效:

$categories = ORM::factory('category')->find_all();

foreach ($categories as $category)
{
    echo '<b>'.$category->category.'</b><br />';
    $products = ORM::factory('product')->where('category_id',$category->id)->find();

    foreach($products as $product)
    {
        echo $product->product.'<br />';
    }
    echo '<hr />';
}

我假设您为每个表创建了模型?如果没有,请阅读here

更好如果您分隔数据和查看图层。并在模型文件中创建类别和产品之间的关系。然后在控制器中你应该只调用它:

$categories = ORM::factory('category')->find_all();
$view = new View('yourView');
$viem->categories = $categories;

在视图文件中:

foreach ($categories as $category)
{
    echo '<b>'.$category->category.'</b><br />';

    foreach($category->products as $product)
    {
        echo $product->product.'<br />';
    }
    echo '<hr />';
}

您不必为每一行调用第二个查询。 Kohana ORM将为您服务。您所要做的就是创建合适的模型:

class Category_Model extends ORM {
    protected $has_many = array('products');
}

class Product_Model extends ORM {
    protected $belongs_to = array('category');
}

另一种方法:您可以加入表类别和产品

$products = DB::select('categories.category AS cat','products.product AS prod')
                    ->from('categories')
                    ->join('products','products.category_id','category.id')
                    ->as_object()
                    ->execute();

然后:

$current_cat = '';

foreach($products as $product)
{
    if($current_cat != $products->cat)
    {
        echo '<b>'.$products->cat.'</b><br />';
        $current_cat = $products->cat;
    }

    echo $product->prod.'<br />';
}

答案 1 :(得分:0)

快速思考:

使用ORM方法时,我认为你可以使用with()方法实现与join-query相同的功能:

$products = ORM::factory('product')
  ->with('category')
  ->find_all();
$view = new View('yourView');
$viem->products = $products;

我没有足够的时间来处理它,但这是我会尝试的。