简体   繁体   English

联接表CakePHP属于,hasMany

[英]Joining tables CakePHP belongsTo, hasMany

Tl;Dr: How to create one array; Tl; Dr:如何创建一个数组; from two tables; 从两个表; with hasMany and belongsTo associations; 与hasMany和belongsTo关联; to foreach through; 通过 with CakePHP; 与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.... 编辑:From(http://book.cakephp.org/view/872/Joining-tables)这是我正在尝试学习的方法,尽管它说“ hasOne”而不是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." “在CakePHP中,某些关联(belongsTo和hasOne)执行自动联接以检索数据,因此您可以发出查询以基于相关联的数据检索模型。”

I'm trying to foreach through an array that will ideally contain data from two tables that have a hasMany and belongsTo relationship. 我试图遍历一个数组,该数组将理想地包含来自具有hasMany和belongsTo关系的两个表中的数据。 I know that CakePHP has functionality built in to make this very simple and would like to use it: 我知道CakePHP具有内置的功能,可以使其变得非常简单,并希望使用它:

  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. 注意,没有任何关于加入包含find参数的数组的操作。 I think CakePHP joins tables when I create associations; 我认为CakePHP在创建关联时会联接表。 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. 我可以解决这个问题(通过创建一个单独的数组并使用更复杂的视图文件),但是我才刚刚开始学习编程,并希望在某种程度上做得更好,并使用CakePHP的功能。

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()? 使用这些关联(belongsTo和hasMany),我不必在controller / find()中显式声明联接吗? I might, for now, try and manually make the joins to see what happens. 现在,我可能会尝试手动进行联接以查看会发生什么。

Existing code (this works fine): 现有代码(可以正常工作):

trips_controllers: 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: 如果Trip有很多分支,您应该可以像这样遍历数据:

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

debug($trips) is your friend. debug($trips)是您的朋友。 The default Cake data array layout is very sensible. 默认的Cake数据数组布局非常明智。 Try to get used to it, there should be very little need to modify the structure in 99.99% of applications. 尝试适应它,在99.99%的应用程序中几乎不需要修改结构。 On the contrary, there's a strong case to be made for keeping it consistent everywhere. 相反,有很强的理由要使其在所有地方保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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