简体   繁体   中英

Joining tables CakePHP belongsTo, hasMany

Tl;Dr: How to create one array; from two tables; with hasMany and belongsTo associations; to foreach through; with CakePHP; optimally.

EDIT: From(http://book.cakephp.org/view/872/Joining-tables) This is what I'm trying to learn how to do although it says "hasOne" and not hasMany....

"In CakePHP some associations (belongsTo and hasOne) performs automatic joins to retrieve data, so you can issue queries to retrieve models based on data in the related one."

I'm trying to foreach through an array that will ideally contain data from two tables that have a hasMany and belongsTo relationship. I know that CakePHP has functionality built in to make this very simple and would like to use it:

  1. trips hasMany legs
  2. legs belongsTo trips

So, I want to make an array containing data from both tables so I can easily display the data in the view. Notice there is nothing about joining in the array that contains find parameters. I think CakePHP joins tables when I create associations; I'm just not sure how to access those associations when creating an array.

(I've spent hours on this already and couldn't find an example of how to do it on the web or in books so thanks for your help). I could hack my way around this (by creating a separate array and having a much more convoluted view file) but I'm just starting to learn to program and would like to do it somewhat optimally and use CakePHP's functionality.

Thanks in advance for your help! Am I not right that with these associations (belongsTo and hasMany) I don't have to explicitly declare the joins in the controller/find()? I might, for now, try and manually make the joins to see what happens.

Existing code (this works fine):

trips_controllers:

function view() {
    if(empty($this->data)){
        $this->Session->setFlash('You forgot to put stuff in the form fields!');
        $this->redirect(array('action'=>'index'));
    }
    $price=$this->data['Trip']['price'];
    $origin=$this->data['Trip']['origin_airport'];
    $this->set('trips', $this->Trip->find('all', array('conditions'=>array('Trip.price <='=>$price, 'Trip.origin_airport'=>$origin), 'limit'=>30, 'recursive=>2')));

view:

<th>Trip ID</th>
            <th>Price</th>
            <th>Origin</th  
            </tr>
            <? foreach($trips as $trip):?>
            <tr>
                <td><?=$trip['Trip']['trip_id'];?></td>
                <td><?=$html->link($trip['Trip']['url']);       ?>
                <td><?=$trip['Trip']['origin_airport'];?></td>
                <td><?=$trip['Trip']['price'];?></td>               
                </tr>
                <? endforeach;?>

Thanks again!

If Trip hasMany Legs, you should be able to loop through the data like so:

foreach ($trips as $trip) {
    echo $trip['Trip']['id'];
    …
    foreach ($trip['Leg'] as $leg) {
        echo $leg['id'];
        …
    }
}

debug($trips) is your friend. The default Cake data array layout is very sensible. Try to get used to it, there should be very little need to modify the structure in 99.99% of applications. On the contrary, there's a strong case to be made for keeping it consistent everywhere.

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