Part of the EllisLab Network
   
 
encoding / decoding data in models
Posted: 04 July 2009 07:36 AM   [ Ignore ]  
Summer Student
Total Posts:  14
Joined  06-15-2009

hi guys;

i’m just wondering if there’s any smart way to encrypt or decrypt results sets provided by db->get or get_where functions. i try to stick to the model and do this basic data manipulation there (instead of decrypting data in view for example).

so far, i’ve tried a couple of things including:

array_walk($results,'$this->encrypt->decode');

... but none of my attempts are successful.
any hints?

thanks in advance!
a

Profile
 
 
Posted: 04 July 2009 08:04 AM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3782
Joined  04-25-2008

Please show a bit more of your code.

If you get the result array, you’ll probably need to iterate through the array, and change the appropriate values.

$res = $this->db->get('some_table')->result_array();

foreach (
$res as &$row)
{
    $row[
'some_field'] = $this->encrypt->decode($row['some_field']);
}
 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
Posted: 04 July 2009 10:36 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  14
Joined  06-15-2009

hello;

thanks a lot for the reply. i was looking for a way to inject a transformation / function before the final result set is generated, but I believe the way you showed me would work fine.
the only think is I don’t know how to access the elements of the modified result set now. the standard method wouldn’t work I guess. see the message below.

a part of the model:

// get all user's data
    
function getAll()
    
{
        $results
=$this->db->get_where('users',array('id'=>$this->session->userdata('id')))->result_array();
        foreach (
$results as $row)
        
{
            $row[
'email'] = $this->encrypt->decode($row['email']);
        
}
        
return $results;
    
}

the controller:

// a form used to edit user's data
    
function regFormEdit()
    
{
        $data[
'results']=$this->users_model->getAll();
        
$this->load->view('users_edit_view',$data);
    
}

and the view:

<?php $row=$results->row();?>
Email
: <input type="text" name="email" value="<?=$row->email?>"/>

All that gives me:
Fatal error: Call to a member function row() on a non-object in users_edit_view.php on line 9

Thanks again.

Profile
 
 
Posted: 04 July 2009 10:44 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3782
Joined  04-25-2008

In my projects, I have the model pass back the results as an array, I assumed you would have done the same? As far as I know, there’s no way to actually edit the resource that’s returned from the database, so that’s the only way to do it. I think you have two options. You can either have your model pass back the data you want in the format you want, or have it pass back the result object, and format it appropriately from within your controller method before passing it in to the view.

Please note the ampersand in my example. Without it, you won’t be modifying the actual array like you need to.

 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
Posted: 04 July 2009 10:58 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  14
Joined  06-15-2009

I’ll take it further from here. Thanks!

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120200 Total Logged-in Users: 68
Total Topics: 126330 Total Anonymous Users: 4
Total Replies: 664385 Total Guests: 546
Total Posts: 790715    
Members ( View Memberlist )