执行从Prestashop到外部数据库的查询

时间:2015-11-06 08:50:24

标签: php mysql smarty prestashop prestashop-1.6

我尝试从 Prestashop连接到外部数据库(ERP)以从中获取订单历史记录。

我克隆了历史记录控制器并将其命名为" residui "。

我创建了 ResiduiController.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;   }   }

我在Order.php中插入了类getCustomerResidui:

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]; }

将DB.php中的getFromGazie实例和所有连接参数添加到settings.inc.php 中的外部数据库,例如GAZ_PREFIX等

db.php中:

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

模板 residui.tpl:

vector<T> func(){
    vector<T> result;
    ...
    return result; 
}

问题是我没有显示任何行(我也在PhpMyAdmin中手动测试了查询)。

我试了几个小时,但我看不出错误(我确定我做了一个或多个)。

你能说点什么吗?感谢...

1 个答案:

答案 0 :(得分:2)

得到了!!!!

首先感谢Sergii P建议我_PS_MODE_DEV_,我不知道......

问题是它总是试图在同一个DB上执行查询。为了解决这个问题,我在_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;
}

Etvoilà,一切正常!