dongzhouhao4316 2019-03-07 07:07
浏览 136

关系的getBeans()返回空数组

I am creating a custom ACL class that would check whether the relationship exists between the records and if so load all the related records to that particular bean. I have looked at the sugar documentation which says to use load_relationship($relationshipName) for checking if relationship exists and getBeans() to load all the related records (as an array of objects). I have implemented this into my class but for some reason whichever module and relationship I use it always returns an empty array.

The data I use for checking has 3 parts:

  1. The Module accessing the data
  2. The relationship name with the target module (not module name)
  3. The ID of the record accessing the data

The link here at sugar community shows a similar problem that I'm having, but the answer to this does not so solve my problem

Here is my custom ACL:

namespace Sugarcrm\Sugarcrm\custom\clients\base;

class CustomACL
{
    const ACL_NONE = 0;
    const ACL_READ_ONLY = 1;
    const ACL_READ_WRITE = 2;

    public static function checkRelated($module, $linkedRelationshipName, $id)
    {
        $bean = \BeanFactory::getBean($module);
        if ($bean->load_relationship($linkedRelationshipName)) {
            return self::checkRecordRelated($bean, $id,$linkedRelationshipName);
        } else {
            return false;
        }
    }

    /**
     * Checks if record is related
     * @param $bean
     * @param $id
     * @param $linkedModule
     * @return bool
     */
    protected static function checkRecordRelated($bean, $id, $linkedModule)
    {
        $bean->retrieve_by_string_fields(array(
            "id" => $id
        ));
        if ($bean->load_relationship($linkedModule)) {
            $relatedRecords = $bean->$linkedModule->getBeans();
            return $relatedRecords;
        } else {
            return false;
        }
    }

}

This class should be working for any module, even if it is custom or non custom. I have tried using my custom module and even the default modules (leads, accounts etc) but none of them returns anything except an empty array.

  • 写回答

1条回答 默认 最新

  • douya2007 2019-03-07 07:40
    关注

    I suspect the problem is that you are reusing the previously empty bean, for which you already loaded the same link using load_relationship() before.
    On the second load_relationship() call, Sugar probably returns the cached result from the first call (as the link is already internally flagged as having been loaded), therefore returning the same empty array again.

    Therefore instead of using

    $bean->retrieve_by_string_fields(array(
            "id" => $id
        ));
    

    I'd suggest creating a new bean e.g. using

    if (empty($id)) {
        return false;
    }
    $bean = BeanFactory::retrieveBean($module, $id);
    if (!$bean) {
        return false;
    }
    

    (which should actually not be too slow, as the bean is probably cached already)

    Notes:

    • Your variable names are somewhat confusing. $linkedRelationshipName and $linkedModule should contain neither the relationship name nor the module name, but the name of the link-type field.

    EDIT:

    To reiterate: The documentation may be misleading there, but load_relationship() does not expect the relationship name as parameter. What it expects is the link name!.

    from data/SugarBean.php:

    /**
     * Loads the request relationship. This method should be called before performing any operations on the related data.
     *
     * This method searches the vardef array for the requested attribute's definition. If the attribute is of the type
     * link then it creates a similary named variable and loads the relationship definition.
     *
     * @param string $link_name link/attribute name.
     *
     *@return nothing.
     */
    function load_relationship($link_name)
    

    So make sure to check the VarDefs of each module for the correct link name.

    E.g.

    • relationship name: accounts_contacts
    • link field for this relationship in account: contacts,
      so you should be calling $accountBean->load_relationship('contacts')
    • link field for this relationship in contact: accounts,
      so you should be calling $contactBean->load_relationship('accounts')

    Note: link names are basically arbitrary across different modules, don't rely on them being lowercase singular/plural of the linked module. In some cases (and for custom relationships) they will not be.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法