⚠ 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

editing of two tables



Mizupo

Mizupo
  • profile picture
  • Member

Posted 25 April 2015 - 13:20 PM

Hi there!
Has become indebted to . Clumsy will excuse me in English .
It is a question about simultaneous editing of two tables today .
 
table:animal
id(prymary_key)
animal_id
owner_id
comment
 
table:owner_detail
owner_id(prymary_key)
owner_name
owner_address
 
As there were two tables,
and the animal on the table to be set in the grocery CRUD.
You can manage screen you can edit it directly is when the animal of the table,
also owner_detail to the owner_id the search criteria
We would like to be able to edit the management screen.
 
... I wonder if I do? How can had been considered,
Since the failure message is displayed after editing, it does not seem to work.
It should be noted that the correction screen will safely operate and comment out the owner_name and owner_address of edit_fields.
 
Also, when you re-edited in the editing screen
There is also the problem that the sky so did not get before the entered value.
 
//Question Summary
While to get the value of the second table is displayed on the field,
Please professor the flow for a successful editing.
Excuse me is difficult to read and then attach but sample code below.
 
//Sample code
function animal_example() {
    $crud = new grocery_CRUD();

    $crud->set_table('animal')
        ->columns('animal_id','owner_id','comment');
    $crud->set_primary_key('id');

    $crud->add_fields('owner_name','owner_address');
    $crud->edit_fields('comment','owner_name','owner_address');
    $crud->change_field_type('animal_id','invisible');
    $crud->change_field_type('owner_id','invisible');

    $crud->callback_before_update(array($this,'animal_detail_send'));
    
    $output = $crud->render();
    $this->load->view('animal_list',$output);
}

function animal_detail_send($post_array){
    $data = array(
        'owner_name' => $post_array['owner_name'],
        'owner_address' => $post_array['owner_address']
    );
    $this->db->where('owner_id',$post_array['owner_id']);
    $this->db->insert('owner_detail',$data);
    
    unset($post_array['owner_name']);
    unset($post_array['owner_address']);
    
    return $post_array;
}
Thank you for reading!

 


Mizupo

Mizupo
  • profile picture
  • Member

Posted 26 April 2015 - 03:17 AM

The display values ​​that are registered in the DB in the field passes the prymary_key by callback_field, was succeeded by returning a value that get the owner_detail in the callback .
 
After the failed updates.
because it operates to comment out the owner_detail -related items, and is probably considered necessary is unset the POST values in callback_before_update. But I do not know because even if unset has failed .

 


Mizupo

Mizupo
  • profile picture
  • Member

Posted 26 April 2015 - 03:57 AM

Has been resolved!
I had made a big mistake of one.
 
//The wrong state
function animal_example() {
    $crud = new grocery_CRUD();

    $crud->set_table('animal')
        ->columns('animal_id','owner_id','comment');
    $crud->set_primary_key('id');

    $crud->add_fields('owner_name','owner_address');
    $crud->edit_fields('comment','owner_name','owner_address');
    $crud->change_field_type('animal_id','invisible');
    $crud->change_field_type('owner_id','invisible');

    //callback_before_update
$crud->callback_before_update(array($this,'coara_before_insert'));
$crud->callback_before_update(array($this,'unset_custum_field'));
    
    $output = $crud->render();
    $this->load->view('animal_list',$output);
}

function coara_before_insert($post_array) {

  $data = array(
  'shop_id' => $post_array('owner_name'),
  'item_id' => $post_array['owner_address'],
  );
  
  $this->db->where('owner_id', $post_array['owner_id');
$this->db->update('owner_detail', $data);

return $post_array;

}

function unset_custum_field($post_array){

unset ($post_array['owner_name']);
unset ($post_array['owner_address']);
return $post_array;
}
 
//Correct state
function animal_example() {
    $crud = new grocery_CRUD();

    $crud->set_table('animal')
        ->columns('animal_id','owner_id','comment');
    $crud->set_primary_key('id');

    $crud->add_fields('owner_name','owner_address');
    $crud->edit_fields('comment','owner_name','owner_address');
    $crud->change_field_type('animal_id','invisible');
    $crud->change_field_type('owner_id','invisible');

    //callback_before_update
$crud->callback_before_update(array($this,'coara_before_insert'));
//This line is not required!
//$crud->callback_before_update(array($this,'unset_custum_field'));
    
    $output = $crud->render();
    $this->load->view('animal_list',$output);
}

function coara_before_insert($post_array) {

  $data = array(
  'shop_id' => $post_array('owner_name'),
  'item_id' => $post_array['owner_address'],
  );
  
  $this->db->where('owner_id', $post_array['owner_id');
$this->db->update('owner_detail', $data);

unset ($post_array['owner_name']);
unset ($post_array['owner_address']);
return $post_array;

return $post_array;

}
 
Thank you!