⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Add Action for insert data in other table



deib97
  • profile picture
  • Member

Posted 27 February 2013 - 09:10 AM

I've any problems with my project. I like with this grocery..awesome, great...

but i have query left join in my tables. how i can insert data in other table with this condition??

 

function equipment_branch() 
	{
            $crud = new grocery_CRUD();
            $crud->set_model('my_grocery_model');
            $crud->set_table('aj_alat');
            $crud->columns('id_alat','kd_alat','nm_alat','cabang','tgl_berlaku');
            $crud->unset_columns('id_alat');
			$crud->display_as('kd_alat','Kode')
				 ->display_as('nm_alat','Alat / Kendaraan')
				 ->display_as('tgl_berlaku','Tgl Berlaku');
	   $crud->unset_add();
	   $crud->unset_delete();
	   $crud->unset_edit();
           $crud->set_subject('Mutasi Alat / Kendaraan');
	   $crud->add_action('Lihat Riwayat', base_url().'assets/grocery_crud/themes/flexigrid/css/images/Insert-Element-32','your_controller/your_method');
            $crud->add_action('Perbaharui', base_url().'assets/grocery_crud/themes/flexigrid/css/images/update', 'your_controller/your_method');
            $output = $crud->render();
	    $this->template->load('template','admin/mutasi_alat',$output);	
	}

This my_grocery_model :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class  My_grocery_model extends grocery_CRUD_Model  {

function get_list()
	    {
		 if($this->table_name === null)
		  return false;
	    
		 $select = "{$this->table_name}.*";
	  
  $select .= ", aj_mutasi_alat.*";
 
		 if(!empty($this->relation))
		  foreach($this->relation as $relation)
		  {
		   list($field_name , $related_table , $related_field_title) = $relation;
		   $unique_join_name = $this->_unique_join_name($field_name);
		   $unique_field_name = $this->_unique_field_name($field_name);
		  
	    if(strstr($related_field_title,'{'))
			    $select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\'",$related_field_title))."') as $unique_field_name";
		   else   
			    $select .= ", $unique_join_name.$related_field_title as $unique_field_name";
		  
		   if($this->field_exists($related_field_title))
			    $select .= ", {$this->table_name}.$related_field_title as '{$this->table_name}.$related_field_title'";
		  }
	     
		 $this->db->select($select, false);
	   
  $this->db->join('aj_mutasi_alat','aj_mutasi_alat.kd_alat_mutasi = '. $this->table_name . '.id_alat','left');
 
		 $results = $this->db->get($this->table_name)->result();
	    
		 return $results;
	    }
           }
  
?>

How i can make action button but insert into aj_mutasi_alat? not in aj_alat..?

your_controller/your_method?

Thanks advance..


deib97
  • profile picture
  • Member

Posted 28 February 2013 - 02:37 AM

:mellow: need help, please...


deib97
  • profile picture
  • Member

Posted 28 February 2013 - 07:11 AM

yes... done...

i make function jumper and then to get a new function with set_table aj_alat...

:D


edramirez
  • profile picture
  • Member

Posted 11 November 2013 - 05:14 AM

You mentioned that you need to add records into a table that is different from the table you are presently viewing in the grocerycrud interface.

 

Here's how I have implemented that functionality in my projects:

 

If you need to add records, you need a separate grocerycrud controller to do the job. The reason why a lot of people find difficulty with the grocerycrud is that a lot of stuff is compressed into one controller. The best way that works for me is to create one controller for every crud task that has to be implemented. In this way, you could work out complex operations, including the infamous master-detail relationship.

 

Since you have already created the controller for the file that you are viewing and you have removed the add and edit functionality of that table, you now have to create your second controller (yes, another php file). Since grocerycrud doesn't work on the index function, you need to create the separate function where the crud functions will reside. But don't put any code yet. Make sure you test the add action function and make sure that it goes to the the 2nd controller. If all is well, you are ready to proceed to the next step.

 

Create the grocercrud operation on that 2nd controller. This is easy for you so I won't discuss this here. Remember that grocerycrud will pass one single field (the primary key of the other file). I think you need to will use this information to filter the records of the grocerycrud operation with a where function. You probably will need to insert this information into one of the fields of the 2nd table.

 

Create a view file where the grocerycrud operation will be rendered. At the bottom of the file, you need to put a button that says something like "return to <the name of the main table>". The code in this button is the same as the code that opens the main file. For example, let's say you had a menu selection that said "Departments File" and the code you used to open the controller is "equip.php". The button in the 2nd form should be a redirect command that opens "equip.php". You will notice that grocerycrud will remember the exact page where it left off before the new action button was clicked.

 

If you do it this way, you will create awesome functionality with less code, less bugs, and clean code!

 

And don't forget to click the Like button...  B)

 

Regards,

 

Ed Ramirez