Welcome Guest, Not a member yet? Register   Sign In
Datamapper | m:m + join_fields edit form
#1

[eluser]Anestetikas[/eluser]
Hi,
I'm having some problems with making a good edit form to work with datamapper.

For example:

I have a table "Cars":
id, model, registration_number

and table job:
id, name, description

Also table for many to many relation:

cars_job:
id, car_id, job_id, due_date

I want to make a edit form for my cars where i could also manage all possible jobs related to it.

If it would only be m:m I could use multiselect, but here I need some kind of checkbox+input field (for due_date) combo.

How should I approach this?
#2

[eluser]Anestetikas[/eluser]
bump
#3

[eluser]Anestetikas[/eluser]
Wrote this one, how can I improve it?

generate edit_view:
Code:
....
$jobs = new Job;
            $jobs->get();

            $this->data['jobs'] = $jobs;

            $jobs_marked = new Job;
            $jobs_marked->include_join_fields();
            $jobs_marked->get_by_related($car);

            $this->data['jobs_marked'] = $jobs_marked;
...



edit_view:
Code:
....
foreach ($jobs as $job) {

                $value = FALSE;
                $value2 = "";

                foreach ($jobs_marked as $job_marked) {

                    if ($job->id == $job_marked->id) {
                        $value = TRUE;
                        $value2 = $job_marked->join_date;
                    }
                }

                $data = array(
                    'name' => "jobs[" . $job->id . "]",
                    'id' => $job->id,
                    'checked' => $value,
                );
                $data1 = array(
                    'name' => "jobs2[" . $job->id . "]",
                    'id' => $job->id,
                    'value' => set_value("jobs2[" . $job->id . "]", $value2),
                    'style' => 'width: 200px',
                );

                echo "<tr>";
                echo "<td class=\"redagavimas_pavadinimas\">";
                echo form_label($this->lang->line($job->description), $job->id, $label_options);
                echo "</td>";
                echo "<td class=\"none\">";

                echo form_checkbox($data) . form_input($data1);

                echo "</td>";
                echo "</tr>";
            }
....


save controller:
Code:
....
                $jobs_old = new Job;
                $jobs_old->get_by_related($car);


                //delete old job relations
                $car->delete($jobs_old->all);
            }

    //set new car details
            foreach ($this->input->post() as $post => $value) {

                $car->{$post} = $value;
            }

    //if from add unset id
            if ($this->input->post('id') == "X")
                unset($car->id);

            $car->user_id = $this->ion_auth->get_user()->id;

    //declare new jobs
            $jobs_array = array();

            foreach ($this->input->post('jobs') as $job => $value) {
                $jobs_array[] = $value;
            }
            $jobs = new Job;
            $jobs->where_in('id', $jobs_array);
            $jobs->get();

            //save all new job relations
            $car->save($jobs->all);

            //save join fields
            $join_fields = $this->input->post('jobs2');

            foreach ($jobs as $job) {

                if (!empty($join_fields[$jobs->id]))
                    $job->set_join_field($car, 'due_date', $join_fields[$job->id]);
            }
...




Theme © iAndrew 2016 - Forum software by © MyBB