• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DB Query Question

#1
[eluser]loganbest[/eluser]
Hi,

I'm fairly new to OOPHP and CI so I had a quick question that could be an easy answer for some people.

I have a form right now with about 100 checkboxes all with the same name so they will submit as an array of numbers.

I also have a table named tag_restaurant_rel which is a relational table that is setup as InnoDB and both `restaurant_id` and `tag_id` are Foreign Keys relating to the corresponding columns in two other tables.

The form will be submitted with 1 restaurant_id and several tag_id's.

In the tag_restaurant_rel table there is a record for each tag_id in the array all with the same restaurant_id from the form.

What I need to happen is for the form to be submitted and depending on the array of checkbox values the tag_restaurant_rel table will need to delete the records that are not in the array and create a new record for each tag_id in the array.

I'm mostly curious in the most efficient way to do this OOP and CI style.

#2
[eluser]loganbest[/eluser]
Really? nothing?

#3
[eluser]danmontgomery[/eluser]
Any particular reason to try and figure out the difference between the checked tags and what's in the db?

Code:
$this->db->where('restaurant_id', $restaurant_id)->delete('tag_restaurant_rel');

$insert = array();
foreach($tags as $tag_id) {
    $insert[] = array('tag_id' => $tag_id, 'restaurant_id' => $restaurant_id);
}

$this->db->insert_batch('tag_restaurant_rel', $insert);

#4
[eluser]loganbest[/eluser]
Because if they uncheck those tags when they update the record they need to be removed from the relational table.

Also do I just set $tags as $this->input->post( 'taglist', true )

#5
[eluser]loganbest[/eluser]
Call to undefined method CI_DB_mysql_driver::insert_batch()

Why do I get this?

I have:
$active_group = "default";
$active_record = TRUE;

#6
[eluser]danmontgomery[/eluser]
[quote author="loganbest" date="1304986983"]Because if they uncheck those tags when they update the record they need to be removed from the relational table.

Also do I just set $tags as $this->input->post( 'taglist', true )[/quote]

If you're deleting and adding records anyways, there's no point in only deleting what's not checked. Just delete all of the records for that restaurant and re-add what's necessary, like what's in my example.

#7
[eluser]danmontgomery[/eluser]
[quote author="loganbest" date="1304987981"]Call to undefined method CI_DB_mysql_driver::insert_batch()

Why do I get this?

I have:
$active_group = "default";
$active_record = TRUE;[/quote]

You're using an old version? insert_batch and update_batch have been around for over a year. If you're not going to upgrade you would have to run an insert query for each row, or manually construct a batch insert statement.

https://bitbucket.org/ellislab/codeigniter-reactor


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.