weixin_41494138 2025-05-06 15:32 采纳率: 14.3%
浏览 83

thinkphp5.1连接达梦数据库后进行增删改

thinkphp5.1连接达梦数据库后如何使用model新增数据?
使用php7.3 nginx DM管理工具 V8 (Build 2024.09.18)
使用save方法返回true但是数据库中没有新增数据
输出的$device->getLastSql();如下图

img

应该如何修改达梦数据库驱动部分代码?
think/db/builder/Dm.php

<?php

namespace think\db\builder;

use think\db\Builder;
use think\db\Query;

/**
 * Dm数据库驱动
 */
class Dm extends Builder
{
    protected $selectSql = 'SELECT * FROM (SELECT thinkphp.*, rownum AS numrow FROM (SELECT  %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%) thinkphp ) %LIMIT%%COMMENT%';

    /**
     * limit分析
     * @access protected
     * @param  Query $query 查询对象
     * @param  mixed $limit
     * @return string
     */
    protected function parseLimit($query, $limit)
    {
        $limitStr = '';

        if (!empty($limit)) {
            $limit = explode(',', $limit);

            if (count($limit) > 1) {
                $limitStr = "(numrow>" . $limit[0] . ") AND (numrow<=" . ($limit[0] + $limit[1]) . ")";
            } else {
                $limitStr = "(numrow>0 AND numrow<=" . $limit[0] . ")";
            }

        }

        return $limitStr ? ' WHERE ' . $limitStr : '';
    }

    /**
     * 设置锁机制
     * @access protected
     * @param  Query      $query 查询对象
     * @param  bool|false $lock
     * @return string
     */
    protected function parseLock($query, $lock = false)
    {
        if (!$lock) {
            return '';
        }

        return ' FOR UPDATE NOWAIT ';
    }

    /**
     * 字段和表名处理
     * @access public
     * @param  Query  $query  查询对象
     * @param  string $key
     * @param  string $strict
     * @return string
     */
    public function parseKey($query, $key, $strict = false)
    {
        $key = trim($key);

        if (strpos($key, '->') && false === strpos($key, '(')) {
            // JSON字段支持
            list($field, $name) = explode($key, '->');
            $key                = $field . '."' . $name . '"';
        }

        return $key;
    }

    /**
     * 随机排序
     * @access protected
     * @param  Query $query 查询对象
     * @return string
     */
    protected function parseRand($query)
    {
        return 'DBMS_RANDOM.value';
    }
}

think/db/connector/Dm.php

<?php

namespace think\db\connector;

use PDO;
use think\db\BaseQuery;
use think\db\Connection;

/**
 * Dm数据库驱动
 */
class Dm extends Connection
{
    /**
     * 解析pdo连接的dsn信息
     * @access protected
     * @param array $config 连接信息
     * @return string
     */
    protected function parseDsn($config)
    {
        $dsn = 'dm:';

        if (!empty($config['hostname'])) {
            //  Oracle Instant Client
            $dsn .=  $config['hostname'] . ($config['hostport'] ? ':' . $config['hostport'] : '') . '/';
        }


        if (!empty($config['charset'])) {
            $dsn .= ';charset=' . $config['charset'];
        }

        return $dsn;
    }

    /**
     * 取得数据表的字段信息
     * @access public
     * @param string $tableName
     * @return array
     */
    public function getFields($tableName)
    {
        list($tableName) = explode(' ', $tableName);
        $sql             = "select a.column_name,data_type,DECODE (nullable, 'Y', 0, 1) notnull,data_default, DECODE (A .column_name,b.column_name,1,0) pk from all_tab_columns a,(select column_name from all_constraints c, all_cons_columns col where c.constraint_name = col.constraint_name and c.constraint_type = 'P' and c.table_name = '" . strtoupper($tableName) . "' ) b where table_name = '" . strtoupper($tableName) . "' and a.column_name = b.column_name (+)";

        $pdo    = $this->query($sql, [], false, true);
        $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
        $info   = [];

        if ($result) {
            foreach ($result as $key => $val) {
                $val                       = array_change_key_case($val);
                $info[$val['column_name']] = [
                    'name'    => $val['column_name'],
                    'type'    => $val['data_type'],
                    'notnull' => $val['notnull'],
                    'default' => $val['data_default'],
                    'primary' => $val['pk'],
                    'autoinc' => $val['pk'],
                ];
            }
        }

        return $this->fieldCase($info);
    }

