簡體   English   中英

聯接如何在CakePHP中正常工作

[英]How exactly does a join work in CakePHP

我想在控制器中有第二個表,其中包含數據以便在視圖中使用它。 此時,我有一個名為“ PortfoliosController.php”的控制器。 在這個控制器中,我有一個名為Index的公共函數,在這里我想加入其中的一個聯接。

到目前為止,我有以下代碼(只有我仍然無法訪問聯接表中的數據):

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

這里有人看到問題,或者有可能對您有幫助的答案嗎?

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

因此,在這種情況下,聽起來好像要使用倉促關系,因為投資組合可能包含第二個表中的許多結果? 這意味着當您查找“ all”時,由於它的關系,它不僅會從該表中查找所有數據,而且還會從第二個表中查找數據。

有關在此處建立關系的更多信息,請參見http://book.cakephp.org/1.3/view/1040/Relationship-Types

您的模型

array('className'=>'State_master','foreignKey'=>否,'type'=>'left','condition'=> array('State_master.state_id = Student_master.student_state'),'dependent'=> true),'City_master'=> array('className'=>'City_master','foreignKey'=> false,'type'=>'left','conditions'=> array('City_master.city_id = Student_master.student_city '),'dependent'=> true),'Class_master'=> array('className'=>'Class_master','foreignKey'=>否,'type'=>'left','conditions'=> array( 'Class_master.class_id = Student_master.student_class'),'dependent'=> true)); }?>

控制器:

$ this-> paginate = array('limit'=> 25,'order'=> array('admin_id'=>'asc'),'condition'=> array('school_admin_id'=> $ this-> Session- > read('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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM