在suitecrm中添加子面板one2many字段

时间:2017-02-20 18:30:38

标签: php sugarcrm suitecrm

我是关于suitecrm的新手,我想在CampaignsProducts

之间添加one2many关系

subpanel形式,据我所知,我应该修改

suitecrm/modules/Campaigns/metadata/subpaneldefs.php

档案,但我不明白我应该把这些关系中的哪一个作为例子。

有人可以让我朝着正确的方向前进吗?

1 个答案:

答案 0 :(得分:1)

您好我正在回答这个问题,你必须在suitcrm做同样的事情。

我检查了广告系列模块中只有一对一的关系。

因此,尝试使用一对多关系创建自定义子面板

<强> 1。创建新的链接类

这应该进入自定义/模块// YourNewLink.php,这个类将充当自定义功能,将在两个记录之间建立链接。

<?php

/**
 * Custom filtered link
 */
class YourNewLink extends Link2
{
    /**
     * DB
     *
     * @var DBManager
     */
    protected $db;

    public function __construct($linkName, $bean, $linkDef = false)
    {
        $this->focus = $bean;
        $this->name = $linkName;
        $this->db = DBManagerFactory::getInstance();
        if (empty($linkDef)) {
            $this->def = $bean->field_defs[$linkName];
        } else {
            $this->def = $linkDef;
        }
    }

    /**
     * Returns false if no relationship was found for this link
     *
     * @return bool
     */
    public function loadedSuccesfully()
    {
        // this link always loads successfully
        return true;
    }

    /**
     * @see Link2::getRelatedModuleName()
     */
    public function getRelatedModuleName()
    {
        return '<Your_Module>';
    }

    /**
     *
     * @see Link2::buildJoinSugarQuery()
     */
    public function buildJoinSugarQuery($sugar_query, $options = array())
    {
        $joinParams = array('joinType' => isset($options['joinType']) ? $options['joinType'] : 'INNER');
        $jta = 'active_other_invites';
        if (!empty($options['joinTableAlias'])) {
            $jta = $joinParams['alias'] = $options['joinTableAlias'];
        }

        $sugar_query->joinRaw($this->getCustomJoin($options), $joinParams);
        return $sugar_query->join[$jta];
    }

    /**
     * Builds main join subpanel
     * @param string $params
     * @return string JOIN clause
     */
    protected function getCustomJoin($params = array())
    {
        $bean_id = $this->db->quoted($this->focus->id);
        $sql = " INNER JOIN(";
        $sql .= "SELECT id FROM accounts WHERE id={$bean_id}"; // This is essentially a select statement that will return a set of ids that you can match with the existing sugar_query
        $sql .= ") accounts_result ON accounts_result.id = sugar_query_table.id";
        return $sql;
    }
}

参数$ sugar_query是一个新的SugarQuery对象,其详细信息请在此处记录。您基本上需要做的是使用您要添加的任何连接/过滤器扩展此查询。这是在我指定的内部联接中完成的。

注意:内部联接可能变得非常复杂,因此如果您想要一个真实的工作示例,请检查模块/ Emails / ArchivedEmailsLink.php以及核心糖团队如何使用它。但我可以确认这适用于自定义连接。

以下是getEmailsJoin,可帮助您了解通过此自定义联接实际生成的内容。

 /**
   * Builds main join for archived emails
   * @param string $params
   * @return string JOIN clause
   */
  protected function getEmailsJoin($params = array())
  {
      $bean_id = $this->db->quoted($this->focus->id);
      if (!empty($params['join_table_alias'])) {
          $table_name = $params['join_table_alias'];
      } else {
          $table_name = 'emails';
      }

      return "INNER JOIN (\n".
              // directly assigned emails
          "select eb.email_id, 'direct' source FROM emails_beans eb where eb.bean_module = '{$this->focus->module_dir}'
              AND eb.bean_id = $bean_id AND eb.deleted=0\n" .
" UNION ".
      // Related by directly by email
          "select DISTINCT eear.email_id, 'relate' source  from emails_email_addr_rel eear INNER JOIN email_addr_bean_rel eabr
          ON eabr.bean_id = $bean_id AND eabr.bean_module = '{$this->focus->module_dir}' AND
          eabr.email_address_id = eear.email_address_id and eabr.deleted=0 where eear.deleted=0\n" .
           ") email_ids ON $table_name.id=email_ids.email_id ";
  }

<强> 2。为链接字段添加新的vardef条目。

对于此示例,我将在联系人模块上创建自定义链接。所以这段代码进入自定义/ Extension / modules / Contacts / Ext / Vardefs / your_field_name.php

<?php
$dictionary["Contact"]["fields"]["your_field_name"] = array(
    'name' => 'active_other_invites',
    'type' => 'link',
    'link_file' => 'custom/modules/<YourModule>/YourNewLink.php',
    'link_class' => 'YourNewLink',
    'source' => 'non-db',
    'vname' => 'LBL_NEW_LINK',
    'module' => '<YourModule>',
    'link_type' => 'many',
    'relationship' => '',
);

第3。将新链接添加为子面板

This goes under custom/Extension/modules/Contacts/Ext/clients/base/layouts/subpanels/your_subpanel_name.php

<?php
$viewdefs['Contacts']['base']['layout']['subpanels']['components'][] = array (
  'layout' => 'subpanel',
  'label' => 'LBL_NEW_LINK',
  'context' =>
  array (
    'link' => 'your_field_name',
  ),
);

<强> 4。添加标签

Under custom/Extension/modules/Contacts/Ext/Language/en_us.new_link.php

<?php
$mod_strings['LBL_ACTIVE_OTHER_INVITES'] = 'Your New Link';

<强> 5。快速修复和重建

这应该有希望让你开始。在调试查询时,请密切关注糖测量。我还发现使用xdebug和SugarQueries compileSql函数非常有价值,可以找出我需要做什么来获得一个有效的INNER JOIN语句。

This is the refrence Link