    /**
     * 取得数据库的表信息(暂时实现取得用户表信息)
     * @access   public
     * @param string $dbName
     * @return array
     */
    public function getTables($dbName = '')
    {
        $sql    = 'select table_name from all_tables';
        $pdo    = $this->query($sql, [], false, true);
        $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
        $info   = [];

        foreach ($result as $key => $val) {
            $info[$key] = current($val);
        }

        return $info;
    }

    /**
     * SQL性能分析
     * @access protected
     * @param string $sql
     * @return array
     */
    protected function getExplain($sql)
    {
        return [];
    }

    protected function supportSavepoint()
    {
        return true;
    }
}


  • 写回答

3条回答 默认 最新

  • 紫雾凌寒 新星创作者: 人工智能技术领域 2025-05-06 15:36
    关注

    让 “紫雾凌寒” 来帮你回答,本回答结合 DeepSeek

    如果还有问题,请评论或点击头像私信,如果有帮助到您, 请关注、采纳、点赞

    1. 关键点分析:
      - 首先是ThinkPHP 5.1与达梦数据库的连接配置。
      - 然后是使用Model的save方法新增数据时,返回true但数据未成功插入数据库的问题排查。
    2. 解决方案:
      - 连接达梦数据库配置
      config/database.php中配置达梦数据库连接信息,示例如下:
      php 'type' => 'dm', 'hostname' => '你的数据库主机地址', 'database' => '你的数据库名', 'username' => '用户名', 'password' => '密码', 'hostport' => '端口号', 'params' => [], 'charset' => 'UTF8', 'prefix' => '表前缀',
      - 新增数据时排查问题

      • 确保Model定义正确,例如:
        ```php
        namespace app\index\model;

      use think\Model;

      class YourModel extends Model
      {
      protected $table = ‘your_table_name’;
      }
      - 检查save方法传入的数据格式是否正确,例如:php
      $model = new YourModel();
      $data = [
      ‘field1’ => ‘value1’,
      ‘field2’ => ‘value2’
      ];
      $result = $model->save($data);
      if ($result) {
      echo ‘新增成功’;
      } else {
      echo ‘新增失败’;
      // 可以进一步打印错误信息排查,如 $model->getError()
      }
      ```
      - 查看数据库日志,达梦数据库有自己的日志记录方式,通过日志可以查看是否有插入操作被拒绝等原因。例如,检查是否有字段类型不匹配等错误。
      3. 多种解决方案的优缺点:
      - 配置检查
      - 优点:这是基础步骤,如果连接配置错误,根本无法进行后续操作,所以首先排查配置是很必要的。
      - 缺点:相对比较容易排查,只要按照文档正确配置一般不会出错。
      - Model定义检查
      - 优点:如果Model定义错误,save方法可能无法正确识别表结构等信息,所以检查Model定义可以确保数据操作的准确性。
      - 缺点:通常逻辑比较清晰,只要熟悉Model的定义规则,很容易发现问题。
      - 数据格式检查
      - 优点:数据格式不正确是常见的导致数据插入失败的原因,通过详细检查传入的数据格式,可以快速定位问题。
      - 缺点:需要对数据库表结构和传入数据有清晰的了解,否则可能会遗漏一些细节。
      - 数据库日志检查
      - 优点:可以直接看到数据库实际执行的操作和错误信息,对于定位问题非常有帮助,尤其是在前面几种方法都无法明确问题时。
      - 缺点:不同数据库的日志查看方式可能不同,需要熟悉达梦数据库的日志查看方法,可能会花费一些时间在这上面。
      4. 总结:
      - 首先要确保ThinkPHP 5.1与达梦数据库的连接配置正确。然后仔细检查Model定义、传入save方法的数据格式是否正确。如果以上方法都不能解决问题,查看数据库日志是进一步排查的有效手段。通过逐步排查这些方面,应该能够找到数据新增失败的原因并解决问题。

    希望以上解答对您有所帮助。如果您有任何疑问,欢迎在评论区提出。

    评论

报告相同问题?

问题事件

  • 修改了问题 5月6日
  • 创建了问题 5月6日