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

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


相关文章

IIS 屏蔽垃圾蜘蛛

<rule name="Block spider">      <match url="(^robots.txt$)&qu...

zblog链式sql不支持 ESCAPE  ,但是sqlite 必须要用ESCAPE ,硬改sql不优雅了

zblog链式sql不支持 ESCAPE ,但是sqlite 必须要用ESCAPE ,硬改sql不优雅了

/zb_system/function/lib/zblogphp.php通过用户名获取用户实例(不区分大小写). public function GetMemberByN...

error_reporting(0); php 关闭 PHP 的错误报告 display_errors memory_limit set_time_limit 内存限制+ 时间限制 +不显示错误

error_reporting(0); 是 PHP 中的一个配置指令,用于关闭 PHP 的错误报告功能。在 PHP 运行过程中,如果没有开启错误报告,那么当出现错误时,PHP 不会显示任何错...

在phpstorm中  $zbp->Load(); $zbp下面会刷红提示 未定义的变量 '$zbp' 但是在 require 'function/c_system_base.php';中已经有$GL

在phpstorm中 $zbp->Load(); $zbp下面会刷红提示 未定义的变量 '$zbp' 但是在 require 'function/c_system_base.php';中已经有$GL

在phpstorm中  $zbp->Load(); $zbp下面会刷红提示 未定义的变量 '$zbp' 但是在 require 'function/c_syst...

linux伪静态 (大括号转义)

linux伪静态 (大括号转义)

大括号转义问题: Nginx配置文件中,大括号{}默认用于配置块(如server{}),直接用在正则表达式中会被误解析。 解决方案:用单引号包裹正则表达式(推荐),或转义大括号(但转义可能不生效,...