web-dev-qa-db-ja.com

JModelListカテゴリー別グループ

次の表のコンポーネントがあります。

+------------------+------------------+------+-----+---------------------+----------------+
| 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                |                |
+------------------+------------------+------+-----+---------------------+----------------+

JCategoriesを使用しています。これが私の現在のモデルファイルです:

<?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();
    }

}

さて、実際の質問:

次のような形式でデータを返すにはどうすればよいですか。

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

範囲外であること、または質問が多すぎることをお詫び申し上げます。コメントを削除して問題があった場合は、その他の情報が必要な場合はお知らせください。

2

SQLを使用してこれが可能かどうかはわかりませんが、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
(
    [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

PDOを使用している場合は、キーペアメソッドを使用してフェッチできます。PDO+ Joomlaの経験はあまりありませんが、純粋なPDOでは次のようなことができます。

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

次に、それらをキー=>値としてグループ化します。「$ sth-> fetchAll(PDO :: FETCH_ASSOC | PDO :: FETCH_GROUP、2)」のように記述して、グループとしてフェッチすることもできます。

http://php.net/manual/en/pdo.constants.php 「FETCH_」を検索すると、フェッチメソッドのセットが表示されます。

それはあなたの質問に完全には答えないかもしれませんが、うまくいけばあなたの問題を解決する方法についてのいくつかのアイデアを目覚めさせます。

1
Purple Penguin