zblog发(更新)文章写表 关于ID 骚操作 更新ID 修改ID

zblog1年前zblog问题解答48
zb_system/function/lib/base.php 的 Save() 函数


接口 Filter_Plugin_Post_Save 和 Filter_Plugin_PostArticle_Core 只能改 ID外的内容。

Save()  如果新增ID 为空,直接忽略ID 写入,写入后sql返回文章ID
如果不为空 则直接根据ID进行更新。

所以,要修改 文章ID,两种方式,

1 文章发布前 挂 Filter_Plugin_Post_Save 和 Filter_Plugin_PostArticle_Core 把计算出来的 ID(如值 222) 写入zbp_post表(其他值默认值空值), + $post->ID = 222;
  这样 在save时候,直接更新了。
  
  
2 文章发布后 改ID  挂 Filter_Plugin_PostArticle_Succeed 接口,获取ID, 把 计算出来的id 覆盖掉获取的ID(建议第一中,事少)。



可以用  $article->ID == 0 判断是否新文章。

public function Save()
{
    # 打印当前php 的 根目录到文件名的全路径 和 当前行数
    #print_r(debug_backtrace());
    #die();

    #print_r($this->data);
    #die("s");
    global $bloghost;
    #print_r($this->idname);
    # die();

    if (array_key_exists('Meta', $this->data)) {
        $this->data['Meta'] = $this->Metas->Serialize();
    }
    #print_r($this->datainfo);
    #die(" end");
    $keys = array();
    foreach ($this->datainfo as $key => $value) {
        if (!is_array($value) || count($value) < 4) {
            continue;
        }

        $keys[] = $value[0];
    }
    $keyvalue = array_fill_keys($keys, '');
    $keyvalue_orig = array();

    foreach ($this->datainfo as $key => $value) {
        if (!is_array($value) || count($value) < 4) {
            continue;
        }
        if (!array_key_exists($key, $this->data)) {
            //如果unset(某个$key)就不再插入或修改该数据
            unset($keyvalue[$value[0]]);
            continue;
        }

        if ($value[1] == 'boolean') {
            $keyvalue[$value[0]] = (int)$this->data[$key];
            $keyvalue_orig[$value[0]] = (int)$this->original[$key];
        } elseif ($value[1] == 'integer') {
            $keyvalue[$value[0]] = (int)$this->data[$key];
            $keyvalue_orig[$value[0]] = (int)$this->original[$key];
        } elseif ($value[1] == 'float') {
            $keyvalue[$value[0]] = (float)$this->data[$key];
            $keyvalue_orig[$value[0]] = (float)$this->original[$key];
        } elseif ($value[1] == 'double') {
            $keyvalue[$value[0]] = (float)$this->data[$key];
            $keyvalue_orig[$value[0]] = (float)$this->original[$key];
        } elseif ($value[1] == 'string' || $value[1] == 'char') {
            if ($key == 'Meta' || $bloghost == '/') {
                $keyvalue[$value[0]] = $this->data[$key];
                $keyvalue_orig[$value[0]] = $this->original[$key];
            } else {
                $keyvalue[$value[0]] = ($this->isreplacehost) ? $this->ReplaceHost2Tag($this->data[$key]) : $this->data[$key];
                $keyvalue_orig[$value[0]] = ($this->isreplacehost) ? $this->ReplaceHost2Tag($this->original[$key]) : $this->original[$key];
            }
        } else {
            $keyvalue[$value[0]] = $this->data[$key];
            $keyvalue_orig[$value[0]] = $this->original[$key];
        }
    }
    array_shift($keyvalue);
    array_shift($keyvalue_orig);

    $id_name = $this->idname;
    $id_field = $this->datainfo[$id_name][0];


    if (empty($this->$id_name)) {
        if (count($keyvalue) == 0) {
            return true;
        }


        $sql = $this->db->sql->Insert($this->table, $keyvalue);


        $this->$id_name = $this->db->Insert($sql);

    } else {
        foreach ($keyvalue as $key => $value) {
            if (array_key_exists($key, $keyvalue_orig)) {
                if ($value === $keyvalue_orig[$key]) {
                    unset($keyvalue[$key]);
                }
            }
        }
        if (count($keyvalue) == 0) {
            return true;
        }
        $sql = $this->db->sql->Update($this->table, $keyvalue, array(array('=', $id_field, $this->$id_name)));
        $r = $this->db->Update($sql);

        $this->original = $this->data;
        return $r;
    }

    $this->original = $this->data;
    return true;
}


相关文章

linux修改密码 ssh端口查看

查看ssh端口 cat /etc/ssh/sshd_config | grep Port 修改密码 passwd...

php curl 典型案例代码 curl例子 curl_init() curl_setopt

      $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);...

Nginx没有防火墙 Nginx设置加上 垃圾蜘蛛过滤

 if ($http_user_agent ~* "censys|bytedance|GPTBot|openai|Amazonbot|dotbot|c...

php读取一行 随机读一行file 函数 随机读文件夹的*.txt内容

# 读取文件随机一行 $lines = @file('peizhi/keyword/1.txt');  $keyword_nr ...

ucs-4 这是啥编码 mb_convert_encoding($content, 'ucs-4', 'utf-8') 啥意思

UCS-4 是一种Unicode字符编码形式,全称为 Universal Character Set - 4 bytes。它是Unicode标准的一种实现,其中每个字符都使用4个字...

php array_pop 函数 从数组的最后一个元素弹出值,并返回这个值。

php array_pop 函数array_pop 函数是 PHP 中的一个内置函数,它的作用是从数组的最后一个元素弹出值,并返回这个值。同时,该数组的长度会减少 1,因为被弹出的元素已经从...