我正在运行查询,然后在控制器中解密它。在解密之后,我将结果放入一个数组并将其发送到视图。问题在于这个解决方案我需要重写我的所有视图来解析发送的数组而不是之前发送的活动记录对象。
有没有办法将解密后的数组转换回一个可以在视图中使用现有活动记录代码的对象?
控制器:
$name = $this->Clients_model->getNameData('*','client_id='.$clid,'');
$data['name'] = $name;
$this->load->view('names/name_view',$data);
查看:
if($name->num_rows()) > 0){
foreach($name->result() as $row){
echo $row->data;
[...]
控制器:
$name = $this->Clients_model->getNameData('*','client_id='.$clid,'');
$nameArray= array();
foreach ($name->result() as $row){
$x = $row;
$keys = array('id','client_id');
$unenc = array();
foreach ($x as $key=>$value){
if(! in_array($key, $keys)){
$unenc[$key]=$this->encrypt->decode($value,$this->e_key);
}else{
$unenc[$key]=$value;
}
}
array_push($nameArray,$unenc);
}
//Creates an object with the data, but doesn't work with CI active record
//foreach ($nameArray as $akey => $aval) {
// $namea -> {$akey} = $aval;
//}
//return $data;
$data['name'] = $nameArray;
$this->load->view('names/name_view',$data);
查看:
if(count($name) > 0){
foreach($name as $key=>$row){
echo $row['data'];
[...]
在第二个(现在)控制器中,有一些注释掉的代码将构成一个对象,但它的行为与活动记录不符。有没有办法获取$nameArray()
数组并将其更改为可与现有视图代码一起使用的对象(例如上面'before:view'中的代码)?
谢谢!
答案 0 :(得分:0)
我在CI论坛上提出了这个问题,并得到了用户mddd的一些精彩帮助:http://codeigniter.com/forums/viewthread/157516/#760320
解决方案最终是在控制器中处理mysql结果对象:
$name = $this->Clients_model->getNameData('*','client_id='.$clid,'');
$dummy = $name->result();
$ignore_keys = array('id', 'client_id');
// watch! we're getting the row as a reference so we're really changing it; not working on a copy
foreach ($name->result_object as &$row) {
foreach (get_object_vars($row) as $key=>$value){
if(!in_array($key, $ignore_keys)){
$row->$key = $this->encrypt->decode($value,$this->e_key);
}
}
}
$data['name']=$name;
$this->load->view('names/name_view',$data);
这允许我只修改控制器中的查询而不是查询和所有视图。