Kohana でカテゴリと製品をリストするときに SQL、PHP、ビューを分離する
-
19-09-2019 - |
質問
私の小規模な Web サイトを Kohana に移行したいと考えており、SQL、PHP、ビューを分離しようとしていますが、これにはいくつか問題があります。
テーブルに行かなければなりません。すべてのカテゴリに複数の商品を含めることができます。
カテゴリ表
- ID
- カテゴリー
製品一覧表
- ID
- カテゴリ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 />';
}
これが他の人にも役立つことを願っています。誰かがより良い解決策を持っている場合は、共有してください。
解決
これは機能するはずです:
$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 />';
}
テーブルごとにモデルを作成したと思いますか?そうでない場合は、読んでください ここ.
より良い データを分離してレイヤーを表示する場合。そして、モデルファイルでカテゴリーと製品間の関係を作成します。次に、コントローラーではこれのみを呼び出す必要があります。
$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 />';
}
すべての行に対して 2 番目のクエリを呼び出す必要はありません。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 />';
}
他のヒント
だけで簡単に考えます:
:ORM-アプローチを使用する場合は、、私はあなたと()メソッドを使用して参加し、クエリと同じことを実現することができると思います
$products = ORM::factory('product')
->with('category')
->find_all();
$view = new View('yourView');
$viem->products = $products;
私はそれで周りの混乱への十分な時間がなかったが、これは私がしようとするだろうものです。