⚠ 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

Insert operations on model with set_relation & where



Nicholas
  • profile picture
  • Member

Posted 23 December 2011 - 19:03 PM

Hi, great library, thanks!

I'm having an issue where I want to insert a record when using a relation and a where statement. What I am looking for is to have the primary key of the related field to be inserted into the child table. For example (and yes, the db conventions are terrible, legacy database)


function member_branches(){
$crud = new grocery_CRUD();
$crud->set_model('mssql_Grocery_Model');
$crud->set_theme('datatables');
$crud->set_table('Branch');
$crud->set_subject('Member Firm Branch Office');
$crud->columns('BranchName', 'Address1', 'City', 'State');
$crud->set_relation('MemberID', 'Members', 'Name');
$crud->edit_fields('BranchName', 'Address1', 'Address2', 'City', 'State', 'Zip', 'Phone', 'Fax');
$crud->add_fields('MemberID', 'BranchName', 'Address1', 'Address2', 'City', 'State', 'Zip', 'Phone', 'Fax');

$where = $this->uri->segment(3, 0);
$crud->where('Branch.MemberID',$where, FALSE);
$output = $crud->render();
$this->_members_output($output);
}


So, in this case, I am looking for the MemberID on add_fields to be populated with the $where uri segment. This seems like it would be a standard problem/issue. If I use MemberID in the add_fields then a dropdown is available, which defaults to the first entry, if I do not add it (which is what I want, no client side ability to modify) then it does not get populated.

Ideas?

Happy Holidays!

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 23 December 2011 - 20:40 PM

[quote name='Nicholas' timestamp='1324667018' post='159']
Hi, great library, thanks!

I'm having an issue where I want to insert a record when using a relation and a where statement. What I am looking for is to have the primary key of the related field to be inserted into the child table. For example (and yes, the db conventions are terrible, legacy database)


function member_branches(){
$crud = new grocery_CRUD();
$crud->set_model('mssql_Grocery_Model');
$crud->set_theme('datatables');
$crud->set_table('Branch');
$crud->set_subject('Member Firm Branch Office');
$crud->columns('BranchName', 'Address1', 'City', 'State');
$crud->set_relation('MemberID', 'Members', 'Name');
$crud->edit_fields('BranchName', 'Address1', 'Address2', 'City', 'State', 'Zip', 'Phone', 'Fax');
$crud->add_fields('MemberID', 'BranchName', 'Address1', 'Address2', 'City', 'State', 'Zip', 'Phone', 'Fax');

$where = $this->uri->segment(3, 0);
$crud->where('Branch.MemberID',$where, FALSE);
$output = $crud->render();
$this->_members_output($output);
}


So, in this case, I am looking for the MemberID on add_fields to be populated with the $where uri segment. This seems like it would be a standard problem/issue. If I use MemberID in the add_fields then a dropdown is available, which defaults to the first entry, if I do not add it (which is what I want, no client side ability to modify) then it does not get populated.

Ideas?

Happy Holidays!
[/quote]



function member_branches($where = null){
$crud = new grocery_CRUD();
$crud->set_model('mssql_Grocery_Model');
$crud->set_theme('datatables');
$crud->set_table('Branch');
$crud->set_subject('Member Firm Branch Office');
$crud->columns('BranchName', 'Address1', 'City', 'State');
$crud->edit_fields('BranchName', 'Address1', 'Address2', 'City', 'State', 'Zip', 'Phone', 'Fax');
$crud->add_fields('MemberID', 'BranchName', 'Address1', 'Address2', 'City', 'State', 'Zip', 'Phone', 'Fax');

if($where !== null)
{
$crud->change_field_type('MemberID', 'hidden', $where);
$crud->where('Branch.MemberID',$where, FALSE);
}
$output = $crud->render();
$this->_members_output($output);
}


I think this will solve the problem. You don't need any set_relation as you have the id. Also don't worry about the segment , grocery CRUD is clever enough to understand that the first segment is not an operation segment ;-) . Try it and tell me if it works. Just make sure that you use grocery CRUD v1.1.4 or later to work with hidden inputs

Nicholas
  • profile picture
  • Member

Posted 23 December 2011 - 20:44 PM

Ah, I see, missed that in the documentation.

http://www.grocerycrud.com/crud/function_name/change_field_type

Thanks for the heads up.

Nicholas
  • profile picture
  • Member

Posted 27 December 2011 - 20:38 PM

Just a note for anyone looking into this later, web-johnny's approach listed above works perfectly.