codeigniter - Datamapper: Save more than one value in a relationship -


i’m totally new @ ci , datamapper , wanna simple thing, think.

i have database 3 tables

 courses  students  students_courses  

i’m using models

student

        <?php            class student extends datamapper {            var $has_many = array('course');           }  

courses

        <?php           class course extends datamapper {           var $has_many = array('student');         }  

and controller add students , select 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, form on 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 $item) {              echo "<option value='$item->name'>" . "$item->name" . "</option>";              }               ?>         </select>     </p>      <input type="submit" value="submit">   <?php echo form_close(); ?>  

all works great when want save 1 value select list….but, have save more 1 value?

thanks!!!

i think problem related database modelling. need specify many-to-many relationship between student , course.

also, need iterate on $this->input->post('curso') courses student may have. this:

foreach ($this->input->post('curso') $entry) {     $curso = new course();     $curso->get_by_name($entry);     $user->save($curso); } 

i don't have experience datamapper, think problem can solved way :)


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -