在 suitecrm 中添加子面板 one2many 字段
Add subpanel one2many field in suitecrm
我是 suitecrm 的新手,我想在 Campaigns
和 Products
之间添加一对多关系
以subpanel
形式,据我所知,我应该修改
suitecrm/modules/Campaigns/metadata/subpaneldefs.php
文件,但我不明白应该以哪个关系为例
有人能帮我指出正确的方向吗?
你好,我是根据 sugarcrm 回答这个问题的,你必须在 suitcrm 中做同样的事情。
我检查过在活动模块中只有一对一的关系。
因此,尝试创建具有一对多关系的自定义子面板
1.新建一个linkclass
这应该进入 custom/modules//YourNewLink.php 并且这个 class 将充当将在两条记录之间构建您的 link 的自定义功能。
<?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 对象,其详细信息记录在此处。您基本上需要做的是使用您希望添加的 join/filters 扩展此查询。这是在我指定的内部连接中完成的。
注意:内部连接可能会变得非常复杂,所以如果您想要一个真实的工作示例,请查看 modules/Emails/ArchivedEmailsLink.php 以及核心 sugar 团队如何使用它。不过,我可以确认这确实适用于自定义联接。
这里是 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。为 link 字段添加一个新的 vardef 条目。
对于此示例,我将在联系人模块上创建自定义 link。所以这段代码进入 custom/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。添加新的 link 作为子面板
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.快速修复和重建
这应该可以帮助您入门。在调试查询时留意 sugarlogs。我还发现使用 xdebug 和 SugarQueries compileSql 函数对于弄清楚我需要做什么才能获得有效的 INNER JOIN 语句非常有用。
我是 suitecrm 的新手,我想在 Campaigns
和 Products
以subpanel
形式,据我所知,我应该修改
suitecrm/modules/Campaigns/metadata/subpaneldefs.php
文件,但我不明白应该以哪个关系为例
有人能帮我指出正确的方向吗?
你好,我是根据 sugarcrm 回答这个问题的,你必须在 suitcrm 中做同样的事情。
我检查过在活动模块中只有一对一的关系。
因此,尝试创建具有一对多关系的自定义子面板
1.新建一个linkclass
这应该进入 custom/modules//YourNewLink.php 并且这个 class 将充当将在两条记录之间构建您的 link 的自定义功能。
<?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 对象,其详细信息记录在此处。您基本上需要做的是使用您希望添加的 join/filters 扩展此查询。这是在我指定的内部连接中完成的。
注意:内部连接可能会变得非常复杂,所以如果您想要一个真实的工作示例,请查看 modules/Emails/ArchivedEmailsLink.php 以及核心 sugar 团队如何使用它。不过,我可以确认这确实适用于自定义联接。
这里是 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。为 link 字段添加一个新的 vardef 条目。
对于此示例,我将在联系人模块上创建自定义 link。所以这段代码进入 custom/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。添加新的 link 作为子面板
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.快速修复和重建
这应该可以帮助您入门。在调试查询时留意 sugarlogs。我还发现使用 xdebug 和 SugarQueries compileSql 函数对于弄清楚我需要做什么才能获得有效的 INNER JOIN 语句非常有用。