You need to enable JavaScript to run this app. Codeigniter版Perfex CRM实现卖家自定义分组时提示未定义$groups报错
优惠活动
大模型
产品
解决方案
定价
更多

Codeigniter版Perfex CRM实现卖家自定义分组时提示未定义$groups报错

Perfex CRM 卖家自定义分组开发报错排查

开发背景

基于CodeIgniter框架的Perfex CRM卖家模块开发,目标是为卖家主体实现自定义分组的创建、关联、管理功能。目前已完成对应数据库表、关联外键的创建,且数据库连接、表结构配置校验无异常。

触发的报错信息

已触发PHP错误,严重级别:Notice
错误消息:未定义变量:$groups
出错文件名:groups/profile.php
出错行号:70

已实现的代码片段

视图层(view.php)

<?php 
$selected = array();
if(isset($vendor_groups)){
  foreach($vendor_groups as $group){
    array_push($selected,$group['groupid']);
  }
}
if(is_admin() || get_option('staff_members_create_inline_customer_groups') == '1'){
  echo render_select_with_input_groups(
    'groups_in[]',
    $groups,
    array('id','name'),
    'vendor_groups',
    $selected,
    '<a href="#" data-toggle="modal" data-target="#customer_group_modal"><i class="fa fa-plus"></i></a>',
    array('multiple'=>true,'data-actions-box'=>true),
    array(),
    '',
    '',
    false
  );
} else {
  echo render_select(
    'groups_in[]',
    $groups,
    array('id','name'),
    'vendor_groups',
    $selected,
    array('multiple'=>true,'data-actions-box'=>true),
    array(),
    '',
    '',
    false
  );
}
?>

模型层(model.php)

public function get_vendor_groups($id)
{
    $this->db->where('vendor_id', $id);
    return $this->db->get(db_prefix().'vendor_groups')->result_array();
}

/**
 * 获取分组列表
 * @param  string $id
 * @return mixed
 */
public function get_groups($id = '')
{
    if (is_numeric($id)) {
        $this->db->where('id', $id);
        return $this->db->get(db_prefix().'vendorrs_groups')->row();
    }
    $this->db->order_by('name', 'asc');
    return $this->db->get(db_prefix().'vendors_groups')->result_array();
}

/**
 * 编辑分组
 * @param  array $data 表单提交数据
 * @return boolean
 */
public function edit($data)
{
    $this->db->where('id', $data['id']);
    $this->db->update(db_prefix().'vendors_groups', [
        'name' => $data['name'],
    ]);
    if ($this->db->affected_rows() > 0) {
        log_activity('Vendor Group Updated [ID:' . $data['id'] . ']');
        return true;
    }
    return false;
}

/**
 * 删除分组
 * @param  mixed $id 分组ID
 * @return boolean
 */
public function delete($id)
{
    $this->db->where('id', $id);
    $this->db->delete(db_prefix().'vendors_groups');
    if ($this->db->affected_rows() > 0) {
        $this->db->where('groupid', $id);
        $this->db->delete(db_prefix().'vendor_groups');
        hooks()->do_action('vendor_group_deleted', $id);
        log_activity('Vendor Group Deleted [ID:' . $id . ']');
        return true;
    }
    return false;
}

/**
 * 同步卖家关联的分组关系
 * @param  mixed $id        卖家ID
 * @param  mixed $groups_in 提交的分组ID集合
 * @return boolean
 */
public function sync_customer_groups($id, $groups_in)
{
    if ($groups_in == false) {
        unset($groups_in);
    }
    $affectedRows    = 0;
    $vendor_groups = $this->get_vendor_groups($id);
    if (sizeof($vendorr_groups) > 0) {
        foreach ($vendor_groups as $vendor_group) {
            if (isset($groups_in)) {
                if (!in_array($vendor_group['groupid'], $groups_in)) {
                    $this->db->where('vendor_id', $id);
                    $this->db->where('id', $vendorr_group['id']);
                    $this->db->delete(db_prefix().'vendor_groups');
                    if ($this->db->affected_rows() > 0) {
                        $affectedRows++;
                    }
                }
            } else {
                $this->db->where('vendor_id', $id);
                $this->db->delete(db_prefix().'vendor_groups');
                if ($this->db->affected_rows() > 0) {
                    $affectedRows++;
                }
            }
        }
        if (isset($groups_in)) {
            foreach ($groups_in as $group) {
                $this->db->where('vendor_id', $id);
                $this->db->where('groupid', $group);
                $_exists = $this->db->get(db_prefix().'vendor_groups')->row();
                if (!$_exists) {
                    if (empty($group)) {
                        continue;
                    }
                    $this->db->insert(db_prefix().'vendor_groups', [
                        'vendor_id' => $id,
                        'groupid'     => $group,
                    ]);
                    if ($this->db->affected_rows() > 0) {
                        $affectedRows++;
                    }
                }
            }
        }
    } else {
        if (isset($groups_in)) {
            foreach ($groups_in as $group) {
                if (empty($group)) {
                    continue;
                }
                $this->db->insert(db_prefix().'vendor_groups', [
                    'vendor_id' => $id,
                    'groupid'     => $group,
                ]);
                if ($this->db->affected_rows() > 0) {
                    $affectedRows++;
                }
            }
        }
    }

    if ($affectedRows > 0) {
        return true;
    }
    return false;
}

排查与修复方向

  • 核心报错根因:视图层依赖$groups变量渲染下拉选择框,但控制器加载groups/profile.php视图时,未将模型方法获取的全量分组列表作为参数传入视图上下文,导致视图直接读取不存在的变量触发Notice报错。
  • 隐藏的代码缺陷:
    • 模型get_groups方法中,单条分组查询的表名误写为vendorrs_groups(多冗余字母r)
    • 模型sync_customer_groups方法中,已赋值的关联记录变量为$vendor_groups,判断数组长度、读取关联记录ID时误写为$vendorr_groups(多冗余字母r),即使解决了变量传递问题,后续执行分组同步逻辑仍会触发未定义变量的报错
  • 修复操作清单:
    1. 在加载卖家分组视图的对应控制器方法中,调用分组模型的get_groups()方法获取全部分组列表,通过视图加载参数将列表赋值为$groups传递到视图上下文
    2. 全局修正模型中所有表名、变量名的拼写错误,统一匹配项目约定的命名规则
    3. 视图层增加isset($groups)的兜底判断,避免极端场景下的报错溢出

内容的提问来源于stack exchange,提问作者OlegB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.06 08:06:07