简体   繁体   中英

How exactly does a join work in CakePHP

I'd like to have a second table with data in my controller for using it in the view. At this point I have a controller named 'PortfoliosController.php'. In this controller I have a public function called Index, here I'd like to have a join in it.

So far I have the following code (only I still can't access the data from the join table):

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

Is anyone here seeing a problem, or have a answer that might help!?

The best way to get the data from joining tables, would be to set up a relationship between the two tables in your portfolio model.

So in this case it sounds like a hasmany relationship would be used, because a portfolio could contain many results from the second table? This meant that when you do a find 'all', not only would it find all the the data from that table, but also the data from the second table due to its relationship.

more info about setting up the relationships here http://book.cakephp.org/1.3/view/1040/Relationship-Types

your model

array( 'className' => 'State_master', 'foreignKey' => false, 'type'=>'left', 'conditions' => 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' => false, 'type'=>'left', 'conditions' => array('Class_master.class_id = Student_master.student_class'), 'dependent' => true ) ); } ?>

controller:

$this->paginate = array('limit' => 25,'order' => array('admin_id' => 'asc'), 'conditions'=>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); 

view:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM