繁体   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