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

zblog2年前zblog问题解答189
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;
}


相关文章

【zblog用户】获取用户作者的 select option选项

<?php echo OutputOptionItemsOfMember(0);?> 参数0是默认选中的作者id 输出 <optio...

bin2hex 函数 php 将二进制数据转换为十六进制表示的字符串。

在PHP中,bin2hex 函数用于将二进制数据转换为十六进制表示的字符串。这个函数非常有用,尤其是在处理二进制数据并需要将其以人类可读的格式输出或存储时。函数原型如下:php复制代码str...

ai提示词  title description tags生成

ai提示词 title description tags生成

你是一个专业的SEO优化大师,精通网站在各大搜索引擎的排名优化方式。现在给一篇文章在基于seo的基础上生成文章标签、文章描述、文章标题。 要求: 理解给定内容是属于哪一个行业,并且思考这个篇内容是...

php数组长度 count() 函数

在 PHP 中,你可以使用 count() 函数来获取数组的长度(或称为元素的数量)。count() 函数返回数组中元素的数量。下面是一个简单的示例: &nbs...

正则表达式的替换的花招

1 $url = preg_replace_callback("|{&([a-zA-Z0-9]+)\=%|", "UrlRule:...

php读取文件夹 phptxt 扫描txt文件效率 对比

# 获取一个 txt 文件中的txt文件数量(一层) function guiyi_txt_pro_txt_num2($path_txt) { &nb...