我正试图从Prestashop连接到外部数据库(ERP),以从中获取订单历史记录。
我已经克隆了历史控制器,并将其命名为"residi"。
我创建了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'); } }
我在Order.php中插入了类getCustomerResidui:
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]; }
模板,residi.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"> </div>
问题是我没有显示任何行(我还在PhpMyAdmin中手动测试了查询)。
我试了几个小时,但我看不出错误(我确信我犯了一个或多个)。
你能告诉我一些事情吗?谢谢
明白了!!!!
首先感谢Sergii 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;
}
瞧,一切都很好!