Welcome Guest, Not a member yet? Register   Sign In
Update relations with DataMapper
#1

[eluser]liebgott[/eluser]
Hi!!

I'm testing things with Datamapper and I'm using this code to save (and update). Works great when i change 'name', but in the join table adds another relation instead delete all first when i select one new relation.


Code:
$estudiante=new Student();
        $estudiante->where('id', '17')->get();
        $estudiante->name=$this->input->post('nombre');    
        foreach ($this->input->post('curso') as $key => $value) {
              $curso = new Course();
            $curso->get_by_name($value);
            $estudiante->save($curso);

            }

I mean, i have this in my join table

Code:
id_student | id_course

17             1
17             2

When i update from form selecting a different course, and only one (e.g., course with id 3) -->

Code:
id_student | id_course

17             1
17             2
17             3

instead of
Code:
id_student | id_course

17             3

What i'm doing wrong????

Thanks!!
#2

[eluser]WanWizard[/eluser]
You have defined a many-to-many relationship, so it's quite logical that it adds another one, it's perfectly valid. If a student only does one course at the time, make it a one-to-many relation.

If you want to update all relations in one go, i.e. from a multiselect on a form, you will have to delete the old relations first.
#3

[eluser]liebgott[/eluser]
That's right. Thanks!!




Theme © iAndrew 2016 - Forum software by © MyBB