Typecho——Argument 1 passed to TypechoRouterget() must be of the type string

前言

开发EasyBe主题的置顶功能时候,测试的时候出现了Typecho\Router::get()报错,根据对应的报错信息查看了下源码后解决了该问题;

内容

查询SQL

// 获取全部文章
function getAllPosts($page, $limit) {
    $db = Typecho_Db::get();
    $sql = $db->select('c.cid', 'c.title', 'c.created', 'c.text', 'c.password', 'c.commentsNum', 'c.views', 'c.digg', 'f.str_value as sticky')
            ->from('table.contents as c')
            ->join('table.fields as f', 'f.cid = c.cid', Typecho_Db::LEFT_JOIN)
            ->where('c.status = ? and c.type = ?', 'publish', 'post')
            ->order('sticky', Typecho_Db::SORT_DESC)
            ->order('c.created', Typecho_Db::SORT_DESC)
            ->page($page, $limit);
    return  $db->fetchAll($sql);
}

报错信息

根据报错,这里我们只需要关注#0的提示即可;
Argument 1 passed to Typecho\Router::get() must be of the type string, null given, called in /data/wwwroot/dev.wangyangyang.vip/build/var/Widget/Base/Contents.php on line 536
TypeError: Argument 1 passed to Typecho\Router::get() must be of the type string, null given, called in /data/wwwroot/dev.wangyangyang.vip/build/var/Widget/Base/Contents.php on line 536 and defined in /data/wwwroot/dev.wangyangyang.vip/build/var/Typecho/Router.php:170
Stack trace:
#0 /data/wwwroot/dev.wangyangyang.vip/build/var/Widget/Base/Contents.php(536): Typecho\Router::get(NULL)
#1 /data/wwwroot/dev.wangyangyang.vip/build/var/Widget/Base/Contents.php(478): Widget\Base\Contents->filter(Array)
#2 /data/wwwroot/dev.wangyangyang.vip/build/usr/themes/easybe/index.php(19): Widget\Base\Contents->push(Array)
#3 /data/wwwroot/dev.wangyangyang.vip/build/var/Widget/Archive.php(1415): require_once('/data/wwwroot/d...')
#4 /data/wwwroot/dev.wangyangyang.vip/build/var/Typecho/Router.php(99): Widget\Archive->render()
#5 /data/wwwroot/dev.wangyangyang.vip/build/index.php(23): Typecho\Router::dispatch()
#6 {main}

排查经过

根据报错进入到对应的文件中,使用vi进行编辑;


?> 到这里其实问题已经就解决了,因为我上面查询的SQL中,并没有将字段type筛选出来,所以这里才导致了传NULL的情况;

解决

把~c c.type c~加入查询字段即可;

// 获取全部文章
function getAllPosts($page, $limit) {
    $db = Typecho_Db::get();
    $sql = $db->select('c.cid', 'c.title', 'c.created', 'c.text', 'c.password', 'c.commentsNum', 'c.views', 'c.type', 'c.digg', 'f.str_value as sticky')
            ->from('table.contents as c')
            ->join('table.fields as f', 'f.cid = c.cid', Typecho_Db::LEFT_JOIN)
            ->where('c.status = ? and c.type = ?', 'publish', 'post')
            ->order('sticky', Typecho_Db::SORT_DESC)
            ->order('c.created', Typecho_Db::SORT_DESC)
            ->page($page, $limit);
    return  $db->fetchAll($sql);
}

posted @ 2023-02-24 21:29:23 王洋 阅读(825) 评论(0)
发表评论
昵称
邮箱
网址