Home >Backend Development >PHP Tutorial > ThinkPHP 3.0的parseFieldsMap有关问题

ThinkPHP 3.0的parseFieldsMap有关问题

WBOY
WBOYOriginal
2016-06-13 13:41:54909browse

ThinkPHP 3.0的parseFieldsMap问题
数据库里的字段是uid, uname
页面表单的name分别是id和name

Model代码:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
    protected $_map = array(
        'id' => 'uid',
        'name' => 'uname',
    );



Action代码:
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
        $model = M('Demo');
        $list = $model -> select();
        print_r($list);
        $list = $model -> parseFieldsMap($list);
        echo '<br><br>';
        print_r($list);



输出结果都一样
都是

Array ( [0] => Array ( [uid] => 1 [uname] => 测试 ) ) 


问题在这里:
按官方的说法是parseFieldsMap转了以后
最终输出的那个$list应该是

Array ( [0] => Array ( [id] => 1 [name] => 测试 ) ) 

才对啊?
网上到处搜不到
哪位知道还请不吝赐教啊!

------解决方案--------------------
其实 你应该区分 M 和 D 方法的 M的方法是不加载自己定义的模型文件, 而$_map 是定义在你自己创建的模型文件中, 所以应该这样调用: $list = D('Demo') -> parseFieldsMap($list);
------解决方案--------------------
由 parseFieldsMap 方法的定义可知:
他只能作用于一维数组,即
Array ( [uid] => 1 [uname] => 测试 )
变为
Array ( [id] => 1 [name] => 测试 )
PHP code
    /**
     +----------------------
     * 处理字段映射
     +----------------------
     * @access public
     +----------------------
     * @param array $data 当前数据
     * @param integer $type 类型 0 写入 1 读取
     +----------------------
     * @return array
     +----------------------
     */
    public function parseFieldsMap($data,$type=1) {
        // 检查字段映射
        if(!empty($this->_map)) {
            foreach ($this->_map as $key=>$val){
                if($type==1) { // 读取
                    if(isset($data[$val])) {
                        $data[$key] =   $data[$val];
                        unset($data[$val]);
                    }
                }else{
                    if(isset($data[$key])) {
                        $data[$val] =   $data[$key];
                        unset($data[$key]);
                    }
                }
            }
        }
        return $data;
    } <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn