_relationship_add logic hook update query不';我不在sugarcrm工作
我已经在cases模块中创建了after_relationship_add logic hook,在这个模块中有一个自定义字段,用于插入一个自定义模块关系数据。钩子被正确地调用了,一切都正常工作。但是当我在钩子逻辑中更新案例记录时,更新查询不起作用。但是如果我加上一个骰子();语句执行update查询后,记录将被更新。逻辑钩子代码如下所示_relationship_add logic hook update query不';我不在sugarcrm工作,sugarcrm,Sugarcrm,我已经在cases模块中创建了after_relationship_add logic hook,在这个模块中有一个自定义字段,用于插入一个自定义模块关系数据。钩子被正确地调用了,一切都正常工作。但是当我在钩子逻辑中更新案例记录时,更新查询不起作用。但是如果我加上一个骰子();语句执行update查询后,记录将被更新。逻辑钩子代码如下所示 public function updateData($bean, $event, $arguments){ $caseid = $bean-&
public function updateData($bean, $event, $arguments){
$caseid = $bean->id;
$dataid = $arguments['related_id'];
$query = "SELECT name FROM data1_data where id = '" .$dataid. "'";
$dataresult = $GLOBALS['db']->query($query , false);
$dataname = "";
while (($row = $GLOBALS['db']->fetchByAssoc($dataresult )) != null) {
$dataname = $row['name'];
}
$newQuery = 'UPDATE cases_cstm SET data_c = "'.$dataname.'" where id_c = "'.$caseid.'" ';
$newResult = $GLOBALS['db']->query($newQuery);
/* here when die() statement is added update query executes properly and
* after removing die(); statement nothing happens.*/
die();
}
有人能帮我解决这个问题吗?在SugarCRM中,您几乎不应该直接与数据库交互。几乎所有您需要做的事情都可以通过SugarBean对象及其扩展来完成。您在这里看到的是一个很好的例子,说明了为什么:您的更新正在命中数据库,但随后立即加载的SugarCRM更新的其余部分正在清除它 我已经用SugarBean和BeanFactory重写了你的函数。请注意所需的代码要少得多,我希望您会发现它可以工作,因为它不会导致额外的更新 我不确定的一点是,您是否真的需要
$bean->save()代码>在末尾。如果我们使用的是before\u save逻辑钩子,则不需要它,但我使用after\u relationship\u add的频率较低,因此这里可能需要它
/**
* @param $bean aCase object
* @param $event string, or specifically 'after_relationship_add'
* @param $arguments array
*/
public function updateData($bean, $event, $arguments){
/*
* Instead of loading the data1_data information from the database
* directly, consider using the SugarBean PHP object, as this is a SugarCRM
* best practice.
*
* Note that we return early if the data1_data object cannot be found or
* if the 'name' value is blank (as that would make the rest of this script
* useless)
*/
$data = BeanFactory::getBean('data1_data',$arguments['related_id']);
if(empty($data->name)) return;
$dataname = $data->name;
/*
* Instead of sending an update query directly to the database, use the
* SugarBean objects, one's loaded already in $bean. Saving objects
* with the SugarBean objects instead of direct SQL will ensure that
* all workflows and logic hooks are executed correctly. Further,
* updating with direct SQL *before* a pending update is sent (such as
* in a logic hook) will likely overwrite whatever update we're making
* in SQL.
*/
$bean->data_c = $dataname;
$bean->save();
}
在SugarCRM中,您实际上不应该直接与数据库交互。几乎所有您需要做的事情都可以通过SugarBean对象及其扩展来完成。您在这里看到的是一个很好的例子,说明了为什么:您的更新正在命中数据库,但随后立即加载的SugarCRM更新的其余部分正在清除它
我已经用SugarBean和BeanFactory重写了你的函数。请注意所需的代码要少得多,我希望您会发现它可以工作,因为它不会导致额外的更新
我不确定的一点是,您是否真的需要$bean->save()代码>在末尾。如果我们使用的是before\u save逻辑钩子,则不需要它,但我使用after\u relationship\u add的频率较低,因此这里可能需要它
/**
* @param $bean aCase object
* @param $event string, or specifically 'after_relationship_add'
* @param $arguments array
*/
public function updateData($bean, $event, $arguments){
/*
* Instead of loading the data1_data information from the database
* directly, consider using the SugarBean PHP object, as this is a SugarCRM
* best practice.
*
* Note that we return early if the data1_data object cannot be found or
* if the 'name' value is blank (as that would make the rest of this script
* useless)
*/
$data = BeanFactory::getBean('data1_data',$arguments['related_id']);
if(empty($data->name)) return;
$dataname = $data->name;
/*
* Instead of sending an update query directly to the database, use the
* SugarBean objects, one's loaded already in $bean. Saving objects
* with the SugarBean objects instead of direct SQL will ensure that
* all workflows and logic hooks are executed correctly. Further,
* updating with direct SQL *before* a pending update is sent (such as
* in a logic hook) will likely overwrite whatever update we're making
* in SQL.
*/
$bean->data_c = $dataname;
$bean->save();
}