2012-11-23 22 views
0

我想在控制器中使用第二個包含數據的表,以便在視圖中使用它。 在這一點上,我有一個名爲'PortfoliosController.php'的控制器。在這個控制器中我有一個名爲Index的公共函數,在這裏我想加入它。在CakePHP中加入的工作原理

到目前爲止,我有下面的代碼(只有我仍然無法從連接表訪問數據):

public function index() { 
    $this->Portfolio->recursive = -1; 

    $options = $this->Portfolio->find('all', array('joins' => array(
     array(
      'table' => 'students', 
      'alias' => 'Student', 
      'type' => 'LEFT', 
      'foreignKey' => true, 
      'conditions'=> array('Student.userid = Portfolio.userid') 
     ) 
    ))); 

    $this->set('portfolios', $this->Portfolio->find('all', $options)); 
} 

是有人在這裏看到了問題,或者有一個答案,可能幫助!

回答

1

從聯結表中獲取數據的最佳方法是在組合模型中的兩個表之間建立關係。

因此,在這種情況下,它聽起來像一個hasmany關係將被使用,因爲一個投資組合可能包含來自第二個表的許多結果?這意味着,當您查找「全部」時,不僅會查找該表中的所有數據,還會查找來自第二個表的數據。關於這裏建立關係http://book.cakephp.org/1.3/view/1040/Relationship-Types

0

模型

陣列( '的className'=> 'State_master', '外鍵'=>假, '型'

更多信息=>'左」, '條件'=>數組( 'State_master.state_id = Student_master.student_state'), '依賴'=>真 ) 'City_master'=>數組( '的className'=> 'City_master', 'foreignKey'=> false, '類型'=> '左', '條件'=>數組( 'City_master.city_id = Student_master.student_city'), '依賴'=>真 ) 'Class_master'=>數組( 「的className '=>'Class_master', 'foreignKey'=> false, 'type'=>'left', 'conditions'=> array('Class_master.class_id = Student_master.student_class'), 'dependent'=> true ) ); } >

控制器:

$這 - >分頁=陣列( '限制'=> 25, '訂單'=>數組( 'admin_id'=> 'ASC'), 「條件'=>數組(' school_admin_id '=> $這 - >會話級>讀(' Auth.User.school_admin_id')));

   $stlist = $this->paginate('Student_master'); 
       $this->set('stlist',$stlist); 

       $this->set('students_data',$stlist); 

視圖:

<tr> 
    <th><input type="checkbox" name="checkall" id="checkall" onclick='checkedAll();'></th> 
    <th>Name</th> 
    <th>Email ID</th> 
    <th>Contact No</th> 
    <th>Class</th> 
    <th>Status</th> 
    <th>Option</th> 
</tr> 

<?php 
    $rowcount=0; 
    foreach ($students_data as $st_data) { 
    $student_status=$st_data['Student_master']['student_status']; 
    $student_id=$st_data['Student_master']['student_id']; 
    $rowcount++; 

    if($rowcount%2==0){ 
     $class='evenrow'; 
    }else{ 
     $class='oddrow'; 
    } 

?> 

<tr class='<?php echo $class;?>'> 
<td> 

<?php echo $this->Form->input("checkbox_std.", array("type" => "checkbox","value"=>$student_id));?> 
</td> 
    <td><?php echo $st_data['Student_master']['student_name'];?></td> 
    <td><?php echo $st_data['Student_master']['student_email'];?></td> 
    <td><?php echo $st_data['Student_master']['student_contact'];?></td> 
    <td><?php echo $st_data['Class_master']['class_name'];?></td> 


    <td> 
     <?php 

      if($student_status==0){ 

     ?> 
     <input type='button' value='Activate'> 
     <?php }else{ ?> 
     <input type='button' value='De-Activate'> 
     <?php } ?> 

    </td> 
    <td> 
     <input type='button' value='Edit'> 
    <input type='button' value='View'> 
    <input type='button' value='Delete'> 

    </td> 

</tr>