CodeIgniter Forums
extend DB table with edit by field - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: extend DB table with edit by field (/showthread.php?tid=406)



extend DB table with edit by field - Lordi - 11-30-2014

hello,

we have same backend make by a freelancer and he end the job.
now we try to understand codeigniter and this doing.  Confused

we want to extend our database model with a hidden fielt edit_by with value from the editing user logined right now in the backend.
DB Field names "edit_by"

any help for us???

CONTROLLER
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Bars extends CI_Controller {

 public function 
__construct()
 {
 
parent::__construct();

 
$this->load->database();
 
$this->load->helper('url');

 
$this->load->library('grocery_CRUD');
 }

 public function 
data_output($output null)
 {
 
$data['page'] = 'barsview';
 
$data['output']=$output;
 
     $this->load->view('template'$data);
 }
 function 
modified_callback($post_array$primary_key) {
 
$post_array['modified']=time();
 return 
$post_array;
 }
 function 
inserted_callback($post_array$primary_key) {
 
$post_array['modified']=time();
 return 
$post_array;
 }
 public function 
index()
 {
 try{
 
$crud = new grocery_CRUD();

 
//$crud->set_theme('datatables');
 
$crud->set_table('bars');
 
$crud->set_subject('Bar');
 
$crud->change_field_type('show_contact_details','true_false');
 
$crud->change_field_type('modified','invisible');
 
$crud->callback_before_update(array($this,'modified_callback'));
 
$crud->callback_before_insert(array($this,'inserted_callback'));
 
$crud->set_rules('popularity','Popularity','integer');
 
$crud->set_rules('popularity''Popularity''required|xss_clean|max_length[5]');
 
$crud->required_fields('name''description''image''address''zipcode''city''latitude''longitude''availability''price''priority''reviews');
 
$crud->unset_texteditor('reviewlink','full_text');
 
$crud->unset_texteditor('moreinfo','full_text');
 
$crud->unset_texteditor('overview','full_text');
 
$crud->unset_texteditor('address','full_text');
 
$crud->set_field_upload('image','assets/restaurantsUploads/');
 
     $crud->callback_column('image',array($this,'_callback_fun'));
 
$crud->columns('name''description''image''address''zipcode''city' 'popularity''edit_by');
 
 
$output $crud->render();

 
$this->data_output($output);

 }catch(
Exception $e){
 
show_error($e->getMessage().' --- '.$e->getTraceAsString());
 }
 }

 


VIEW
PHP Code:
<?php 
foreach($css_files as $file): ?>
 <link type="text/css" rel="stylesheet" href="<?php echo $file?>" />
<?php endforeach; ?>
<?php 
foreach($js_files as $file): ?>
 <script src="<?php echo $file?>"></script>
<?php endforeach; ?>

    <div>
 <?php echo $output?>
    </div> 



RE: extend DB table with edit by field - Chroma - 12-02-2014

(11-30-2014, 08:24 AM)Lordi Wrote: hello,

we have same backend make by a freelancer and he end the job.
now we try to understand codeigniter and this doing.  Confused

we want to extend our database model with a hidden fielt edit_by with value from the editing user logined right now in the backend.
DB Field names "edit_by"

any help for us???

CONTROLLER

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Bars extends CI_Controller {

 public function 
__construct()
 {
 
parent::__construct();

 
$this->load->database();
 
$this->load->helper('url');

 
$this->load->library('grocery_CRUD');
 }

 public function 
data_output($output null)
 {
 
$data['page'] = 'barsview';
 
$data['output']=$output;
 
     $this->load->view('template'$data);
 }
 function 
modified_callback($post_array$primary_key) {
 
$post_array['modified']=time();
 return 
$post_array;
 }
 function 
inserted_callback($post_array$primary_key) {
 
$post_array['modified']=time();
 return 
$post_array;
 }
 public function 
index()
 {
 try{
 
$crud = new grocery_CRUD();

 
//$crud->set_theme('datatables');
 
$crud->set_table('bars');
 
$crud->set_subject('Bar');
 
$crud->change_field_type('show_contact_details','true_false');
 
$crud->change_field_type('modified','invisible');
 
$crud->callback_before_update(array($this,'modified_callback'));
 
$crud->callback_before_insert(array($this,'inserted_callback'));
 
$crud->set_rules('popularity','Popularity','integer');
 
$crud->set_rules('popularity''Popularity''required|xss_clean|max_length[5]');
 
$crud->required_fields('name''description''image''address''zipcode''city''latitude''longitude''availability''price''priority''reviews');
 
$crud->unset_texteditor('reviewlink','full_text');
 
$crud->unset_texteditor('moreinfo','full_text');
 
$crud->unset_texteditor('overview','full_text');
 
$crud->unset_texteditor('address','full_text');
 
$crud->set_field_upload('image','assets/restaurantsUploads/');
 
     $crud->callback_column('image',array($this,'_callback_fun'));
 
$crud->columns('name''description''image''address''zipcode''city' 'popularity''edit_by');
 
 
$output $crud->render();

 
$this->data_output($output);

 }catch(
Exception $e){
 
show_error($e->getMessage().' --- '.$e->getTraceAsString());
 }
 }

 


VIEW

PHP Code:
<?php 
foreach($css_files as $file): ?>
 <link type="text/css" rel="stylesheet" href="<?php echo $file?>" />
<?php endforeach; ?>
<?php 
foreach($js_files as $file): ?>
 <script src="<?php echo $file?>"></script>
<?php endforeach; ?>

    <div>
 <?php echo $output?>
    </div> 

It might be most appropriate to talk to you freelance developer and have them update your system.

Perhaps they could also create some documentation too. That might help into the future.


RE: extend DB table with edit by field - includebeer - 12-02-2014

Your project uses the Grocery CRUD library. You will probably find the answer in the documentation : http://www.grocerycrud.com/


RE: extend DB table with edit by field - Lordi - 12-06-2014

Thank you for your answers.

@Chroma our dev quite the Job. No help no Doku...

@includebeer we will take a look there, Thank you very mutch


RE: extend DB table with edit by field - Lordi - 12-24-2014

i take a look, and found it have to work by
Code:
callback

so i try and try and now it work´s.

i write a new functions
PHP Code:
function modified_edit_by($post_array$primary_key) {
        
$post_array['edit_by'] = $this->session->userdata('name');
        return 
$post_array;
    } 

and add the fields
PHP Code:
$crud->change_field_type('edit_by','invisible'); 
PHP Code:
$crud->callback_before_update(array($this,'modified_edit_by')); 

i need a bit to found out that "name" is the session value for username. But i look in the headercode of my headerview and found it.

Thank you for helping.