it-swarm-id.com

Grup JModelList berdasarkan Kategori

Saya memiliki Komponen dengan tabel berikut:

+------------------+------------------+------+-----+---------------------+----------------+
| Field            | Type             | Null | Key | Default             | Extra          |
+------------------+------------------+------+-----+---------------------+----------------+
| id               | int(11) unsigned | NO   | PRI | NULL                | auto_increment |
| asset_id         | int(10) unsigned | NO   |     | 0                   |                |
| title            | varchar(255)     | NO   |     | NULL                |                |
| file             | varchar(255)     | NO   |     | NULL                |                |
| category         | int(11)          | NO   |     | NULL                |                |
| ordering         | int(11)          | NO   |     | NULL                |                |
| state            | tinyint(1)       | NO   |     | NULL                |                |
| checked_out      | int(11)          | NO   |     | NULL                |                |
| checked_out_time | datetime         | NO   |     | 0000-00-00 00:00:00 |                |
| created_by       | int(11)          | NO   |     | NULL                |                |
+------------------+------------------+------+-----+---------------------+----------------+

Saya menggunakan JCategories. Ini adalah File model saya saat ini:

<?php

/**
 * @version     1.0.0
 * @package     com_media_centre
 * @copyright   Copyright (C) 2014. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Dawid van der Hoven <[email protected]> - http://www.jamfactory.co.za
 */
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

/**
 * Methods supporting a list of Media_centre records.
 */
class MediaModelFiles extends JModelList {

    /**
     * Constructor.
     *
     * @param    array    An optional associative array of configuration settings.
     * @see        JController
     * @since    1.6
     */
    public function __construct($config = array()) {
        parent::__construct($config);
    }

    /**
     * Method to auto-populate the model state.
     *
     * Note. Calling getState in this method will result in recursion.
     *
     * @since   1.6
     */
    protected function populateState($ordering = null, $direction = null) {

        // Initialise variables.
        $app = JFactory::getApplication();

        // List state information
        $limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'));
        $this->setState('list.limit', $limit);

        $limitstart = JFactory::getApplication()->input->getInt('limitstart', 0);
        $this->setState('list.start', $limitstart);


        if(empty($ordering)) {
            $ordering = 'a.ordering';
        }

        // List state information.
        parent::populateState($ordering, $direction);
    }

    /**
     * Build an SQL query to load the list data.
     *
     * @return  JDatabaseQuery
     * @since   1.6
     */
    protected function getListQuery() {
        // Create a new query object.
        $db = $this->getDbo();
        $query = $db->getQuery(true);

        // Select the required fields from the table.
        $query->select(
                $this->getState(
                        'list.select', 'a.*'
                )
        );

        $query->from('`#__media_files` AS a');


    // Join over the users for the checked out user.
    $query->select('uc.name AS editor');
    $query->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');

        // Join over the category 'category'
        $query->select('category.title AS category_title');
        $query->join('LEFT', '#__categories AS category ON category.id = a.category');
        // Join over the created by field 'created_by'
        $query->select('created_by.name AS created_by');
        $query->join('LEFT', '#__users AS created_by ON created_by.id = a.created_by');


        // Filter by search in title
        $search = $this->getState('filter.search');
        if (!empty($search)) {
            if (stripos($search, 'id:') === 0) {
                $query->where('a.id = ' . (int) substr($search, 3));
            } else {
                $search = $db->Quote('%' . $db->escape($search, true) . '%');
                $query->where('( a.title LIKE '.$search.' )');
            }
        }



        //Filtering category
        $filter_category = $this->state->get("filter.category");
        if ($filter_category) {
            $query->where("a.category = '".$filter_category."'");
        }

        return $query;
    }

    public function getItems() {
        return parent::getItems();
    }

}

Oke Sekarang pertanyaan sebenarnya:

Bagaimana saya bisa mengembalikan data dalam format seperti:

Category 1 (and the alternative layout param)
 - Item in Category 1
 - Item in Category 1
 - Item in Category 1

Category 2 (and the alternative layout param)
 - Item in Category 2
 - Item in Category 2
 - Item in Category 2

Saya minta maaf itu tidak dalam ruang lingkup atau jika terlalu banyak pertanyaan, Jika hanya memberikan komentar dan menghapus sakit, jika tidak tolong beri tahu saya jika ada informasi lain yang diperlukan.

2

Saya tidak tahu apakah ini mungkin menggunakan SQL, tetapi Anda bisa mendapatkan hasil seperti itu menggunakan PHP:

$byCategory = array();

foreach ($items as $item)
{
    if (empty($byCategory[$item->category_title]))
    {
            $byCategory[$item->category_title] = array();
    }
    $byCategory[$item->category_title][] = $item;
}

$byCategory array akan mengurutkan data seperti yang telah Anda jelaskan:

Array
(
    [Category 1] => Array
        (
            [0] => stdClass Object
                (
                    Item in Category 1
                )

            [1] => stdClass Object
                (
                    Item in Category 1
                )

            [2] => stdClass Object
                (
                    Item in Category 1
                )
        )

    [Category 2] => Array
        (
            [0] => stdClass Object
                (
                    Item in Category 2
                )

            [1] => stdClass Object
                (
                    Item in Category 2
                )

            [2] => stdClass Object
                (
                    Item in Category 2
                )
        )
)
2
Dmitry Rekun

Jika Anda menggunakan PDO, Anda dapat mengambil menggunakan metode pasangan kunci, saya tidak terlalu berpengalaman dengan PDO + Joomla, tetapi di PDO murni Anda dapat melakukan sesuatu seperti ini:

$q = $db->query("SELECT `category_title` AS name, `value` AS value FROM `wherever`;");
$r  = $q->fetchAll(PDO::FETCH_KEY_PAIR);

Kemudian akan mengelompokkannya sebagai nilai key =>, Anda juga dapat menulis sesuatu seperti "$ sth-> fetchAll (PDO :: FETCH_ASSOC | PDO :: FETCH_GROUP, 2)" untuk mengambilnya sebagai grup.

http://php.net/manual/en/pdo.constants.php Cari "FETCH_" dan Anda dapat melihat serangkaian metode pengambilan.

Ini mungkin tidak menjawab pertanyaan Anda sepenuhnya, tetapi mudah-mudahan membangunkan beberapa ide tentang bagaimana menyelesaikan masalah Anda.

1
Purple Penguin