数据库里的字段是uid, uname
页面表单的name分别是id和name
Model代码:
protected $_map = array( 'id' => 'uid', 'name' => 'uname', );
Action代码:
$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);
其实 你应该区分 M 和 D 方法的 M的方法是不加载自己定义的模型文件, 而$_map 是定义在你自己创建的模型文件中, 所以应该这样调用: $list = D('Demo') -> parseFieldsMap($list);
官方文档里写的就是M,而且我试过D,一样没用~
由 parseFieldsMap 方法的定义可知:
他只能作用于一维数组,即
Array ( [uid] => 1 [uname] => 测试 )
变为
Array ( [id] => 1 [name] => 测试 )
/** +---------------------------------------------------------- * 处理字段映射 +---------------------------------------------------------- * @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; }
/** * 处理字段映射 * @access public * @param array $data 当前数据 * @param integer $type 类型 0 写入 1 读取 * @return array */ public function parseFieldsMap($data,$type=1) { // 检查字段映射 if(!empty($this->_map)) { foreach($data as $dkey => $dval){ if(is_array($dval)){ $data[$dkey] = self::parseFieldsMap($dval, $type); }else{ 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; } } /* 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; }

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
