简体   繁体   English

无法加入cakephp中的表

[英]unable to join tables in cakephp

I have a two tables in database accommodations and locations . 我在数据库accommodationslocations有两个表。 The accommodations table structure is : accommodations表的结构为:

id name location_id        
1  A     2

And the locations table structure is similar to : locations表的结构类似于:

id name
2  PQ

The AccommodationModel.php is: AccommodationModel.php是:

class AccommodationModel extends AppModel{
    var $name = 'Accommodation';
    var $hasOne = array(
        'Location' => array(
            'className' => 'Location',
        ),
    );
}

But when I tried to get the accommodation name and location name from AccommodationsController 但是当我尝试从AccommodationsController获取accommodation namelocation name

$accommodations = $this->Accommodation->find('all',array('conditions' => $conditions,'order' => $order,'limit'=>$limit));

It only shows the Accommodation array. 它仅显示Accommodation数组。 Why so and how can I get the location name ? 为什么这样,如何获取位置name

What CakePHP-Version are you Using? 您正在使用哪个CakePHP版本? Since 2.x its (without "Model" in Classname) 从2.x开始(在类名中没有“ Model”)

class Accommodation extends AppModel {

Anyways, here you go: 无论如何,您可以在这里:

var $hasOne = array('Location');

..is all you need for simple hasOne - association, if you follow all Cake(Naming)Conventions. 如果遵循所有Cake(Naming)Conventions,则仅需要简单的hasOne-关联即可。

Whats the result? 结果如何?

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

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