Reputation: 21
I’m totally new at CI and Datamapper and i wanna do a very simple thing, i think.
I have a database with 3 tables
courses
students
students_courses
I’m using this models
Student
<?php
class Student extends DataMapper {
var $has_many = array('course');
}
Courses
<?php
class Course extends DataMapper {
var $has_many = array('student');
}
And this controller to add students and select their courses
Students Controller
function add(){
$estudiante = new Student();
$estudiante->name = $this->input->post('nombre');
$estudiante->save();
$user = new Student();
$curso = new Course();
$user->get_by_name($estudiante->name);
$curso->get_by_name($this->input->post('curso'));
$user->save($curso);
$this->load->view('student/confirm');
}
and, finally, this form on the view
<p>
<label for="nombre">Nombre:</label>
<input type="text" name="nombre" id="nombre">
</p>
<p>
<label for="nombre">Curso:</label>
<select multiple name="curso" id="curso">
<?php
foreach($course_list as $item) {
echo "<option value='$item->name'>" . "$item->name" . "</option>";
}
?>
</select>
</p>
<input type="submit" value="submit">
<?php echo form_close(); ?>
All works great when i want to save ONE value from the select list….but, what i have to do to save MORE than one value?
Thanks!!!
Upvotes: 0
Views: 542
Reputation: 3549
I think your problem could be related with database modelling. You need to specify an many-to-many relationship between Student
and Course
.
Also, you need to iterate over $this->input->post('curso')
to get all Courses an Student may have. Something like this:
foreach ($this->input->post('curso') as $entry)
{
$curso = new Course();
$curso->get_by_name($entry);
$user->save($curso);
}
I don't have experience with DataMapper, but I think your problem can be solved this way :)
Upvotes: 2