2015-11-06 47 views
1

我試圖從Prestashop連接到外部數據庫(ERP)以從中獲取訂單歷史記錄。執行從Prestashop到外部數據庫的查詢

我已經克隆了歷史控制器並將其命名爲「residui」。

我創建ResiduiController.php包含:

class ResiduiControllerCore extends FrontController { 
public $auth = true; 
public $php_self = 'residui'; 
public $authRedirection = 'residui'; 
public $ssl = true; 

public function setMedia() { 
    parent::setMedia(); 
    $this->addCSS(array(
     _THEME_CSS_DIR_.'residui.css', 
    )); 
    $this->addJS(array(
     _THEME_JS_DIR_.'history.js', 
     _THEME_JS_DIR_.'tools.js' // retro compat themes 1.5 
    )); 
    $this->addJqueryPlugin('footable'); 
    $this->addJqueryPlugin('footable-sort'); 
    $this->addJqueryPlugin('scrollTo'); } 

public function initContent() { 
    parent::initContent(); 

    $residui = Order::getCustomerResidui($this->context->customer->id); 

    $this->context->smarty->assign(array(
     'residui' => $residui 
    )); 

    $this->setTemplate(_PS_THEME_DIR_.'residui.tpl'); } } 

我插入類getCustomerResidui在Order.php:

public static function getCustomerResidui($id_customer, $showHiddenStatus = false, Context $context = null) { 
    if (!$context) 
     $context = Context::getContext(); 
    $evadi = 'S'; 
    $stato = 'GENERATO'; 
    $resi = Db::getFromGazie()->executeS(" 
    SELECT * 
    FROM "._GAZ_PREFIX_."tesbro 
    WHERE id_cli_presta = '".(int)$id_customer."' AND status = '".$stato."' 
    ORDER BY id_tes DESC"); 
    if (!$resi) 
     return array(); 

    foreach ($resi as $key => $val) { 
     $resi2 = Db::getFromGazie()->executeS(" 
      SELECT * 
      FROM "._GAZ_PREFIX_."rigbro 
      WHERE id_doc = '".$val['numdoc']."' AND evadi <> '".$evadi."' 
      ORDER BY codart DESC LIMIT 1"); 

     if ($resi2) 
      $resi[$key] = array_merge($resi[$key], $resi2[0]); } 
    return $resi; } } 

添加在DB.php中的getFromGazie實例和所有連接參數到settings.inc.php中的外部數據庫,如GAZ_PREFIX等

db.php中:

public static function getFromGazie($master = true) { 
    static $id = 0; 

    // This MUST not be declared with the class members because some defines (like _DB_SERVER_) may not exist yet (the constructor can be called directly with params) 
    if (!self::$_servers) 
     self::$_servers = array(
      array('gaz_server' => _GAZ_SERVER_, 'gaz_user' => _GAZ_USER_, 'gaz_password' => _GAZ_PASSWD_, 'gaz_database' => _GAZ_NAME_), /* MySQL Master server */ 
     ); 

    Db::loadSlaveServers(); 

    $total_servers = count(self::$_servers); 
    if ($master || $total_servers == 1) 
     $id_server = 0; 
    else { 
     $id++; 
     $id_server = ($total_servers > 2 && ($id % $total_servers) != 0) ? $id % $total_servers : 1; } 

    if (!isset(self::$instance[$id_server])) { 
     $class = Db::getClass(); 
     self::$instance[$id_server] = new $class(
      self::$_servers[$id_server]['gaz_server'], 
      self::$_servers[$id_server]['gaz_user'], 
      self::$_servers[$id_server]['gaz_password'], 
      self::$_servers[$id_server]['gaz_database']); } 

    return self::$instance[$id_server]; } 

模板,residui.tpl:

<div class="block-center" id="block-history"> 
    <table id="order-list" class="table table-bordered footab"> 
     <thead> 
      <tr> 
       <th class="first_item" data-sort-ignore="true">{l s='Order reference'}</th> 
       <th class="item">{l s='Date'}</th> 

      </tr> 
     </thead> 
     <tbody> 
      {foreach from=$residui item=residuo name=myLoop} 
       <tr class="{if $smarty.foreach.myLoop.first}first_item{elseif $smarty.foreach.myLoop.last}last_item{else}item{/if} {if $smarty.foreach.myLoop.index % 2}alternate_item{/if}"> 
        <td class="history_link bold"> 
         <p class="color-myaccount"> 
          {$residuo['numdoc']} 
         </p> 
        </td> 
        <td class="history_date bold"> 
        {$residuo['datemi']} 
        </td> 

       </tr> 
      {/foreach} 
     </tbody> 
    </table> 
    <div id="block-order-detail" class="unvisible">&nbsp;</div> 

的問題是,我不沒有顯示任何行(我也在PhpMyAdmin中手動測試了查詢)。

我試了幾個小時,但我看不出錯誤(我確信我做了一個或多個)。

你能告訴我什麼嗎?謝謝...

+0

好吧,一步一步你有沒有調試? 'getFromGazie'返回正確的實例? 'var_dump(Order :: getCustomerResidui($ this-> context-> customer-> id));'result?也可以在'config/defines.inc.php'中設置define('_ PS_MODE_DEV_',true);'直接在第 –

+0

處查看錯誤謝謝你的回答。我沒有知道PS_DEV_MODE,現在我試着改變'var_dump(Order :: getCustomerResidui($ this-> context-> customer-> id));'用'$ this-> context-> smarty-> assign(陣列( 'residui'=> DB :: getFromGazie() - >執行( 「SELECT * FROM」 ._GAZ_PREFIX _ 「tesbro WHERE id_cli_presta =「」 $ id_customer「。」 \t \t和狀態= 'GENERATO' \t \t。 ORDER BY id_tes DESC「)));'結果是一個空行。問題是getFromGazie試圖從Prestashop的DB執行查詢......我不明白爲什麼! –

+0

以下是調試結果:**表'vhsxxxx_yyyyyy.gaz_001tesbro'不存在**,但getFromGazie _GAZ_SERVER是** vhsxxxx_zzzzzz.gaz_001tesbro ** –

回答

2

明白了!!!!

一切都歸功於謝爾蓋·P的首先是表明我_PS_MODE_DEV_,我不知道......

的問題是,它總是試圖在同一個數據庫執行查詢。爲了解決這個問題,我在_GAZ_PREFIX_之前加了_GAZ_NAME_,就像這樣:

public static function getCustomerResidui($id_customer, $showHiddenStatus = false, Context $context = null) 
{ 
    if (!$context) 
     $context = Context::getContext(); 
    $evadi = 'S'; 
    $stato = 'GENERATO'; 
    $resi = Db::getFromGazie()->executeS(" 
    SELECT * 
    FROM "._GAZ_NAME_."."._GAZ_PREFIX_."tesbro 
    WHERE id_cli_presta = '".(int)$id_customer."' AND status = '".$stato."' 
    ORDER BY id_tes DESC"); 
    if (!$resi) 
     return array(); 

    foreach ($resi as $key => $val) 
    { 
     $resi2 = Db::getFromGazie()->executeS(" 
      SELECT * 
      FROM "._GAZ_NAME_."."._GAZ_PREFIX_."rigbro 
      WHERE id_doc = '".$val['numdoc']."' AND evadi <> '".$evadi."' 
      ORDER BY codart DESC LIMIT 1"); 

     if ($resi2) 
      $resi[$key] = array_merge($resi[$key], $resi2[0]); 

    } 
    return $resi; 
} 

等voilà,一切正常!