Separare SQL, PHP e la vista quando si elencano le categorie e prodotti in Kohana
-
19-09-2019 - |
Domanda
Voglio migrare verso Kohana con i miei siti web di piccole e sto cercando di separare il codice SQL, PHP e la vista, ma ho alcuni problemi con questo.
Devo tabelle. Ogni categoria può avere più prodotti.
tabella Categorie
- id
- categoria
Tavolo Prodotti
- id
- category_id
- prodotto
Questo è stato il mio codice precedente (convertito in generatore di query di 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 />';
}
voglio fare lo stesso, proprio nel file di visualizzazione Non voglio usare altro che l'eco le variabili.
Aggiornamento: Io preferirei una soluzione senza utilizzare il modulo ORM di Kohana. Btw Sto usando Kohana 3.0
Aggiornamento 2:
Ho accettato l'ultima soluzione di Lukasz, ma sono necessarie alcune modifiche per fare esattamente quello che volevo (si noti che questo è per Kohana 3.0, mentre Lukasz stava lavorando con una versione precedente):
di codice 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();
Codice nel file di visualizzazione (vedere i commenti per visualizzare la spiegazione):
// 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 />';
}
Spero che questo è stato utile anche ad altri, se qualcuno ha una soluzione migliore andare avanti, condividerlo!
Soluzione
Questo dovrebbe funzionare:
$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 />';
}
Suppongo che si è creato modelli per ogni tabella? In caso contrario, leggere qui .
Meglio se separate layer di dati e visualizzare. E creare relazioni tra categorie e prodotti in file di modello. Poi nel controller che si dovrebbe chiamare solo questo:
$categories = ORM::factory('category')->find_all();
$view = new View('yourView');
$viem->categories = $categories;
E nel file di visualizzazione:
foreach ($categories as $category)
{
echo '<b>'.$category->category.'</b><br />';
foreach($category->products as $product)
{
echo $product->product.'<br />';
}
echo '<hr />';
}
Non dovrete chiamare seconda query per ogni riga. Kohana ORM lo farà per voi. Tutto quello che dovete fare è di creare modelli appropriati:
class Category_Model extends ORM {
protected $has_many = array('products');
}
class Product_Model extends ORM {
protected $belongs_to = array('category');
}
Un altro approccio: è possibile unire tabelle categorie e prodotti
$products = DB::select('categories.category AS cat','products.product AS prod')
->from('categories')
->join('products','products.category_id','category.id')
->as_object()
->execute();
E poi:
$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 />';
}
Altri suggerimenti
Solo un pensiero rapido:
Quando si utilizza l'ORM-approccio, credo che si potrebbe ottenere la stessa cosa di un join-query utilizzando la con () Metodo:
$products = ORM::factory('product')
->with('category')
->find_all();
$view = new View('yourView');
$viem->products = $products;
Non ho avuto abbastanza tempo per pasticciare con essa, ma questo è quello che vorrei provare.