php版本兼容的坑,php8兼容性

发布时间:2023-01-06

本文目录一览:

  1. 如何避免ThinkPHP的两个坑
  2. THINKPHP项目的一些坑
  3. ecshop 与php5.4兼容性上会出现什么问题

如何避免ThinkPHP的两个坑

public function createProblems(){
    $problems = I('json.');
    if (empty($problems)) {
        $this->error($problems, 'json格式不符合规范');
    }
    foreach ($problems as $problem) {
        $data = D('Problem')->create($problem, self::OP_INSERT);
        if (!$data) {
            $this->error($problem, D('Problem')->getError());
        }
        $temp[] = $data;
    }
    $ret = D('Problem', 'Service')->addProblems($temp);
    if ($ret === false) {
        $this->error(null, '导入失败');
    }
    $this->success(null);
}

接口完成的功能是批量创建问题,参数为json数组,addProblem()方法中就是一个addAll操作。

第一个坑-自动填充

使用自动填充可能会覆盖表单提交项目。其目的是为了防止表单非法提交字段。使用Model类的create方法创建数据对象的时候会自动进行表单数据处理。 官方文档明确说了自动填充会覆盖表单,所以即使你post过来的参数中给出了具体数值,使用create方法之后也可能会被覆盖。千万注意!!!

第二个坑-addAll方法

addAll方法中不能出现null值,否则其他数据会自动向前移动,导致添加失败。举例:

[
    {
        "appId": 1,
        "serviceId": 2,
        "createUser": null,
        "status": 1,
        "priority": 2
    }
]

其中,createUser字段为null,插入时的SQL语句会变成:

insert into va_problem (appId, serviceId, createUser, status, priority) values (1, 2, 1, 2)

null值没了,导致插入失败,这应该是ThinkPHP3.2.3的一个bug。这篇博客有相关的讨论

引入坑的过程

problemModel里面有对createUser做自动填充。 通过API创建问题时,首先自动填充会覆盖,所以表单中的createUser值失效,这是第一个坑。然后,由于自动填充createUser调用的是get_username()函数,通过API调用时,session(username)取不到值,所以create之后字段变成"createUser": null,引入了第二个坑。

THINKPHP项目的一些坑

当我们设置input type="text" style="display:block"时,其宽度并不等于父元素的100%,而是需要我们手动设置width=100%; {:C("IP_URL")}会调用Common下Conf里config.php的自定义变量。 结论IS_POST好用,IS_GET不好用,可以用count($_GET)来替代。 PHP中不区分NULLnull。 两个表连接:->join("B on A.xx=B.xx") ✅ 三个表连接:

->join("inner join B on A.xx=B.xx join C on A.xx=C.xx") ✅
->join("inner join B on A.xx=B.xx inner join C on A.xx=C.xx") ✅

MySQL-PHP-视图层 MySQL中整形字段 → PHP接收为字符串 → 模板中又为number。

ecshop 与php5.4兼容性上会出现什么问题

是会有问题的,PHP5.4里面会抛弃掉一些5.3的函数,所以ecshop的2.7.3版本在PHP5.4下面用的话会有一些不兼容。不过现在ecshop出3.0版本了,直接兼容到PHP5.6,模板堂官方对外免费使用的模板也都兼容5.6了,您可以去下载安装用下。