2011-02-09 2 views
0

저는 CI와 Datamapper를 처음 접했고 아주 간단한 것을하고 싶습니다.Datamapper : 관계에 하나 이상의 값을 저장하십시오.

나는 학생을 추가하는 3 개 테이블

courses 
students 
students_courses 

나는 학생

이 모델

을 사용하고

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

과정

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

와 데이터베이스 그리고이 컨트롤러가 및 나누었다 ...

학생 컨트롤러

  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'); 

      } 

내가 선택 목록에서 하나의 값을 저장할 때

<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(); ?> 

모든 위대한 작품을보기에, 마지막으로,이 양식을 자신의 코스 선택 하나 이상의 값을 저장하려면 어떻게해야합니까?

감사합니다 !!!

답변

2

문제점은 데이터베이스 모델링과 관련 될 수 있다고 생각합니다. StudentCourse 사이에 다 대다 관계를 지정해야합니다.

또한 학생이 가질 수있는 모든 과정을 보려면 $this->input->post('curso')을 반복해야합니다. 이런 식으로 뭔가가 :

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

나는 DataMapper 경험이없는,하지만 난 당신의 문제가이 방법으로 해결 될 수 있다고 생각합니다 :)

관련 문제