2013-07-16 68 views
-3

我可以在joomla 2.5的模型中執行2個單獨的查詢嗎? 如果可能,你如何做到這一點?Joomla 2.5中的一個模型中的多個查詢2.5

這是我的模型:

<?php 
      defined('_JEXEC') or die(); 

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

      class AnagraficheModelServiziassociatiaggiuntivi extends JModelList 
      { 
          public function __construct($config = array()) 
          { 
              if (empty($config['filter_fields'])) 
              { 
                  $config['filter_fields'] = array('id_servizi_aggiuntivi', 'id_persona', 'id_servizio', 'nome_servizio', 'cod_servizio', 'id_tipologia_socio', 'nome_servizio'); 
              } 

              parent::__construct($config); 
          } 

          function getListQuery() 
          {    
              $db = JFactory::getDBO(); 
              $query = $db->getQuery(true); 
              $query->select('id_servizi_aggiuntivi, id_persona , id_servizio , nome_servizio'); 
              $query->from('#__servizi_aggiuntivi as serviziaggiuntivi, #__elenco_servizi'); 
              $query->where('cod_servizio=id_servizio'); 
              $result1 = $db->loadObjectList(); 


              //$db->setQuery($query); 

              $query = $db->getQuery(true); 
              $query->select('id_tipologia_socio, id_servizio as cod_servizio, nome_servizio'); 
              $query->from('#__associazione_servizi as serviziassociati, #__elenco_servizi'); 
              $query->where('cod_servizio=id_servizio'); 
              $result1 = $db->loadObjectList(); 


              return $query; 
          } 

          protected function populateState($ordering = null, $direction = null) 
          { 
              // Load the filter state. 
              $search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search'); 
              $this->setState('filter.search', $search); 

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

這並不工作,但如果我刪除第二個查詢它的偉大工程! 在我看來,問題出在函數getListQuery()中。在這個函數中,我想插入2個查詢而不是1個查詢!可能嗎?

+0

我已經插入我的模型示例 –

+0

您想完成什麼?你試圖覆蓋你甚至不理解的方法。請考慮編輯你的問題。您還應該查看http://docs.joomla.org/Accessing_the_database_using_JDatabase/2.5。 – mattosmat

+0

我想在函數getListQuery()中做2個查詢!這是可能的?!? 我想做2個分開的選擇! –

回答

0

簡短的回答是否定的。 getListQuery應該返回一個查詢。你不能讓它返回兩個查詢。

相反,您可能希望覆蓋getItems函數以在第一個查詢運行後處理項目。您可以使用類似以下的方式調整或添加項目:

public function getItems() { 
    // load items from the parent getItems function, which will call getListQuery to handle your first query. 
    //It only adds items if the first query works. 
    if ($items = parent::getItems()) { 
     $db = JFactory::getDbo(); 

     $query = $db->getQuery(true); 
     $query->select('id_tipologia_socio, id_servizio as cod_servizio, nome_servizio'); 
     $query->from('#__associazione_servizi as serviziassociati, #__elenco_servizi'); 
     $query->where('cod_servizio=id_servizio'); 
     $result1 = $db->setQuery($query)->loadObjectList(); 

     // this is a simple way to merge the objects. You could do other processing (loops, etc) to meld the information together 
     $items = (object) array_merge((array) $items, (array) $restult1); 
    } 
    return $items; 
}