فصل SQL، PHP وعرض عند إدراج الفئات والمنتجات في Kohana

StackOverflow https://stackoverflow.com/questions/1498893

  •  19-09-2019
  •  | 
  •  

سؤال

أريد أن أخرج إلى Kohana مع مواقع الويب الصغيرة وأحاول فصل SQL و PHP والعرض، لكنني بعض المشاكل مع هذا واحد.

لا بد لي من الجداول. يمكن أن تحتوي كل فئة على منتجات متعددة.

الفئات الجدول

  • هوية شخصية
  • الفئة

جدول المنتجات

  • هوية شخصية
  • معرف الفئة
  • المنتج

كان هذا الرمز السابق (تحويله إلى باني استعلام كوهانا):

        $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 />';
    }

أريد أن أفعل نفس الشيء، فقط في ملف الرؤية لا أريد استخدامه بخلاف صدى المتغيرات.

تحديث:أفضل حلا دون استخدام وحدة orm كوهانا. راجع للشغل أنا أستخدم Kohana 3.0

تحديث 2:

لقد قبلت الحل الأخير لوكاسز، ولكن هناك حاجة إلى عدد قليل من التعديلات للقيام بالضبط ما أردت (لاحظ أن هذا هو في كوهانا 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();

رمز في ملف العرض (انظر التعليقات للحصول على Explanation):

        // 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 />';
}

لن تضطر إلى استدعاء الاستعلام الثاني لكل صف. كوهانا أورم سوف تفعل ذلك من أجلك. كل ما عليك فعله هو إنشاء النماذج المناسبة:

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;

لم يكن لدي ما يكفي من الوقت للبحث عنها، لكن هذا ما سأحاوله.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top