Heim  >  Artikel  >  Backend-Entwicklung  >  thinkphp问题咨询

thinkphp问题咨询

WBOY
WBOYOriginal
2016-09-08 08:43:501215Durchsuche

字段过滤
如果写入了数据表中不存在的字段数据,则会被直接过滤,例如:
$data['name'] = 'thinkphp';
$data['email'] = 'thinkphp@gmail.com';
$data['test'] = 'test';
$User = M('User');
$User->data($data)->add();
复制代码
其中test字段是不存在的,所以写入数据的时候会自动过滤掉。
在3.2.2版本以上,如果开启调试模式的话,则会抛出异常,提示:非法数据对象:[test=>test]

上面是手册里面的话,开启了debug,可是就是没报错。之前别的项目会报的。自己重新载了官网最新的源码,就是不报错。特别是update的时候,条件字段写错了,自己过滤了,把所有数据都update了,很蛋疼

回复内容:

字段过滤
如果写入了数据表中不存在的字段数据,则会被直接过滤,例如:
$data['name'] = 'thinkphp';
$data['email'] = 'thinkphp@gmail.com';
$data['test'] = 'test';
$User = M('User');
$User->data($data)->add();
复制代码
其中test字段是不存在的,所以写入数据的时候会自动过滤掉。
在3.2.2版本以上,如果开启调试模式的话,则会抛出异常,提示:非法数据对象:[test=>test]

上面是手册里面的话,开启了debug,可是就是没报错。之前别的项目会报的。自己重新载了官网最新的源码,就是不报错。特别是update的时候,条件字段写错了,自己过滤了,把所有数据都update了,很蛋疼

翻了下TP3.2.3的源码,只有在$this->options['strict']不为空的时候才会报错,至于在哪里操作这个options['strict'],没找到。

感觉是bug吧。
你可以去试着找下。

thinkphp_3.2.3_full/ThinkPHP/Library/Think/Model.class.php
第267-277行:

<code>foreach ($data as $key=>$val){
    if(!in_array($key,$fields,true)){
        // 如果字段不存在
        if(!empty($this->options['strict'])){
            // 只有在这个$this->options['strict']不为空的时候才抛异常。
            E(L('_DATA_TYPE_INVALID_').':['.$key.'=>'.$val.']');
        }
        // unset掉这个没用的字段
        unset($data[$key]);
    }elseif(is_scalar($val)) {
        $this->_parseType($data,$key);
    }
}</code>

条件的话,你可以另外再加一个where()的连贯操作

ThinkPHPLibraryThinkModel.class.php里面有两处跑错误的,一处是条件错误,一个是插入操作字段错误!
thinkphp问题咨询
具体什么错误无解,对比了3.2.2,改成一样的条件,问题不大。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn