2

因此,这是我第一次遇到 SuiteCRM 或任何其他 CRM。我需要在 CRM 未用于我们报价系统的表上查询数据库。因此,我使用模块生成器创建了模块并修改了模块文件,以便它使用正确的表。问题是,当查询运行时,SuiteCRM 仍然会添加其默认的 where 子句,并将 deleted = 0 条件添加到查询中。因此,我尝试使用此 SO 页面上描述的方法. 这不起作用,因为我收到一个错误,即我正在使用未定义的变量 (db),并且我正在调用非对象上的成员函数 fetchByAssoc()。现在,我将代码放在 moduleName.php 文件中。也许那是我的问题。我不知道,因为我从未参与过任何其他 CRM 项目。如果有人能指出我需要做什么才能查询默认 CRM 表以外的其他表,然后在仪表板内显示该查询的结果,我将不胜感激. 我修复了错误。他们是我的错,因为我没有引用该对象。因此,根据要求,这是我的一些代码。这是我的 php 文件。

    <?php

    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    require_once('include/Dashlets/Dashlet.php');

    class FrtwQuotesDashlet extends Dashlet {
    var $height = '200'; // height of the dashlet
    var $quoteData = "";

    /**
     * Constructor
     *
     * @global string current language
     * @param guid $id id for the current dashlet (assigned from Home module)
     * @param array $def options saved for this dashlet
     */
    function FrtwQuotesDashlet($id, $def) {
        $this->loadLanguage('FrtwQuotesDashlet');

        if(!empty($def['height'])) // set a default height if none is set
            $this->height = $def['height'];

        parent::Dashlet($id); // call parent constructor

        $this->isConfigurable = true; // dashlet is configurable
        $this->hasScript = false;  // dashlet has javascript attached to it

        // if no custom title, use default
        if(empty($def['title'])) $this->title =   $this->dashletStrings['LBL_TITLE'];
        else $this->title = $def['title'];
    }

    /**
     * Displays the dashlet
     *
     * @return string html to display dashlet
     */
    function display() {
         $sql = "SELECT QuoteNbr, crmname, ShipToCity FROM quotes.quotehdr LIMIT 10";
       $result = $GLOBALS["db"]->query($sql);
       $quoteData = "<table>";
           while($quotes = $GLOBALS["db"]->fetchByAssoc($result)){
               foreach ($quotes as $quote) {
                   $quoteData .="<tr><td>".$quote[0]."</td><td>".$quote[1]."</td><td>".$quote[2]."</td></tr>";
               }

           }

         $ss = new Sugar_Smarty();
        //assign variables
        //$ss->assign('greeting', $this->dashletStrings['LBL_GREETING']);
        $ss->assign('quoteData', $this->quoteData);
        $ss->assign('height', $this->height);

        $str = $ss->fetch('custom/modules/Home/FrtwQuotesDashlet/FrtwQuotesDashlet.tpl');
        return parent::display().$str; 
    }
}
?>
4

1 回答 1

3

问题在于 foreach 循环。我删除了它,现在它工作正常。在分析代码时,while 循环实际上执行了所需的迭代。因此,通过添加 foreach,发生的事情是代码正在迭代从数据库返回的每一行,然后做一些奇怪的事情——例如,它只会返回每个值应该是的部分字符串。由于我正在查询 3 个字段,因此它还会在每一行上循环 3 次,从而从每一行创建 3 个不同的行。所以,对于任何有类似问题的人来说,这就是工作代码的样子。

<?php

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('include/Dashlets/Dashlet.php');

class FrtwQuotesDashlet extends Dashlet {
    var $height = '200'; // height of the dashlet
    var $quoteData = "";

    /**
     * Constructor
     *
     * @global string current language
     * @param guid $id id for the current dashlet (assigned from Home module)
     * @param array $def options saved for this dashlet
     */
    function FrtwQuotesDashlet($id, $def) {
        $this->loadLanguage('FrtwQuotesDashlet'); 

        if(!empty($def['height'])) 
            $this->height = $def['height'];

        parent::Dashlet($id); 

        $this->isConfigurable = true; 
        $this->hasScript = false;  

        // if no custom title, use default
        if(empty($def['title'])) $this->title = $this->dashletStrings['LBL_TITLE'];
        else $this->title = $def['title'];
    }

    /**
     * Displays the dashlet
     *
     * @return string html to display dashlet
     */
     function display() {
         $sql = "SELECT QuoteNbr, revnbr, crmname, ShipToCity FROM quotes.quotehdr LIMIT 10";
       $result = $GLOBALS["db"]->query($sql);
$this->quoteData = "Need headers here when we determine exact fields....";
           while($quotes = $GLOBALS["db"]->fetchByAssoc($result)){

                   $this->quoteData .="<tr><td width = \"30%\">".$quotes["QuoteNbr"].' '.$quotes['revnbr']."</td><td width = \"30%\">".$quotes["crmname"]."</td><td width = \"30%\">".$quotes["ShipToCity"]."</td></tr>";

           }

         $ss = new Sugar_Smarty();
        //assign variables
       // $ss->assign('greeting', $this->dashletStrings['LBL_GREETING']);
        $ss->assign('greeting', "This is the Greeting....");
        $ss->assign('quoteData', $this->quoteData);
        $ss->assign('height', $this->height);

        $str = $ss->fetch('modules/Home/Dashlets/FrtwQuotesDashlet/FrtwQuotesDashlet.tpl');
        return parent::display().$str; // return parent::display for title and such
    }
}
?>
于 2015-04-23T12:42:56.187 回答