Joomla 2.5限制每个类别的查询结果

时间:2012-09-20 19:46:05

标签: php mysql joomla2.5

我在Joomla 2.5中使用Module minifrontpage。这里的URL:http://www.bigideaadv.com/wright_flood_OLD/

我要做的是检索最新的4篇文章。 2个来自一个类别,2个来自另一个并且订单需要是第1类文章,然后是第2类文章(我已经设置的只是不能获得每个类别的限制。)

任何帮助将不胜感激。感谢。

我的数据库功能代码如下:

    // Get the dbo
    $database = JFactory::getDbo();

    //Get the config
    $config =& JFactory::getConfig();
    $user =& JFactory::getUser();
    $tzoffset = $config->getValue('config.offset');

    // Get an instance of the generic articles model
    $model = JModel::getInstance('Articles', 'ContentModel', array('ignore_request' => true));

    // Set application parameters in model
    $app = JFactory::getApplication();
    $appParams = $app->getParams();
    $model->setState('params', $appParams);

    // Get Module Parameters
    $number_of_article = (int) $params->get('number_of_article', 5);
    $order          = $params->get( 'order_by', 1);
    $order_type     = $params->get( 'order_type', 'asc');
    $period         = intval( $params->get( 'period', 366 ) );

    echo "ORDER: ".$order."<br />";
    //echo "ORDER BY: ".$order_by;

    //$query2 = "SELECT * FROM ()"

    // Set the filters based on the module params
    $model->setState('list.start', (int) $params->get('number_of_skip', 0));
    $model->setState('list.limit', (int) $params->get('number_of_article', 5));
    $model->setState('filter.published', 1);

    // Access filter
    $access = !JComponentHelper::getParams('com_content')->get('show_noauth');
    $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
    $model->setState('filter.access', $access);

    // Category filter
    $model->setState('filter.category_id', $params->get('catid', array()));

    // User filter
    $userId = JFactory::getUser()->get('id');
    switch ($params->get('user_id'))
    {
        case 'by_me':
            $model->setState('filter.author_id', (int) $userId);
            break;
        case 'not_me':
            $model->setState('filter.author_id', $userId);
            $model->setState('filter.author_id.include', false);
            break;

        case '0':
            break;

        default:
            $model->setState('filter.author_id', (int) $params->get('user_id'));
            break;
    }

    // Filter by language
    $model->setState('filter.language',$app->getLanguageFilter());

    //  Featured switch
    switch ($params->get('show_featured'))
    {
        case '0':
            $model->setState('filter.featured', 'hide');
            break;
        case '1':
        default:
            $model->setState('filter.featured', 'show');
            break;
        case '2':
            $model->setState('filter.featured', 'only');
            break;
    }

    // Set ordering
    $order_map = array(
        '0' => 'a.created',
        '1' => 'a.hits',
        '2' => 'a.ordering',
        '3' => 'RAND()',
    );

    $ordering = "catid, ";
    $ordering .= JArrayHelper::getValue($order_map, $params->get('order_by'), 'a.publish_up');
    //$ordering = JArrayHelper::getValue($order_map, $params->get('order_by'), 'a.publish_up');

    echo $ordering;     

    //help ordering system for DESC or ASC
    switch ($params->get( 'order_type' ))
    {
        case 1:
            $dir = 'DESC';
            break;
        case 0:
        default:
            $dir = 'ASC';
            break;
    }

    $model->setState('list.ordering', $ordering);
    $model->setState('list.direction', $dir);

    //Filter and Set Period (days)
    $model->setState('filter.date_filtering', 'relative');
    $model->setState('filter.relative_date', $period);

    $items = $model->getItems();

1 个答案:

答案 0 :(得分:0)

这也是我需要的。限制每个类别的文章。想知道是否有一个简单的过滤器我们可以设置来实现这一目标。 想避免模特破解。

谢谢!

修改 由于缺乏共鸣,我以一种非常丑陋的方式尝试了这一点,它解决了我的部分问题。为简化起见,我决定每个类别1篇文章。 首先,我更改了models \ category.php,并在第224行附近的模型中添加了一个名为filter.subcategories_article_limit的模型(检查了博客布局)     $model->setState('filter.subcategories_article_limit', 1);

基本上这是为过滤器设置标志

然后在models \ articles.php中,我在322行左右添加了一个where子句     $subcategories_article_limit = (int) $this->getState('filter.subcategories_article_limit', 0); if(!empty($subcategories_article_limit)){ $whereClause = '(a.id IN (SELECT a1.id FROM wiz3j_content AS a1 WHERE a1.catid=a.catid AND a1.publish_up=(SELECT MAX(a2.publish_up) FROM wiz3j_content AS a2 WHERE a1.catid=a2.catid) ))'; $query->where($whereClause);
}

我知道这是一个核心黑客,它不会处理很多事情,如用户权限,文章状态等。我相信这可以改进。

但我希望有些大师可以得到更好的解决方案。